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, 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) unless r.type(:schedules_changed) == '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) }
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, 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, from, "(#{to}") }
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
  '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
  '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)
  "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
      r.zadd(job_key, time.to_i, time.to_i)
      r.expire(job_key, REGISTERED_JOBS_THRESHOLD_IN_SECONDS)
    end
  end

  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, 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?
  Sidekiq.redis { |r| r.exists(: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
  '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, 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, 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, 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