moduleSidekiqSchedulermoduleRedisManagerREGISTERED_JOBS_THRESHOLD_IN_SECONDS=24*60*60# Returns the schedule of a given job## @param [String] name The name of the job## @return [String] schedule in JSON formatdefself.get_job_schedule(name)hget('schedules',name)end# Returns the state of a given job## @param [String] name The name of the job## @return [String] state in JSON formatdefself.get_job_state(name)hget(schedules_state_key,name)end# Returns the next execution time for a given job## @param [String] name The name of the job## @return [String] next time the job has to be executeddefself.get_job_next_time(name)hget(next_times_key,name)end# Returns the last execution time of a given job## @param [String] name The name of the job## @return [String] last time the job was executeddefself.get_job_last_time(name)hget(last_times_key,name)end# Sets the schedule for a given job## @param [String] name The name of the job# @param [Hash] config The new schedule for the jobdefself.set_job_schedule(name,config)hset('schedules',name,JSON.generate(config))end# Sets the state for a given job## @param [String] name The name of the job# @param [Hash] state The new state for the jobdefself.set_job_state(name,state)hset(schedules_state_key,name,JSON.generate(state))end# Sets the next execution time for a given job## @param [String] name The name of the job# @param [String] next_time The next time the job has to be executeddefself.set_job_next_time(name,next_time)hset(next_times_key,name,next_time)end# Sets the last execution time for a given job## @param [String] name The name of the job# @param [String] last_time The last time the job was executeddefself.set_job_last_time(name,last_time)hset(last_times_key,name,last_time)end# Removes the schedule for a given job## @param [String] name The name of the jobdefself.remove_job_schedule(name)hdel('schedules',name)end# Removes the next execution time for a given job## @param [String] name The name of the jobdefself.remove_job_next_time(name)hdel(next_times_key,name)end# Returns the schedules of all the jobs## @return [Hash] hash with all the job schedulesdefself.get_all_schedulesSidekiq.redis{|r|r.hgetall('schedules')}end# Returns boolean value that indicates if the schedules value exists## @return [Boolean] true if the schedules key is set, false otherwisedefself.schedule_exist?Sidekiq.redis{|r|r.exists?('schedules')}end# Returns all the schedule changes for a given time range.## @param [Float] from The minimum value in the range# @param [Float] to The maximum value in the range## @return [Array] array with all the changed job namesdefself.get_schedule_changes(from,to)Sidekiq.redis{|r|r.zrangebyscore('schedules_changed',from,"(#{to}")}end# Register a schedule change for a given job## @param [String] name The name of the jobdefself.add_schedule_change(name)Sidekiq.redis{|r|r.zadd('schedules_changed',Time.now.to_f,name)}end# Remove all the schedule changes recordsdefself.clean_schedules_changedSidekiq.redis{|r|r.del('schedules_changed')unlessr.type('schedules_changed')=='zset'}end# Removes a queued job instance## @param [String] job_name The name of the job# @param [Time] time The time at which the job was cleared by the scheduler## @return [Boolean] true if the job was registered, false otherwisedefself.register_job_instance(job_name,time)job_key=pushed_job_key(job_name)registered,_=Sidekiq.redisdo|r|r.pipelineddo|pipeline|pipeline.zadd(job_key,time.to_i,time.to_i)pipeline.expire(job_key,REGISTERED_JOBS_THRESHOLD_IN_SECONDS)endendregisteredend# Removes instances of the job older than 24 hours## @param [String] job_name The name of the jobdefself.remove_elder_job_instances(job_name)seconds_ago=Time.now.to_i-REGISTERED_JOBS_THRESHOLD_IN_SECONDSSidekiq.redisdo|r|r.zremrangebyscore(pushed_job_key(job_name),0,seconds_ago)endend# Returns the key of the Redis sorted set used to store job enqueues## @param [String] job_name The name of the job## @return [String] the pushed job keydefself.pushed_job_key(job_name)"sidekiq-scheduler:pushed:#{job_name}"end# Returns the key of the Redis hash for job's execution times hash## @return [String] with the keydefself.next_times_key'sidekiq-scheduler:next_times'end# Returns the key of the Redis hash for job's last execution times hash## @return [String] with the keydefself.last_times_key'sidekiq-scheduler:last_times'end# Returns the Redis's key for saving schedule states.## @return [String] with the keydefself.schedules_state_key'sidekiq-scheduler:states'endprivate# Returns the value of a Redis stored hash field## @param [String] hash_key The key name of the hash# @param [String] field_key The key name of the field## @return [String]defself.hget(hash_key,field_key)Sidekiq.redis{|r|r.hget(hash_key,field_key)}end# Sets the value of a Redis stored hash field## @param [String] hash_key The key name of the hash# @param [String] field_key The key name of the field# @param [String] value The new value name for the fielddefself.hset(hash_key,field_key,value)Sidekiq.redis{|r|r.hset(hash_key,field_key,value)}end# Removes the value of a Redis stored hash field## @param [String] hash_key The key name of the hash# @param [String] field_key The key name of the fielddefself.hdel(hash_key,field_key)Sidekiq.redis{|r|r.hdel(hash_key,field_key)}endendend