Module: SidekiqScheduler::Schedule
- Included in:
- Sidekiq
- Defined in:
- lib/sidekiq-scheduler/schedule.rb
Instance Method Summary collapse
-
#get_all_schedules ⇒ Object
gets the schedule as it exists in redis.
-
#get_schedule(name = nil) ⇒ Object
Retrive the schedule configuration for the given name if the name is nil it returns a hash with all the names end their schedules.
-
#reload_schedule! ⇒ Object
reloads the schedule from redis.
-
#remove_schedule(name) ⇒ Object
remove a given schedule by name.
- #schedule ⇒ Object
-
#schedule=(schedule_hash) ⇒ Object
Accepts a new schedule configuration of the form:.
-
#set_schedule(name, config) ⇒ Object
Create or update a schedule with the provided name and configuration.
Instance Method Details
#get_all_schedules ⇒ Object
gets the schedule as it exists in redis
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sidekiq-scheduler/schedule.rb', line 72 def get_all_schedules schedules = nil if Sidekiq.redis { |r| r.exists(:schedules) } schedules = {} Sidekiq.redis { |r| r.hgetall(:schedules) }.tap do |h| h.each do |name, config| schedules[name] = MultiJson.decode(config) end end end schedules end |
#get_schedule(name = nil) ⇒ Object
Retrive the schedule configuration for the given name if the name is nil it returns a hash with all the names end their schedules.
62 63 64 65 66 67 68 69 |
# File 'lib/sidekiq-scheduler/schedule.rb', line 62 def get_schedule(name = nil) if name.nil? get_all_schedules else encoded_schedule = Sidekiq.redis { |r| r.hget(:schedules, name) } encoded_schedule.nil? ? nil : MultiJson.decode(encoded_schedule) end end |
#reload_schedule! ⇒ Object
reloads the schedule from redis
55 56 57 |
# File 'lib/sidekiq-scheduler/schedule.rb', line 55 def reload_schedule! @schedule = get_schedule end |
#remove_schedule(name) ⇒ Object
remove a given schedule by name
106 107 108 109 |
# File 'lib/sidekiq-scheduler/schedule.rb', line 106 def remove_schedule(name) Sidekiq.redis { |r| r.hdel(:schedules, name) } Sidekiq.redis { |r| r.sadd(:schedules_changed, name) } end |
#schedule ⇒ Object
50 51 52 |
# File 'lib/sidekiq-scheduler/schedule.rb', line 50 def schedule @schedule ||= {} end |
#schedule=(schedule_hash) ⇒ Object
Accepts a new schedule configuration of the form:
{
"MakeTea" => {
"every" => "1m" },
"some_name" => {
"cron" => "5/* * * *",
"class" => "DoSomeWork",
"args" => "work on this string",
"description" => "this thing works it"s butter off" },
...
}
Hash keys can be anything and are used to describe and reference the scheduled job. If the “class” argument is missing, the key is used implicitly as “class” argument - in the “MakeTea” example, “MakeTea” is used both as job name and sidekiq worker class.
:cron can be any cron scheduling string
:every can be used in lieu of :cron. see rufus-scheduler’s ‘every’ usage for valid syntax. If :cron is present it will take precedence over :every.
:class must be a sidekiq worker class. If it is missing, the job name (hash key) will be used as :class.
:args can be any yaml which will be converted to a ruby literal and passed in a params. (optional)
:rails_envs is the list of envs where the job gets loaded. Envs are comma separated (optional)
:description is just that, a description of the job (optional). If params is an array, each element in the array is passed as a separate param, otherwise params is passed in as the only parameter to perform.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sidekiq-scheduler/schedule.rb', line 39 def schedule=(schedule_hash) schedule_hash = prepare_schedule(schedule_hash) if Sidekiq::Scheduler.dynamic schedule_hash.each do |name, job_spec| set_schedule(name, job_spec) end end @schedule = schedule_hash end |
#set_schedule(name, config) ⇒ Object
Create or update a schedule with the provided name and configuration.
Note: values for class and custom_job_class need to be strings, not constants.
Resque.set_schedule('some_job', {:class => 'SomeJob',
:every => '15mins',
:queue => 'high',
:args => '/tmp/poop'})
96 97 98 99 100 101 102 103 |
# File 'lib/sidekiq-scheduler/schedule.rb', line 96 def set_schedule(name, config) existing_config = get_schedule(name) unless existing_config && existing_config == config Sidekiq.redis { |r| r.hset(:schedules, name, MultiJson.encode(config)) } Sidekiq.redis { |r| r.sadd(:schedules_changed, name) } end config end |