module SidekiqScheduler::Schedule
def get_all_schedules
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
def get_schedule(name = nil)
if the name is nil it returns a hash with all the
Retrive the schedule configuration for the given name
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
def prepare_schedule(schedule_hash)
def prepare_schedule(schedule_hash) prepared_hash = {} schedule_hash.each do |name, job_spec| job_spec = job_spec.dup job_spec['class'] = name unless job_spec.key?('class') || job_spec.key?(:class) prepared_hash[name] = job_spec end prepared_hash end
def reload_schedule!
def reload_schedule! @schedule = get_schedule end
def remove_schedule(name)
def remove_schedule(name) Sidekiq.redis { |r| r.hdel(:schedules, name) } Sidekiq.redis { |r| r.sadd(:schedules_changed, name) } end
def schedule
def schedule @schedule ||= {} end
def schedule=(schedule_hash)
params is an array, each element in the array is passed as a separate
:description is just that, a description of the job (optional). If
passed in a params. (optional)
:args can be any yaml which will be converted to a ruby literal and
will be used as :class.
:class must be a sidekiq worker class. If it is missing, the job name (hash key)
for valid syntax. If :cron is present it will take precedence over :every.
:every can be used in lieu of :cron. see rufus-scheduler's 'every' usage
:cron can be any cron scheduling string
"MakeTea" is used both as job name and sidekiq worker class.
is used implicitly as "class" argument - in the "MakeTea" example,
the scheduled job. If the "class" argument is missing, the key
Hash keys can be anything and are used to describe and reference
}
...
"description" => "this thing works it"s butter off" },
"args" => "work on this string",
"class" => "DoSomeWork",
"cron" => "5/* * * *",
"some_name" => {
"every" => "1m" },
"MakeTea" => {
{
Accepts a new schedule configuration of the form:
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
def set_schedule(name, config)
:queue => 'high',
:every => '15mins',
Sidekiq.set_schedule('some_job', { :class => 'SomeJob',
not constants.
Note: values for class and custom_job_class need to be strings,
Create or update a schedule with the provided name and configuration.
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