robin-compat 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +28 -0
- data/COPYING +674 -0
- data/LICENSE +165 -0
- data/README.md +258 -0
- data/exe/sidekiq +5 -0
- data/exe/sidekiqswarm +6 -0
- data/lib/robin/compat/cron_line.rb +46 -0
- data/lib/robin/compat/version.rb +8 -0
- data/lib/robin-compat.rb +3 -0
- data/lib/sidekiq/active_job.rb +87 -0
- data/lib/sidekiq/api.rb +352 -0
- data/lib/sidekiq/batch.rb +175 -0
- data/lib/sidekiq/capsule.rb +38 -0
- data/lib/sidekiq/cli.rb +70 -0
- data/lib/sidekiq/client.rb +84 -0
- data/lib/sidekiq/config.rb +190 -0
- data/lib/sidekiq/cron/job.rb +555 -0
- data/lib/sidekiq/cron/poller.rb +47 -0
- data/lib/sidekiq/cron/schedule_loader.rb +41 -0
- data/lib/sidekiq/cron/web.rb +7 -0
- data/lib/sidekiq/cron.rb +146 -0
- data/lib/sidekiq/deploy.rb +62 -0
- data/lib/sidekiq/iterable_job.rb +55 -0
- data/lib/sidekiq/job.rb +207 -0
- data/lib/sidekiq/job_retry.rb +23 -0
- data/lib/sidekiq/limiter.rb +144 -0
- data/lib/sidekiq/logger.rb +25 -0
- data/lib/sidekiq/middleware/chain.rb +27 -0
- data/lib/sidekiq/middleware/current_attributes.rb +19 -0
- data/lib/sidekiq/middleware/modules.rb +15 -0
- data/lib/sidekiq/middleware/server/statsd.rb +3 -0
- data/lib/sidekiq/periodic.rb +94 -0
- data/lib/sidekiq/pro/metrics.rb +134 -0
- data/lib/sidekiq/pro/web.rb +5 -0
- data/lib/sidekiq/rails.rb +18 -0
- data/lib/sidekiq/redis_connection.rb +166 -0
- data/lib/sidekiq/scheduler.rb +4 -0
- data/lib/sidekiq/testing/inline.rb +5 -0
- data/lib/sidekiq/testing.rb +188 -0
- data/lib/sidekiq/web.rb +112 -0
- data/lib/sidekiq-cron.rb +4 -0
- data/lib/sidekiq-ent/periodic/testing.rb +4 -0
- data/lib/sidekiq-ent/web.rb +5 -0
- data/lib/sidekiq-ent.rb +90 -0
- data/lib/sidekiq-pro.rb +16 -0
- data/lib/sidekiq-scheduler/config.rb +65 -0
- data/lib/sidekiq-scheduler/manager.rb +34 -0
- data/lib/sidekiq-scheduler/poller.rb +46 -0
- data/lib/sidekiq-scheduler/redis_manager.rb +108 -0
- data/lib/sidekiq-scheduler/schedule.rb +65 -0
- data/lib/sidekiq-scheduler/scheduled_job.rb +80 -0
- data/lib/sidekiq-scheduler/scheduler.rb +218 -0
- data/lib/sidekiq-scheduler/utils.rb +120 -0
- data/lib/sidekiq-scheduler/version.rb +6 -0
- data/lib/sidekiq-scheduler/web.rb +7 -0
- data/lib/sidekiq-scheduler.rb +48 -0
- data/lib/sidekiq.rb +222 -0
- metadata +121 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "sidekiq/api"
|
|
5
|
+
|
|
6
|
+
module SidekiqScheduler
|
|
7
|
+
# The scheduler: loads Sidekiq.schedule (from Redis, when dynamic) and
|
|
8
|
+
# pushes each job as it comes due. Only Robin's leader pushes, and each
|
|
9
|
+
# push is claimed in Redis (RedisManager.register_job_instance), so
|
|
10
|
+
# sidekiq-scheduler processes and Robin's never push a cron tick twice.
|
|
11
|
+
#
|
|
12
|
+
# The class forwards its instance's methods to Scheduler.instance, as
|
|
13
|
+
# sidekiq-scheduler's does (Sidekiq::Scheduler.reload_schedule!).
|
|
14
|
+
class Scheduler
|
|
15
|
+
attr_accessor :enabled, :dynamic, :dynamic_every, :listened_queues_only, :rufus_scheduler_options
|
|
16
|
+
|
|
17
|
+
# The jobs scheduled, by name (ScheduledJob each).
|
|
18
|
+
attr_reader :scheduled_jobs
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
attr_writer :instance
|
|
22
|
+
|
|
23
|
+
def instance = @instance ||= new
|
|
24
|
+
|
|
25
|
+
def method_missing(name, ...)
|
|
26
|
+
method_defined?(name) ? instance.public_send(name, ...) : super
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def respond_to_missing?(name, *) = method_defined?(name)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def initialize(config = Config.new(without_defaults: true))
|
|
33
|
+
@scheduler_config = config
|
|
34
|
+
self.enabled = config.enabled?
|
|
35
|
+
self.dynamic = config.dynamic?
|
|
36
|
+
self.dynamic_every = config.dynamic_every?
|
|
37
|
+
self.listened_queues_only = config.listened_queues_only?
|
|
38
|
+
self.rufus_scheduler_options = config.rufus_scheduler_options || {}
|
|
39
|
+
@scheduled_jobs = {}
|
|
40
|
+
@tried = {}
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def print_schedule
|
|
44
|
+
logger.info "Scheduling Info\tLast Run"
|
|
45
|
+
scheduled_jobs.each_value { |job| logger.info "#{job.schedule}\t#{RedisManager.get_job_last_time(job.name)}\t" }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Schedules Sidekiq.schedule's jobs (reloading it first when dynamic)
|
|
49
|
+
# and starts pushing them as they come due.
|
|
50
|
+
def load_schedule!
|
|
51
|
+
return logger.info("SidekiqScheduler is disabled") unless enabled
|
|
52
|
+
|
|
53
|
+
logger.info "Loading Schedule"
|
|
54
|
+
if dynamic
|
|
55
|
+
Sidekiq.reload_schedule!
|
|
56
|
+
@current_changed_score = Time.now.to_f
|
|
57
|
+
end
|
|
58
|
+
schedule = Sidekiq.schedule.to_h
|
|
59
|
+
logger.info "Schedule empty! Set Sidekiq.schedule" if schedule.empty?
|
|
60
|
+
@scheduled_jobs = {}
|
|
61
|
+
schedule.each { |name, config| load_schedule_job(name, config) }
|
|
62
|
+
logger.info "Schedules Loaded"
|
|
63
|
+
@poller ||= Poller.new(self, Robin.config).start
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Schedules one job, unless its rails_env (a comma-separated list)
|
|
67
|
+
# leaves out RAILS_ENV.
|
|
68
|
+
def load_schedule_job(name, config)
|
|
69
|
+
return unless config["rails_env"].nil? || rails_env_matches?(config)
|
|
70
|
+
|
|
71
|
+
logger.info "Scheduling #{name} #{config}"
|
|
72
|
+
job = ScheduledJob.build(name, config)
|
|
73
|
+
return logger.info("no #{ScheduledJob::TYPES.join(" / ")} found for #{config["class"]} (#{name}) - skipping") unless job
|
|
74
|
+
|
|
75
|
+
@scheduled_jobs[name] = job
|
|
76
|
+
Utils.update_job_next_time(name, job.next_time)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Pushes the job for +time+, unless it was pushed for it already.
|
|
80
|
+
def idempotent_job_enqueue(job_name, time, config)
|
|
81
|
+
if RedisManager.register_job_instance(job_name, time)
|
|
82
|
+
logger.info "queueing #{config.fetch("class")} (#{job_name})"
|
|
83
|
+
handle_errors { enqueue_job(config, time) }
|
|
84
|
+
RedisManager.remove_elder_job_instances(job_name)
|
|
85
|
+
else
|
|
86
|
+
logger.debug { "Ignoring #{job_name} job as it has been already enqueued" }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Pushes a job from its schedule: with Active Job for Active Job
|
|
91
|
+
# classes, else Sidekiq's client. With include_metadata, its arguments
|
|
92
|
+
# end with {"scheduled_at" => +time+ (epoch seconds)}.
|
|
93
|
+
def enqueue_job(job_config, time = Time.now)
|
|
94
|
+
config = job_config.dup
|
|
95
|
+
config["class"] = Utils.try_to_constantize(config.fetch("class"))
|
|
96
|
+
args = config["args"]
|
|
97
|
+
if !args.instance_of?(Hash)
|
|
98
|
+
args = Array(args)
|
|
99
|
+
elsif Sidekiq.compatible_before?("7.3") && args.respond_to?(:symbolize_keys)
|
|
100
|
+
# sidekiq-scheduler 5 (for Sidekiq 7.2 and earlier) symbolizes a
|
|
101
|
+
# Hash's keys, with Active Support; 6 doesn't.
|
|
102
|
+
args = args.symbolize_keys
|
|
103
|
+
end
|
|
104
|
+
args = [*(args.instance_of?(Array) ? args : [args]), {"scheduled_at" => time.to_f.round(3)}] if config.delete("include_metadata")
|
|
105
|
+
config["args"] = args
|
|
106
|
+
Utils.active_job_enqueue?(config.fetch("class")) ? Utils.enqueue_with_active_job(config) : Utils.enqueue_with_sidekiq(config)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Stops pushing, and unschedules every job.
|
|
110
|
+
def clear_schedule!(*) # sidekiq-scheduler's stop option, unused
|
|
111
|
+
@poller&.terminate
|
|
112
|
+
@poller = nil
|
|
113
|
+
@scheduled_jobs = {}
|
|
114
|
+
@tried = {}
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def reload_schedule!
|
|
118
|
+
return logger.info("SidekiqScheduler is disabled") unless enabled
|
|
119
|
+
|
|
120
|
+
logger.info "Reloading Schedule"
|
|
121
|
+
clear_schedule!
|
|
122
|
+
load_schedule!
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Reschedules the jobs whose schedules changed in Redis since the last
|
|
126
|
+
# look (for dynamic schedules).
|
|
127
|
+
def update_schedule
|
|
128
|
+
since, @current_changed_score = @current_changed_score, Time.now.to_f
|
|
129
|
+
changes = RedisManager.get_schedule_changes(since, @current_changed_score)
|
|
130
|
+
return if changes.empty?
|
|
131
|
+
|
|
132
|
+
logger.info "Updating schedule"
|
|
133
|
+
Sidekiq.reload_schedule!
|
|
134
|
+
changes.each do |name|
|
|
135
|
+
@scheduled_jobs.delete(name)
|
|
136
|
+
@tried.delete(name)
|
|
137
|
+
config = Sidekiq.schedule[name]
|
|
138
|
+
load_schedule_job(name, config) if config
|
|
139
|
+
end
|
|
140
|
+
logger.info "Schedule updated"
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Whether a job in the schedule is enabled: its state in Redis (the
|
|
144
|
+
# dashboard's toggle), else its "enabled" (true by default). nil when
|
|
145
|
+
# it isn't in the schedule.
|
|
146
|
+
def job_enabled?(name)
|
|
147
|
+
config = Sidekiq.schedule.to_h[name]
|
|
148
|
+
schedule_state(name).fetch("enabled", config.fetch("enabled", true)) if config
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def toggle_job_enabled(name)
|
|
152
|
+
set_schedule_state(name, schedule_state(name).merge({"enabled" => !job_enabled?(name)}))
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def toggle_all_jobs(new_state)
|
|
156
|
+
Sidekiq.schedule!.each_key { |name| set_schedule_state(name, schedule_state(name).merge({"enabled" => new_state})) }
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Pushes the jobs due at +now+ (each once for each of its times), as
|
|
160
|
+
# Robin's leader does: Poller calls this.
|
|
161
|
+
def enqueue_due(now = Time.now)
|
|
162
|
+
queues = listened_queues if listened_queues_only
|
|
163
|
+
@scheduled_jobs.each_value do |job|
|
|
164
|
+
time = job.due(now)
|
|
165
|
+
next if @tried[job.name] == time # nil (not due) or tried already
|
|
166
|
+
|
|
167
|
+
@tried[job.name] = time
|
|
168
|
+
next unless queues.nil? || queues.empty? || queues.include?(job.config["queue"].to_s)
|
|
169
|
+
|
|
170
|
+
idempotent_job_enqueue(job.name, time, job.config) if job_enabled?(job.name)
|
|
171
|
+
Utils.update_job_last_time(job.name, time)
|
|
172
|
+
Utils.update_job_next_time(job.name, job.next_time(now))
|
|
173
|
+
rescue => e
|
|
174
|
+
Robin.config.handle_exception(e, {context: "sidekiq-scheduler job #{job.name}"})
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# How long Poller can wait from +now+: until the next job is due, at
|
|
179
|
+
# most a second.
|
|
180
|
+
def seconds_until_next(now = Time.now)
|
|
181
|
+
soonest = @scheduled_jobs.each_value.filter_map { |job| job.next_time(now) }.min
|
|
182
|
+
soonest ? (soonest - now).clamp(0.05, 1.0) : 1.0
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def to_hash = {scheduler_config: @scheduler_config.to_hash}
|
|
186
|
+
|
|
187
|
+
def inspect
|
|
188
|
+
"#<SidekiqScheduler::Scheduler enabled=#{enabled} dynamic=#{dynamic} dynamic_every=#{dynamic_every} " \
|
|
189
|
+
"listened_queues_only=#{listened_queues_only} rufus_scheduler_options=#{rufus_scheduler_options}>"
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
private
|
|
193
|
+
|
|
194
|
+
def logger = Sidekiq.logger
|
|
195
|
+
|
|
196
|
+
def schedule_state(name)
|
|
197
|
+
state = RedisManager.get_job_state(name)
|
|
198
|
+
state ? JSON.parse(state) : {}
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def set_schedule_state(name, state) = RedisManager.set_job_state(name, state)
|
|
202
|
+
|
|
203
|
+
def rails_env_matches?(config) = config.fetch("rails_env").delete(" \t\n").split(",").include?(ENV["RAILS_ENV"])
|
|
204
|
+
|
|
205
|
+
# The queues the live processes take jobs from: the jobs whose queue
|
|
206
|
+
# none of them does aren't pushed (listened_queues_only).
|
|
207
|
+
def listened_queues
|
|
208
|
+
Sidekiq::ProcessSet.new.flat_map(&:queues)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Errors pushing a job are logged, as sidekiq-scheduler does.
|
|
212
|
+
def handle_errors
|
|
213
|
+
yield
|
|
214
|
+
rescue => e
|
|
215
|
+
logger.info "#{e.class}: #{e}"
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "robin/compat/cron_line"
|
|
4
|
+
|
|
5
|
+
module SidekiqScheduler
|
|
6
|
+
# sidekiq-scheduler's helpers.
|
|
7
|
+
module Utils
|
|
8
|
+
# A schedule's keys that say when it runs (or describe it), rather than
|
|
9
|
+
# what job it pushes.
|
|
10
|
+
RUFUS_METADATA_KEYS = %w[description at cron every in interval enabled].freeze
|
|
11
|
+
|
|
12
|
+
# Compact durations: "45m", "1h30m", "2d", "1w".
|
|
13
|
+
DURATION = /\A(?:\d+[wdhms])+\z/
|
|
14
|
+
DURATION_UNITS = {"w" => 604_800, "d" => 86_400, "h" => 3_600, "m" => 60, "s" => 1}.freeze
|
|
15
|
+
private_constant :DURATION, :DURATION_UNITS
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
# +object+ with its Hashes' keys (inside Arrays and Sets too) Strings.
|
|
19
|
+
def stringify_keys(object) = deep_keys(object, &:to_s)
|
|
20
|
+
|
|
21
|
+
# +object+ with its Hashes' keys (inside Arrays and Sets too) Symbols.
|
|
22
|
+
def symbolize_keys(object) = deep_keys(object, &:to_sym)
|
|
23
|
+
|
|
24
|
+
# The class a String names, or +klass+ itself (when it isn't a
|
|
25
|
+
# String, or no such class is loaded).
|
|
26
|
+
def try_to_constantize(klass)
|
|
27
|
+
klass.instance_of?(String) ? Object.const_get(klass) : klass
|
|
28
|
+
rescue NameError
|
|
29
|
+
klass
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# An instance of the Active Job class +klass+, with +args+ (an
|
|
33
|
+
# Array of arguments, or a Hash of keyword arguments with
|
|
34
|
+
# +keyword_argument+, else one argument).
|
|
35
|
+
def initialize_active_job(klass, args, keyword_argument = false)
|
|
36
|
+
if args.instance_of?(Array)
|
|
37
|
+
klass.new(*args)
|
|
38
|
+
elsif args.instance_of?(Hash) && keyword_argument
|
|
39
|
+
klass.new(**symbolize_keys(args))
|
|
40
|
+
else
|
|
41
|
+
klass.new(args)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Whether +klass+ is an Active Job class (pushed with Active Job).
|
|
46
|
+
def active_job_enqueue?(klass)
|
|
47
|
+
base = active_job_base
|
|
48
|
+
(klass.instance_of?(Class) && base && klass < base) || false
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def enqueue_with_sidekiq(config) = Sidekiq::Client.push(sanitize_job_config(config))
|
|
52
|
+
|
|
53
|
+
def enqueue_with_active_job(config)
|
|
54
|
+
# Active Job keeps the job's own queue for queue: nil.
|
|
55
|
+
initialize_active_job(config.fetch("class"), config["args"], config["keyword_argument"]).enqueue(queue: config["queue"])
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def sanitize_job_config(config) = config.except(*RUFUS_METADATA_KEYS)
|
|
59
|
+
|
|
60
|
+
# Saves when a job runs next, or forgets it (nil).
|
|
61
|
+
def update_job_next_time(name, next_time)
|
|
62
|
+
if next_time
|
|
63
|
+
RedisManager.set_job_next_time(name, format_time(next_time))
|
|
64
|
+
else
|
|
65
|
+
RedisManager.remove_job_next_time(name)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def update_job_last_time(name, last_time)
|
|
70
|
+
RedisManager.set_job_last_time(name, format_time(last_time)) if last_time
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The queue a Sidekiq job class (by name or itself) sets, for a
|
|
74
|
+
# schedule without one. nil for Active Job classes, which pick
|
|
75
|
+
# theirs as they're enqueued.
|
|
76
|
+
def sidekiq_queue(klass)
|
|
77
|
+
klass = try_to_constantize(klass)
|
|
78
|
+
klass.sidekiq_options["queue"] if klass.respond_to?(:sidekiq_options) && !active_job_enqueue?(klass)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# A duration as seconds: a number, "45m", "1h30m", or (with the fugit
|
|
82
|
+
# gem) "2 hours and 30 minutes".
|
|
83
|
+
def duration(value)
|
|
84
|
+
return value if value.is_a?(Numeric)
|
|
85
|
+
|
|
86
|
+
text = value.to_s.strip
|
|
87
|
+
return Integer(text, 10) if text.match?(/\A\d+\z/)
|
|
88
|
+
return text.scan(/(\d+)(\w)/).sum { |count, unit| Integer(count, 10) * DURATION_UNITS.fetch(unit) } if DURATION.match?(text)
|
|
89
|
+
|
|
90
|
+
fugit = Robin::Compat::CronLine.fugit
|
|
91
|
+
raise ArgumentError, "#{text.inspect} isn't a duration like \"45m\" or \"1h30m\" (others need the fugit gem)" unless fugit
|
|
92
|
+
|
|
93
|
+
parsed = fugit::Duration.parse(text)
|
|
94
|
+
raise ArgumentError, "#{text.inspect} isn't a duration" unless parsed
|
|
95
|
+
|
|
96
|
+
parsed.to_sec
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
def deep_keys(object, &)
|
|
102
|
+
case object
|
|
103
|
+
when Hash then object.to_h { |key, value| [yield(key), deep_keys(value, &)] }
|
|
104
|
+
when Array, Set then object.map { |value| deep_keys(value, &) }
|
|
105
|
+
else object
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# As rufus-scheduler (et-orbi) writes times, in the system's zone.
|
|
110
|
+
# mutant:disable -- a Time's to_s is this format, and Robin's Redis
|
|
111
|
+
# client writes a Time as its to_s
|
|
112
|
+
def format_time(time) = time.getlocal.strftime("%Y-%m-%d %H:%M:%S %z")
|
|
113
|
+
|
|
114
|
+
# ActiveJob::Base, or nil without Active Job.
|
|
115
|
+
# mutant:disable -- Active Job is loaded for every test at once under
|
|
116
|
+
# mutant (the tests stub this to cover both)
|
|
117
|
+
def active_job_base = (defined?(::ActiveJob::Base) && ::ActiveJob::Base) || nil
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sidekiq-scheduler"
|
|
4
|
+
|
|
5
|
+
# sidekiq-scheduler's Web UI tab: Robin's dashboard has no page for these
|
|
6
|
+
# jobs (Sidekiq.get_schedule lists them).
|
|
7
|
+
Robin.logger.warn("sidekiq-scheduler/web: Robin's dashboard doesn't show sidekiq-scheduler jobs; Sidekiq.get_schedule lists them")
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sidekiq"
|
|
4
|
+
require "sidekiq-scheduler/version"
|
|
5
|
+
require "sidekiq-scheduler/redis_manager"
|
|
6
|
+
require "sidekiq-scheduler/utils"
|
|
7
|
+
require "sidekiq-scheduler/config"
|
|
8
|
+
require "sidekiq-scheduler/schedule"
|
|
9
|
+
require "sidekiq-scheduler/scheduled_job"
|
|
10
|
+
require "sidekiq-scheduler/scheduler"
|
|
11
|
+
require "sidekiq-scheduler/poller"
|
|
12
|
+
require "sidekiq-scheduler/manager"
|
|
13
|
+
|
|
14
|
+
# sidekiq-scheduler's interface, on Robin: the :scheduler: section of
|
|
15
|
+
# config/sidekiq.yml, Sidekiq.schedule= and friends, and
|
|
16
|
+
# Sidekiq::Scheduler.
|
|
17
|
+
#
|
|
18
|
+
# # config/sidekiq.yml
|
|
19
|
+
# :scheduler:
|
|
20
|
+
# :schedule:
|
|
21
|
+
# hello_world:
|
|
22
|
+
# cron: "0 * * * * *"
|
|
23
|
+
# class: HelloWorld
|
|
24
|
+
#
|
|
25
|
+
# The schedule is kept in Redis as sidekiq-scheduler keeps it. Only Robin's
|
|
26
|
+
# leader pushes the jobs, and it claims each cron tick in Redis the way
|
|
27
|
+
# sidekiq-scheduler does, so the two can run side by side while you switch.
|
|
28
|
+
module SidekiqScheduler
|
|
29
|
+
class << self
|
|
30
|
+
# What a server does as it starts: schedules the jobs and pushes them
|
|
31
|
+
# as they come due, until it quiets or stops. +sidekiq_config+ is the
|
|
32
|
+
# server's Sidekiq::Config.
|
|
33
|
+
def boot(sidekiq_config)
|
|
34
|
+
RedisManager.clean_schedules_changed
|
|
35
|
+
manager = Manager.new(Config.new(sidekiq_config: sidekiq_config))
|
|
36
|
+
sidekiq_config[:schedule_manager] = manager
|
|
37
|
+
manager.start
|
|
38
|
+
sidekiq_config.on(:quiet) { manager.stop }
|
|
39
|
+
sidekiq_config.on(:shutdown) { manager.stop }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
module Sidekiq
|
|
45
|
+
Scheduler = SidekiqScheduler::Scheduler
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
Sidekiq.extend SidekiqScheduler::Schedule
|
data/lib/sidekiq.rb
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "robin"
|
|
4
|
+
require "robin/compat/version"
|
|
5
|
+
|
|
6
|
+
# Sidekiq's interface, on top of Robin. Apps keep their Sidekiq code (job
|
|
7
|
+
# classes, configuration, tests, API calls) and run on Robin, which works
|
|
8
|
+
# on Sidekiq's Redis data where it is: no prefix on the keys a job's life
|
|
9
|
+
# runs through, the same job fields and units, the same process records.
|
|
10
|
+
module Sidekiq
|
|
11
|
+
# The Sidekiq version whose interface this matches.
|
|
12
|
+
VERSION = "8.1.0"
|
|
13
|
+
NAME = "Sidekiq"
|
|
14
|
+
|
|
15
|
+
Shutdown = Robin::Shutdown
|
|
16
|
+
|
|
17
|
+
# The Sidekiq versions Sidekiq.compatible_with takes.
|
|
18
|
+
COMPATIBLE = Gem::Requirement.new(">= 7.0", "< 8.2")
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
# The Sidekiq version the app was written for, which decides where
|
|
22
|
+
# Sidekiq's versions disagree: "8.1" unless set (to "7.0", say, or
|
|
23
|
+
# "7.2.4"). Before 8.0, a job records its times in epoch seconds
|
|
24
|
+
# (Floats) and an Active Job names Sidekiq 7's wrapper class, so
|
|
25
|
+
# Sidekiq 7 processes sharing the Redis can run Robin's jobs, and Pro's
|
|
26
|
+
# StatsD metrics have no "sidekiq." prefix. Before 7.3, a Hash of
|
|
27
|
+
# arguments in a sidekiq-scheduler schedule has its keys symbolized (as
|
|
28
|
+
# sidekiq-scheduler 5 does, with Active Support), and before 7.1,
|
|
29
|
+
# push_bulk leaves out the jobs middleware stopped.
|
|
30
|
+
def compatible_with = @compatible_with&.to_s || "8.1"
|
|
31
|
+
|
|
32
|
+
def compatible_with=(version)
|
|
33
|
+
parsed = Gem::Version.new(version) if Gem::Version.correct?(version)
|
|
34
|
+
raise ArgumentError, "robin-compat follows Sidekiq 7.0 to 8.1, not #{version.inspect}" unless parsed && COMPATIBLE.satisfied_by?(parsed)
|
|
35
|
+
|
|
36
|
+
@compatible_with = parsed
|
|
37
|
+
Robin.config.job_timestamps = compatible_before?("8.0") ? :seconds : :milliseconds
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Whether the app was written for a Sidekiq older than +version+.
|
|
41
|
+
def compatible_before?(version) = @compatible_with ? @compatible_with < version : false
|
|
42
|
+
|
|
43
|
+
def server? = Robin.server?
|
|
44
|
+
|
|
45
|
+
def pro? = true
|
|
46
|
+
|
|
47
|
+
def ent? = true
|
|
48
|
+
|
|
49
|
+
# The configuration Sidekiq.configure_* yield (Sidekiq::Config).
|
|
50
|
+
def default_configuration = @config ||= Config.new(Robin.config)
|
|
51
|
+
|
|
52
|
+
# Runs the block with the configuration in a server process. Blocks are
|
|
53
|
+
# kept, so an embedded server (configure_embed) runs them too.
|
|
54
|
+
def configure_server(&block)
|
|
55
|
+
server_blocks << block
|
|
56
|
+
block.call(default_configuration) if server?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def configure_client
|
|
60
|
+
yield default_configuration unless server?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Runs a server inside this process; see Robin.configure_embed. The
|
|
64
|
+
# configure_server blocks run first, then this one.
|
|
65
|
+
def configure_embed(&block)
|
|
66
|
+
Robin.configure_embed do |robin|
|
|
67
|
+
config = Config.new(robin)
|
|
68
|
+
config.concurrency = 2
|
|
69
|
+
server_blocks.each { |b| b.call(config) }
|
|
70
|
+
block&.call(config)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def server_blocks = @server_blocks ||= []
|
|
75
|
+
|
|
76
|
+
# Yields a connection that speaks the redis-client interface
|
|
77
|
+
# (Sidekiq::RedisConnection::Connection).
|
|
78
|
+
def redis(&) = default_configuration.redis(&)
|
|
79
|
+
|
|
80
|
+
def redis_pool = default_configuration.redis_pool
|
|
81
|
+
|
|
82
|
+
def redis_info = default_configuration.redis_info
|
|
83
|
+
|
|
84
|
+
def logger = Robin.logger
|
|
85
|
+
|
|
86
|
+
def logger=(logger)
|
|
87
|
+
default_configuration.logger = logger
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def dump_json(object) = Robin.dump_json(object)
|
|
91
|
+
|
|
92
|
+
def load_json(string) = Robin.load_json(string)
|
|
93
|
+
|
|
94
|
+
def strict_args!(mode = :raise)
|
|
95
|
+
Robin.config.strict_args = mode
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Options every job class starts from (sidekiq_options adds to them).
|
|
99
|
+
def default_job_options = Job.default_options
|
|
100
|
+
|
|
101
|
+
def default_job_options=(options)
|
|
102
|
+
Job.default_options = options
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Holds pushes made inside an Active Record transaction until it
|
|
106
|
+
# commits (see Robin::Rails::TransactionalPush).
|
|
107
|
+
def transactional_push!
|
|
108
|
+
require "robin/rails/transactional_push"
|
|
109
|
+
Robin.config.push_deferrer = Robin::Rails::TransactionalPush
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Sidekiq 8's testing switch: :fake, :inline or :disable.
|
|
113
|
+
def testing!(mode = :fake, &)
|
|
114
|
+
raise "Unknown testing mode: #{mode}" unless %i[fake inline disable].include?(mode.to_sym)
|
|
115
|
+
|
|
116
|
+
require "sidekiq/testing"
|
|
117
|
+
Testing.public_send(:"#{mode}!", &)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def freeze! = default_configuration
|
|
121
|
+
|
|
122
|
+
def loader
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
require "sidekiq/redis_connection"
|
|
128
|
+
require "sidekiq/config"
|
|
129
|
+
require "sidekiq/capsule"
|
|
130
|
+
require "sidekiq/middleware/chain"
|
|
131
|
+
require "sidekiq/middleware/modules"
|
|
132
|
+
require "sidekiq/job"
|
|
133
|
+
require "sidekiq/client"
|
|
134
|
+
require "sidekiq/logger"
|
|
135
|
+
require "sidekiq/job_retry"
|
|
136
|
+
require "sidekiq/iterable_job"
|
|
137
|
+
|
|
138
|
+
module Sidekiq
|
|
139
|
+
# sidekiq-cron's interface (see sidekiq/cron.rb), loaded when it's used.
|
|
140
|
+
autoload :Cron, "sidekiq/cron"
|
|
141
|
+
|
|
142
|
+
# A server runs sidekiq-cron's jobs (Cron.boot) if the app uses
|
|
143
|
+
# Sidekiq::Cron, has its schedule file or has its jobs in Redis.
|
|
144
|
+
CRON_START = -> { Cron.boot(Robin.config) if cron? }
|
|
145
|
+
|
|
146
|
+
# mutant:disable -- whether Sidekiq::Cron is loaded yet, which it always
|
|
147
|
+
# is once the tests have loaded it
|
|
148
|
+
def self.cron? = !autoload?(:Cron) || cron_found?
|
|
149
|
+
|
|
150
|
+
# Whether sidekiq-cron's schedule file or jobs (in its default
|
|
151
|
+
# namespace, or from before namespaces) are there.
|
|
152
|
+
def self.cron_found?
|
|
153
|
+
return true if %w[config/schedule.yml config/schedule.yaml].any? { |file| File.exist?(file) }
|
|
154
|
+
|
|
155
|
+
prefix = Robin.config.key_prefix
|
|
156
|
+
redis { |c| c.call("EXISTS", "#{prefix}cron_jobs:default", "#{prefix}cron_jobs") }.positive?
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# sidekiq-scheduler's interface (see sidekiq-scheduler.rb), loaded when
|
|
160
|
+
# it's used: Sidekiq::Scheduler, SidekiqScheduler, or these methods.
|
|
161
|
+
autoload :Scheduler, "sidekiq/scheduler"
|
|
162
|
+
::Object.autoload :SidekiqScheduler, "sidekiq-scheduler"
|
|
163
|
+
|
|
164
|
+
# Sidekiq.schedule= and the rest, until sidekiq-scheduler.rb (loaded by
|
|
165
|
+
# the first call) gives Sidekiq the real ones.
|
|
166
|
+
module ScheduleLoader
|
|
167
|
+
%i[schedule schedule= schedule! reload_schedule! get_schedule get_all_schedules set_schedule remove_schedule].each do |name|
|
|
168
|
+
define_method(name) do |*args|
|
|
169
|
+
require "sidekiq-scheduler"
|
|
170
|
+
public_send(name, *args)
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
private_constant :ScheduleLoader
|
|
175
|
+
extend ScheduleLoader
|
|
176
|
+
|
|
177
|
+
# A server runs sidekiq-scheduler's jobs (SidekiqScheduler.boot) if the
|
|
178
|
+
# app uses its interface or config/sidekiq.yml has a :scheduler: section.
|
|
179
|
+
SCHEDULER_START = -> { SidekiqScheduler.boot(default_configuration) if scheduler? }
|
|
180
|
+
|
|
181
|
+
# mutant:disable -- whether SidekiqScheduler is loaded yet, which it
|
|
182
|
+
# always is once the tests have loaded it
|
|
183
|
+
def self.scheduler? = !::Object.autoload?(:SidekiqScheduler) || scheduler_configured?
|
|
184
|
+
|
|
185
|
+
def self.scheduler_configured? = !default_configuration[:scheduler].nil?
|
|
186
|
+
|
|
187
|
+
# Gives a Robin config Sidekiq's defaults where it still has Robin's:
|
|
188
|
+
# Sidekiq's Redis layout (the job keys unprefixed, Robin's own under
|
|
189
|
+
# "robin:"), its 25 retries, and its backoff: count**4 + 15 seconds (or
|
|
190
|
+
# what sidekiq_retry_in says) plus up to 10 * (count + 1) seconds of
|
|
191
|
+
# jitter, where the count is 0 after the first failure. Worker threads
|
|
192
|
+
# get Thread.current[:sidekiq_capsule], and servers run sidekiq-cron's
|
|
193
|
+
# and sidekiq-scheduler's jobs (see CRON_START and SCHEDULER_START).
|
|
194
|
+
def self.defaults!(config)
|
|
195
|
+
config.event_handlers << CAPSULE_TRACKER unless config.event_handlers.include?(CAPSULE_TRACKER)
|
|
196
|
+
[CRON_START, SCHEDULER_START].each do |start|
|
|
197
|
+
config.on(:startup, &start) unless config.lifecycle.fetch(:startup).include?(start)
|
|
198
|
+
end
|
|
199
|
+
if config.key_prefix == "robin:"
|
|
200
|
+
config.internal_key_prefix = "robin:"
|
|
201
|
+
config.key_prefix = ""
|
|
202
|
+
end
|
|
203
|
+
config.max_retries = 25 if config.max_retries == 20
|
|
204
|
+
config.job_timestamps = :seconds if compatible_before?("8.0")
|
|
205
|
+
config.retry_backoff ||= Job::RETRY_BACKOFF
|
|
206
|
+
# With sidekiq/batch loaded, batch callbacks get Sidekiq's status.
|
|
207
|
+
if defined?(Batch::Status) && config.batch_status_class == Robin::Batch::Status
|
|
208
|
+
config.batch_status_class = Batch::Status
|
|
209
|
+
end
|
|
210
|
+
config
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
module Sidekiq
|
|
215
|
+
# Sets up Rails integration when this is a Rails app (see Sidekiq::Rails).
|
|
216
|
+
def self.integrate_rails
|
|
217
|
+
require "sidekiq/rails" if defined?(::Rails::Engine)
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
Sidekiq.defaults!(Robin.config)
|
|
222
|
+
Sidekiq.integrate_rails
|