#61 Periodic Tasks with sidekiq-cron


Using a third-party add-on, sidekiq-cron, we can create periodic tasks to extend the functionality of sidekiq.
rails background processing 4:59


Gemfilegem 'sidekiq-cron'
config/initializers/sidekiq.rbSidekiq.configure_server do |config|
  config.redis = { url: 'redis://localhost:6379/0'  }
  schedule_file = "config/schedule.yml"
  if File.exists?(schedule_file)
    Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://localhost:6379/0'  }
  cron: "* * * * *"
  class: "GenerateRandomUserJob"
  queue: default
routes.rbRails.application.routes.draw do
  require 'sidekiq/web'
  require 'sidekiq/cron/web'
  mount Sidekiq::Web => '/sidekiq'

frank004 said about 2 years ago:

can we use  gem 'whenever' with the sidekiq-cron?

kobaltz PRO said about 2 years ago:

You could. However, I would only do this if you need tasks ran on all of the web servers. The two should not conflict with each other.

randomor said about 2 years ago:

Thanks for the video. Are you sure this is for Sidekiq Pro/Enterprise only? Can't find the docs in the repo mentioning this restriction. 

kobaltz PRO said about 2 years ago:

sidekiq-cron is a separate, third-party addon for sidekiq. It does not require Pro/Enterprise. My reference in the video was meant to say that without this gem (or another third-party), and to still have the periodic task functionality, you would have to use Pro/Enterprise.

dancinglightning said about 2 years ago:

Thanks, actually for that whole series on sidekiq. Maybe some testing coming up ?

Having deployed the cron, i had to restart the sidekiq service. Off course. 

As you have everything else nicely setup for copy/paste, adding the schedule.yml file would make it even easier for the next guy.

And i must admit that at first hearing i also understood that this was only for use with enterprise edition. Only a bit of checking and listening again cleared that up.



kobaltz PRO said about 2 years ago:

I do plan on doing some testing. However, I think week after next, I'll be focusing on searchkick/elasticsearch.

I've updated the episode with the schedule.yml file. That was definitely an oversight, so thank you.

naveedkakal said about 2 years ago:

If I had sidekiq-pro, would this still be a good solution for scheduled jobs?  Or is there a built in sidekiq scheduler with pro?

I have been using the clockwork gem, but it requires me to maintain a 'special' mode, along with the gem author recently stopping maintenance on it.  Looking for alternatives and this definitely looks up my alley, thanks.

kobaltz PRO said about 2 years ago:

I don't think that sidekiq-pro offers recurring jobs.

For terminology sake, a scheduled job would be the result of an action telling the background processor to schedule the job for a later time. For example, if a new user signs up for your application, you can set a scheduled job to thank the user for signing up and give some tips about your application 24 hours after they sign up.

A recurring job or periodic task, would be something where you are wanting to perform a task every x intervals. For example, if you have a ToDo list application and you want to send a notification to your users every morning with an overview of what they still need to do or have completed the previous day, sidekiq-cron would be a good fit for the job.

You should be able to use sidekiq-cron along side sidekiq-pro. However, if you're using sidekiq-enterprise, there is no need for sidekiq-cron since this functionality is built into sidekiq-enterprise.

ondrejbartas said about 2 years ago:

Thanks for very nice video about Sidekiq-Cron.

I  added link to readme to this vide if it is ok with you :D

kobaltz PRO said about 2 years ago:

Thanks! Sounds good to me!

[email protected] PRO said 8 months ago:

FYI need to add gem 'rufus-scheduler', '~> 3.4.0'

please see https://github.com/ondrejbartas/sidekiq-cron/issues/201

setting up a new app and could not get this baby working! hope this helps


Login to Comment