Streamline Your Sidekiq Cron Jobs with a Single Worker Class for Rake Task Invocation

- 2 mins read
rails sidekiq sidekiq-cron

Running scheduled processes using Sidekiq Cron was a breeze. The setup process involves adding the Sidekiq Cron gem to the Gemfile and configuring the schedule.yml file to define cron schedule definitions. Additionally, the Sidekiq dashboard provides a convenient page to view the registered cron jobs.

Typically, when running a scheduled process, separate Sidekiq worker classes are created for each cron job, and within these workers, another service or rake task is invoked.

daily_backup_worker:
  cron: "0 0 * * *"
  class: "DailyBackupWorker"
  description: "Run daily backup at midnight"
weekly_backup_worker:
  cron: "0 0 * * 0"
  class: "WeeklyBackupWorker"
  description: "Run weekly backup on Sunday at midnight"

For example, in the DailyBackupWorker class, the existing rake task command db:backup is called to trigger the database backup:

require 'rake'
Rails.application.load_tasks

class DailyBackupWorker
  include Sidekiq::Worker

  def perform
    Rake::Task['db:backup'].execute
  end
end

Similarly, the WeeklyBackupWorker class would have similar code. However, it is possible to streamline the process by using a single class to handle rake task invocation within the Sidekiq cron worker.

Introducing the InvokeRakeTaskWorker class:

require 'rake'
Rails.application.load_tasks

class InvokeRakeTaskWorker
  include Sidekiq::Worker

  def perform(command)
    Rake::Task[command['task']].execute(command['args'])
  end
end

And you can change your schedule.yml into like this.

daily_backup_worker:
  cron: "0 0 * * *"
  class: "InvokeRakeTaskWorker"
  args:
    task: "db:backup"
  description: "Run daily backup at midnight"
weekly_backup_worker:
  cron: "0 0 * * 0"
  class: "InvokeRakeTaskWorker"
  args:
    task: "db:backup"
    args: "--weekly"
  description: "Run weekly backup on Sunday at midnight"

Conclusion

That it’s, now you can use single class to invoke rake command inside sidekiq cron schedule.

using a single class to handle rake task invocation within the Sidekiq cron worker brings benefits such as code reusability, simplified configuration, flexibility, easier maintenance, and improved testing capabilities. Carefully evaluate the trade-offs and consider the specific requirements of your application before deciding on the approach to use.