Module: SidekiqScheduler::RedisManager
- Defined in:
- lib/sidekiq-scheduler/redis_manager.rb
Constant Summary collapse
- REGISTERED_JOBS_THRESHOLD_IN_SECONDS =
24 * 60 * 60
Class Method Summary collapse
-
.add_schedule_change(name) ⇒ Object
Register a schedule change for a given job.
-
.clean_schedules_changed ⇒ Object
Remove all the schedule changes records.
-
.get_all_schedules ⇒ Hash
Returns the schedules of all the jobs.
-
.get_job_last_time(name) ⇒ String
Returns the last execution time of a given job.
-
.get_job_next_time(name) ⇒ String
Returns the next execution time for a given job.
-
.get_job_schedule(name) ⇒ String
Returns the schedule of a given job.
-
.get_job_state(name) ⇒ String
Returns the state of a given job.
-
.get_schedule_changes(from, to) ⇒ Array
Returns all the schedule changes for a given time range.
-
.key_prefix ⇒ String
Returns the key prefix used to generate all scheduler keys.
-
.key_prefix=(value) ⇒ Object
Sets the key prefix used to scope all scheduler keys.
-
.last_times_key ⇒ String
Returns the key of the Redis hash for job’s last execution times hash.
-
.next_times_key ⇒ String
Returns the key of the Redis hash for job’s execution times hash.
-
.pushed_job_key(job_name) ⇒ String
Returns the key of the Redis sorted set used to store job enqueues.
-
.register_job_instance(job_name, time) ⇒ Boolean
Registers a queued job instance.
-
.remove_elder_job_instances(job_name) ⇒ Object
Removes instances of the job older than 24 hours.
-
.remove_job_next_time(name) ⇒ Object
Removes the next execution time for a given job.
-
.remove_job_schedule(name) ⇒ Object
Removes the schedule for a given job.
-
.schedule_exist? ⇒ Boolean
Returns boolean value that indicates if the schedules value exists.
-
.schedules_changed_key ⇒ String
Returns the Redis’s key for saving schedule changes.
-
.schedules_key ⇒ String
Returns the Redis’s key for saving schedules.
-
.schedules_state_key ⇒ String
Returns the Redis’s key for saving schedule states.
-
.set_job_last_time(name, last_time) ⇒ Object
Sets the last execution time for a given job.
-
.set_job_next_time(name, next_time) ⇒ Object
Sets the next execution time for a given job.
-
.set_job_schedule(name, config) ⇒ Object
Sets the schedule for a given job.
-
.set_job_state(name, state) ⇒ Object
Sets the state for a given job.
Class Method Details
.add_schedule_change(name) ⇒ Object
Register a schedule change for a given job
115 116 117 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 115 def self.add_schedule_change(name) Sidekiq.redis { |r| r.zadd(schedules_changed_key, Time.now.to_f, name) } end |
.clean_schedules_changed ⇒ Object
Remove all the schedule changes records
120 121 122 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 120 def self.clean_schedules_changed Sidekiq.redis { |r| r.del(schedules_changed_key) unless r.type(schedules_changed_key) == 'zset' } end |
.get_all_schedules ⇒ Hash
Returns the schedules of all the jobs
91 92 93 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 91 def self.get_all_schedules Sidekiq.redis { |r| r.hgetall(schedules_key) } end |
.get_job_last_time(name) ⇒ String
Returns the last execution time of a given job
38 39 40 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 38 def self.get_job_last_time(name) hget(last_times_key, name) end |
.get_job_next_time(name) ⇒ String
Returns the next execution time for a given job
29 30 31 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 29 def self.get_job_next_time(name) hget(next_times_key, name) end |
.get_job_schedule(name) ⇒ String
Returns the schedule of a given job
11 12 13 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 11 def self.get_job_schedule(name) hget(schedules_key, name) end |
.get_job_state(name) ⇒ String
Returns the state of a given job
20 21 22 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 20 def self.get_job_state(name) hget(schedules_state_key, name) end |
.get_schedule_changes(from, to) ⇒ Array
Returns all the schedule changes for a given time range.
108 109 110 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 108 def self.get_schedule_changes(from, to) SidekiqScheduler::SidekiqAdapter.redis_zrangebyscore(schedules_changed_key, from, "(#{to}") end |
.key_prefix ⇒ String
Returns the key prefix used to generate all scheduler keys
200 201 202 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 200 def self.key_prefix @key_prefix end |
.key_prefix=(value) ⇒ Object
Sets the key prefix used to scope all scheduler keys
207 208 209 210 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 207 def self.key_prefix=(value) value = "#{value}:" if value && !%w[. :].include?(value[-1]) @key_prefix = value end |
.last_times_key ⇒ String
Returns the key of the Redis hash for job’s last execution times hash
172 173 174 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 172 def self.last_times_key "#{key_prefix}sidekiq-scheduler:last_times" end |
.next_times_key ⇒ String
Returns the key of the Redis hash for job’s execution times hash
165 166 167 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 165 def self.next_times_key "#{key_prefix}sidekiq-scheduler:next_times" end |
.pushed_job_key(job_name) ⇒ String
Returns the key of the Redis sorted set used to store job enqueues
158 159 160 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 158 def self.pushed_job_key(job_name) "#{key_prefix}sidekiq-scheduler:pushed:#{job_name}" end |
.register_job_instance(job_name, time) ⇒ Boolean
Registers a queued job instance
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 130 def self.register_job_instance(job_name, time) job_key = pushed_job_key(job_name) registered, _ = Sidekiq.redis do |r| r.multi do |m| m.zadd(job_key, time.to_i, time.to_i) m.expire(job_key, REGISTERED_JOBS_THRESHOLD_IN_SECONDS) end end registered.instance_of?(Integer) ? (registered > 0) : registered end |
.remove_elder_job_instances(job_name) ⇒ Object
Removes instances of the job older than 24 hours
145 146 147 148 149 150 151 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 145 def self.remove_elder_job_instances(job_name) seconds_ago = Time.now.to_i - REGISTERED_JOBS_THRESHOLD_IN_SECONDS Sidekiq.redis do |r| r.zremrangebyscore(pushed_job_key(job_name), 0, seconds_ago) end end |
.remove_job_next_time(name) ⇒ Object
Removes the next execution time for a given job
84 85 86 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 84 def self.remove_job_next_time(name) hdel(next_times_key, name) end |
.remove_job_schedule(name) ⇒ Object
Removes the schedule for a given job
77 78 79 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 77 def self.remove_job_schedule(name) hdel(schedules_key, name) end |
.schedule_exist? ⇒ Boolean
Returns boolean value that indicates if the schedules value exists
98 99 100 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 98 def self.schedule_exist? SidekiqScheduler::SidekiqAdapter.redis_key_exists?(schedules_key) end |
.schedules_changed_key ⇒ String
Returns the Redis’s key for saving schedule changes.
193 194 195 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 193 def self.schedules_changed_key "#{key_prefix}schedules_changed" end |
.schedules_key ⇒ String
Returns the Redis’s key for saving schedules.
186 187 188 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 186 def self.schedules_key "#{key_prefix}schedules" end |
.schedules_state_key ⇒ String
Returns the Redis’s key for saving schedule states.
179 180 181 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 179 def self.schedules_state_key "#{key_prefix}sidekiq-scheduler:states" end |
.set_job_last_time(name, last_time) ⇒ Object
Sets the last execution time for a given job
70 71 72 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 70 def self.set_job_last_time(name, last_time) hset(last_times_key, name, String(last_time)) end |
.set_job_next_time(name, next_time) ⇒ Object
Sets the next execution time for a given job
62 63 64 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 62 def self.set_job_next_time(name, next_time) hset(next_times_key, name, String(next_time)) end |
.set_job_schedule(name, config) ⇒ Object
Sets the schedule for a given job
46 47 48 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 46 def self.set_job_schedule(name, config) hset(schedules_key, name, JSON.generate(config)) end |
.set_job_state(name, state) ⇒ Object
Sets the state for a given job
54 55 56 |
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 54 def self.set_job_state(name, state) hset(schedules_state_key, name, JSON.generate(state)) end |