Add-on for Sidekiq
Allows you to schedule recurring jobs for sidekiq workers using cron notation * * * * *.
- Redis 2.4 or greater is required.
- Sidekiq 2.13.1 or grater is required.
$ gem install sidekiq-cron
or add to your Gemfile
gem "sidekiq-cron", "~> 0.1.0"
If you are not using Rails you need to add require 'sidekiq-cron'
somewhere after require 'sidekiq'
.
Job properties:
{
'name' => 'name_of_job', #must be uniq!
'cron' => '1 * * * *',
'klass' => 'MyClass',
#OPTIONAL
'queue' => 'name of queue',
'args' => '[Array or Hash] of arguments hich will be passed to perform method'
}
class HardWorker
include Sidekiq::Worker
def perform(name, count)
# do something
end
end
Sidekiq::Cron::Job.create( name: 'Hard worker - every 5min', cron: '*/5 * * * *', klass: 'HardWorker')
# => true
create
method will return only true/false if job was saved or not.
job = Sidekiq::Cron::Job.new( name: 'Hard worker - every 5min', cron: '*/5 * * * *', klass: 'HardWorker')
if job.valid?
job.save
else
puts job.errors
end
#or simple
unless job.save
puts job.errors #will return array of errors
end
Load more jobs from hash:
hash = {
'name_of_job' => {
'class' => 'MyClass',
'cron' => '1 * * * *',
'args' => '(OPTIONAL) [Array or Hash]'
},
'My super iber cool job' => {
'class' => 'SecondClass',
'cron' => '*/5 * * * *'
}
}
Sidekiq::Cron::Job.load_from_hash hash
Load more jobs from array:
array = [
{
'name' => 'name_of_job',
'class' => 'MyClass',
'cron' => '1 * * * *',
'args' => '(OPTIONAL) [Array or Hash]'
},
{
'name' => 'Cool Job for Second Class',
'class' => 'SecondClass',
'cron' => '*/5 * * * *'
}
]
Sidekiq::Cron::Job.load_from_array array
or from YML (same notation as Resque-scheduler)
#config/shedule.yml
my_first_job:
cron: "*/5 * * * *"
class: "HardWorker"
queue: hard_worker
second_job:
cron: "*/30 * * * *"
class: "HardWorker"
queue: hard_worker_long
args:
hard: "stuff"
#initializers/sidekiq.rb
schedule_file = "config/schedule.yml"
if File.exists?(schedule_file)
Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
end
#return array of all jobs
Sidekiq::Cron::Job.all
#return one job by its uniq name - case sensitive
Sidekiq::Cron::Job.find "Job Name"
#return one job by its uniq name - you can use hash with 'name' key
Sidekiq::Cron::Job.find name: "Job Name"
#if job can't be found nil is returned
#destroys all jobs
Sidekiq::Cron::Job.destroy_all!
#destroy job by its name
Sidekiq::Cron::Job.destroy "Job Name"
#destroy founded job
Sidekiq::Cron::Job.find('Job name').destroy
job = Sidekiq::Cron::Job.find('Job name')
#disable cron scheduling
job.disable!
#enable cron scheduling
job.enable!
#get status of job:
job.status
# => enabled/disabled
#enqueue job right now!
job.enque!
How to start scheduling? Just start sidekiq workers by:
sidekiq
If you are using sidekiq web ui and you would like to add cron josb to web too,
add require 'sidekiq-cron'
after require 'sidekiq/web'
.
By this you will get:
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
- Fork the project.
- Start a feature/bugfix branch.
- Commit and push until you are happy with your contribution.
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright (c) 2013 Ondrej Bartas. See LICENSE.txt for further details.