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

Class Method Details

.add_schedule_change(name) ⇒ Object

Register a schedule change for a given job

Parameters:

  • name (String)

    The name of the 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_changedObject

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_schedulesHash

Returns the schedules of all the jobs

Returns:

  • (Hash)

    hash with all the job schedules



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

Parameters:

  • name (String)

    The name of the job

Returns:

  • (String)

    last time the job was executed



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

Parameters:

  • name (String)

    The name of the job

Returns:

  • (String)

    next time the job has to be executed



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

Parameters:

  • name (String)

    The name of the job

Returns:

  • (String)

    schedule in JSON format



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

Parameters:

  • name (String)

    The name of the job

Returns:

  • (String)

    state in JSON format



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.

Parameters:

  • from (Float)

    The minimum value in the range

  • to (Float)

    The maximum value in the range

Returns:

  • (Array)

    array with all the changed job names



108
109
110
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 108

def self.get_schedule_changes(from, to)
  Sidekiq.redis { |r| r.zrangebyscore(schedules_changed_key, from, "(#{to}") }
end

.key_prefixString

Returns the key prefix used to generate all scheduler keys

Returns:

  • (String)

    with the key prefix



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

Parameters:

  • value (String)

    The string to use as the prefix. A “:” will be appended as a delimiter if needed.



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_keyString

Returns the key of the Redis hash for job’s last execution times hash

Returns:

  • (String)

    with the key



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_keyString

Returns the key of the Redis hash for job’s execution times hash

Returns:

  • (String)

    with the key



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

Parameters:

  • job_name (String)

    The name of the job

Returns:

  • (String)

    the pushed job key



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

Removes a queued job instance

Parameters:

  • job_name (String)

    The name of the job

  • time (Time)

    The time at which the job was cleared by the scheduler

Returns:

  • (Boolean)

    true if the job was registered, false otherwise



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.pipelined do |pipeline|
      pipeline.zadd(job_key, time.to_i, time.to_i)
      pipeline.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

Parameters:

  • job_name (String)

    The name of the job



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

Parameters:

  • name (String)

    The name of the 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

Parameters:

  • name (String)

    The name of the 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

Returns:

  • (Boolean)

    true if the schedules key is set, false otherwise



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_keyString

Returns the Redis’s key for saving schedule changes.

Returns:

  • (String)

    with the key



193
194
195
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 193

def self.schedules_changed_key
  "#{key_prefix}schedules_changed"
end

.schedules_keyString

Returns the Redis’s key for saving schedules.

Returns:

  • (String)

    with the key



186
187
188
# File 'lib/sidekiq-scheduler/redis_manager.rb', line 186

def self.schedules_key
  "#{key_prefix}schedules"
end

.schedules_state_keyString

Returns the Redis’s key for saving schedule states.

Returns:

  • (String)

    with the key



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

Parameters:

  • name (String)

    The name of the job

  • last_time (String)

    The last time the job was executed



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

Parameters:

  • name (String)

    The name of the job

  • next_time (String)

    The next time the job has to be executed



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

Parameters:

  • name (String)

    The name of the job

  • config (Hash)

    The new schedule for the 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

Parameters:

  • name (String)

    The name of the job

  • state (Hash)

    The new state for the 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