Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 412 lines (357 sloc) 12.454 kB
c7c7af1 @meatballhat More rubocop config tweaks, adding encoding comments for 1.9
meatballhat authored
1 # vim:fileencoding=utf-8
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
2
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
3 require 'rufus/scheduler'
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
4 require_relative 'scheduler/configuration'
8aa1e67 @meatballhat Collapsing lib/resque_scheduler into lib/resque/scheduler
meatballhat authored
5 require_relative 'scheduler/locking'
6 require_relative 'scheduler/logger_builder'
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
7 require_relative 'scheduler/signal_handling'
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
8
9 module Resque
8aa1e67 @meatballhat Collapsing lib/resque_scheduler into lib/resque/scheduler
meatballhat authored
10 module Scheduler
11 autoload :Cli, 'resque/scheduler/cli'
12 autoload :Extension, 'resque/scheduler/extension'
0aba925 @meatballhat Splitting the resque extension's methods up a bit
meatballhat authored
13 autoload :Util, 'resque/scheduler/util'
b3a17bd @rafaelgaspar Autoload VERSION constant
rafaelgaspar authored
14 autoload :VERSION, 'resque/scheduler/version'
8aa1e67 @meatballhat Collapsing lib/resque_scheduler into lib/resque/scheduler
meatballhat authored
15
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
16 private
018663b @meatballhat Touchup around logging + adding `.configure` convenience method
meatballhat authored
17
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
18 extend Resque::Scheduler::Locking
19 extend Resque::Scheduler::Configuration
20 extend Resque::Scheduler::SignalHandling
0c0b0ed @denyago Tests and attr_writer for logger
denyago authored
21
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
22 public
018663b @meatballhat Touchup around logging + adding `.configure` convenience method
meatballhat authored
23
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
24 class << self
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
25 # the Rufus::Scheduler jobs that are scheduled
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
26 attr_reader :scheduled_jobs
01d0862 @carlosantoniodasilva Remove not used variable and whitespaces
carlosantoniodasilva authored
27
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
28 # Schedule all jobs and continually look for delayed jobs (never returns)
29 def run
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
30 procline 'Starting'
658e3c0 @mcspring add Resque.clean_schedules method, useful when setup scheduler first …
mcspring authored
31
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
32 # trap signals
33 register_signal_handlers
34
0460bea merge in logging fix from pull #174
Peter Tersløv Forsberg authored
35 # Quote from the resque/worker.
36 # Fix buffering so we can `rake resque:scheduler > scheduler.log` and
37 # get output from the child in there.
38 $stdout.sync = true
cb67de7 Make sure stderr syncs aswell
Peter Tersløv Forsberg authored
39 $stderr.sync = true
0460bea merge in logging fix from pull #174
Peter Tersløv Forsberg authored
40
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
41 # Load the schedule into rufus
893f13c @davidyang Allow dynamic to be set by an environment variable so the entire
davidyang authored
42 # If dynamic is set, load that schedule otherwise use normal load
43 if dynamic
44 reload_schedule!
3433427 @bvandenbos moving procline
bvandenbos authored
45 else
893f13c @davidyang Allow dynamic to be set by an environment variable so the entire
davidyang authored
46 load_schedule!
47 end
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
48
0f1bcf7 @meatballhat Making signal handling ruby 2 compatible
meatballhat authored
49 begin
50 @th = Thread.current
51
52 # Now start the scheduling part of the loop.
53 loop do
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
54 if master?
0f1bcf7 @meatballhat Making signal handling ruby 2 compatible
meatballhat authored
55 begin
56 handle_delayed_items
57 update_schedule if dynamic
58 rescue Errno::EAGAIN, Errno::ECONNRESET => e
59 log! e.message
60 end
a649ea3 @bvandenbos Adding locking to support master fail over
bvandenbos authored
61 end
0f1bcf7 @meatballhat Making signal handling ruby 2 compatible
meatballhat authored
62 poll_sleep
b79e450 @seejohnrun Allow redis to timeout gracefully while handling items
seejohnrun authored
63 end
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
64
0f1bcf7 @meatballhat Making signal handling ruby 2 compatible
meatballhat authored
65 rescue Interrupt
66 log 'Exiting'
67 end
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
68 end
69
01d0862 @carlosantoniodasilva Remove not used variable and whitespaces
carlosantoniodasilva authored
70 def print_schedule
893f13c @davidyang Allow dynamic to be set by an environment variable so the entire
davidyang authored
71 if rufus_scheduler
72 log! "Scheduling Info\tLast Run"
d24657e @bugant Update rufus-scheduler to ~> 3.0
bugant authored
73 scheduler_jobs = rufus_scheduler.jobs
1300cb6 @meatballhat Updating rubocop + regenerating .rubocop_todo.yml
meatballhat authored
74 scheduler_jobs.each do |_k, v|
893f13c @davidyang Allow dynamic to be set by an environment variable so the entire
davidyang authored
75 log! "#{v.t}\t#{v.last}\t"
76 end
77 end
78 end
79
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
80 # Pulls the schedule from Resque.schedule and loads it into the
81 # rufus scheduler instance
82 def load_schedule!
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
83 procline 'Loading Schedule'
01d0862 @carlosantoniodasilva Remove not used variable and whitespaces
carlosantoniodasilva authored
84
1514977 @bvandenbos Should pull schedule from redis before applying it during startup
bvandenbos authored
85 # Need to load the schedule from redis for the first time if dynamic
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
86 Resque.reload_schedule! if dynamic
87
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
88 log! 'Schedule empty! Set Resque.schedule' if Resque.schedule.empty?
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
89
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
90 @scheduled_jobs = {}
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
91
96e26c2 Finishing up resque:scheduler rake task
Ben VandenBos authored
92 Resque.schedule.each do |name, config|
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
93 load_schedule_job(name, config)
94 end
95 Resque.redis.del(:schedules_changed)
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
96 procline 'Schedules Loaded'
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
97 end
01d0862 @carlosantoniodasilva Remove not used variable and whitespaces
carlosantoniodasilva authored
98
69d2d0a @simonoff Add support for options for every and cron interval types
simonoff authored
99 # modify interval type value to value with options if options available
100 def optionizate_interval_value(value)
101 args = value
102 if args.is_a?(::Array)
103 return args.first if args.size > 2 || !args.last.is_a?(::Hash)
104 # symbolize keys of hash for options
d24657e @bugant Update rufus-scheduler to ~> 3.0
bugant authored
105 args[2] = args[1].reduce({}) do |m, i|
69d2d0a @simonoff Add support for options for every and cron interval types
simonoff authored
106 key, value = i
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
107 m[(key.respond_to?(:to_sym) ? key.to_sym : key) || key] = value
69d2d0a @simonoff Add support for options for every and cron interval types
simonoff authored
108 m
109 end
d24657e @bugant Update rufus-scheduler to ~> 3.0
bugant authored
110
111 args[2][:job] = true
112 args[1] = nil
69d2d0a @simonoff Add support for options for every and cron interval types
simonoff authored
113 end
114 args
115 end
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
116
8aa1e67 @meatballhat Collapsing lib/resque_scheduler into lib/resque/scheduler
meatballhat authored
117 # Loads a job schedule into the Rufus::Scheduler and stores it
118 # in @scheduled_jobs
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
119 def load_schedule_job(name, config)
01f1b1a @meatballhat Touching up the env-based config loading bits
meatballhat authored
120 # If `rails_env` or `env` is set in the config, load jobs only if they
121 # are meant to be loaded in `Resque::Scheduler.env`. If `rails_env` or
122 # `env` is missing, the job should be scheduled regardless of the value
123 # of `Resque::Scheduler.env`.
124
125 configured_env = config['rails_env'] || config['env']
126
127 if configured_env.nil? || env_matches?(configured_env)
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
128 log! "Scheduling #{name} "
129 interval_defined = false
1300cb6 @meatballhat Updating rubocop + regenerating .rubocop_todo.yml
meatballhat authored
130 interval_types = %w(cron every)
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
131 interval_types.each do |interval_type|
132 if !config[interval_type].nil? && config[interval_type].length > 0
69d2d0a @simonoff Add support for options for every and cron interval types
simonoff authored
133 args = optionizate_interval_value(config[interval_type])
d24657e @bugant Update rufus-scheduler to ~> 3.0
bugant authored
134 if args.is_a?(::String)
135 args = [args, nil, job: true]
136 end
137
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
138 job = rufus_scheduler.send(interval_type, *args) do
139 if master?
a649ea3 @bvandenbos Adding locking to support master fail over
bvandenbos authored
140 log! "queueing #{config['class']} (#{name})"
4d5f359 @meatballhat Merge remote-tracking branch 'YotpoLtd/feature-last-execution' into Y…
meatballhat authored
141 Resque.last_enqueued_at(name, Time.now.to_s)
a649ea3 @bvandenbos Adding locking to support master fail over
bvandenbos authored
142 handle_errors { enqueue_from_config(config) }
143 end
d204c39 @gravis New feature : jobs can be loaded per rails environment.
gravis authored
144 end
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
145 @scheduled_jobs[name] = job
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
146 interval_defined = true
147 break
0c210e9 Adding support for empty crons in the schedule
Ben VandenBos authored
148 end
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
149 end
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
150 unless interval_defined
1300cb6 @meatballhat Updating rubocop + regenerating .rubocop_todo.yml
meatballhat authored
151 log! "no #{interval_types.join(' / ')} found for " \
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
152 "#{config['class']} (#{name}) - skipping"
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
153 end
01f1b1a @meatballhat Touching up the env-based config loading bits
meatballhat authored
154 else
1300cb6 @meatballhat Updating rubocop + regenerating .rubocop_todo.yml
meatballhat authored
155 log "Skipping schedule of #{name} because configured " \
156 "env #{configured_env.inspect} does not match current " \
01f1b1a @meatballhat Touching up the env-based config loading bits
meatballhat authored
157 "env #{env.inspect}"
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
158 end
159 end
160
01f1b1a @meatballhat Touching up the env-based config loading bits
meatballhat authored
161 # Returns true if the given schedule config hash matches the current env
43ab859 Updating history, style changes, comments
Ben VandenBos authored
162 def rails_env_matches?(config)
1300cb6 @meatballhat Updating rubocop + regenerating .rubocop_todo.yml
meatballhat authored
163 warn '`Resque::Scheduler.rails_env_matches?` is deprecated. ' \
01f1b1a @meatballhat Touching up the env-based config loading bits
meatballhat authored
164 'Please use `Resque::Scheduler.env_matches?` instead.'
165 config['rails_env'] && env &&
166 config['rails_env'].split(/[\s,]+/).include?(env)
167 end
168
169 # Returns true if the current env is non-nil and the configured env
170 # (which is a comma-split string) includes the current env.
171 def env_matches?(configured_env)
172 env && configured_env.split(/[\s,]+/).include?(env)
43ab859 Updating history, style changes, comments
Ben VandenBos authored
173 end
174
403d4c0 Adding tests and updating readme
Ben VandenBos authored
175 # Handles queueing delayed items
c7ff729 @rcarver Allow passing a time to #handle_delayed_items in order to test future…
rcarver authored
176 # at_time - Time to start scheduling items (default: now).
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
177 def handle_delayed_items(at_time = nil)
178 timestamp = Resque.next_delayed_timestamp(at_time)
179 if timestamp
180 procline 'Processing Delayed Items'
181 until timestamp.nil?
3788f11 Add queue now functionality to delayed timestamps
David Doan authored
182 enqueue_delayed_items_for_timestamp(timestamp)
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
183 timestamp = Resque.next_delayed_timestamp(at_time)
7ac2054 Fixing backup that occurs when timestamps are close together (issue #3)
Ben VandenBos authored
184 end
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
185 end
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
186 end
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
187
3788f11 Add queue now functionality to delayed timestamps
David Doan authored
188 # Enqueues all delayed jobs for a timestamp
189 def enqueue_delayed_items_for_timestamp(timestamp)
190 item = nil
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
191 loop do
3788f11 Add queue now functionality to delayed timestamps
David Doan authored
192 handle_shutdown do
a649ea3 @bvandenbos Adding locking to support master fail over
bvandenbos authored
193 # Continually check that it is still the master
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
194 if master?
195 item = Resque.next_item_for_timestamp(timestamp)
196 if item
197 log "queuing #{item['class']} [delayed]"
198 handle_errors { enqueue_from_config(item) }
199 end
3788f11 Add queue now functionality to delayed timestamps
David Doan authored
200 end
201 end
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
202 # continue processing until there are no more ready items in this
203 # timestamp
204 break if item.nil?
205 end
3788f11 Add queue now functionality to delayed timestamps
David Doan authored
206 end
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
207
208 def handle_shutdown
209 exit if @shutdown
210 yield
211 exit if @shutdown
212 end
01d0862 @carlosantoniodasilva Remove not used variable and whitespaces
carlosantoniodasilva authored
213
4822308 @bvandenbos Cleaning up tests and some error handling
bvandenbos authored
214 def handle_errors
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
215 yield
50eedf6 @meatballhat Updating some rubocop stuff
meatballhat authored
216 rescue => e
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
217 log_error "#{e.class.name}: #{e.message}"
4822308 @bvandenbos Cleaning up tests and some error handling
bvandenbos authored
218 end
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
219
220 # Enqueues a job based on a config hash
88820f3 @bvandenbos reduce reuse recycle
bvandenbos authored
221 def enqueue_from_config(job_config)
222 args = job_config['args'] || job_config[:args]
5029aa2 Support hooks with resque >= 1.15.0
Jon Larkowski and Les Hill authored
223
88820f3 @bvandenbos reduce reuse recycle
bvandenbos authored
224 klass_name = job_config['class'] || job_config[:class]
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
225 begin
8aa1e67 @meatballhat Collapsing lib/resque_scheduler into lib/resque/scheduler
meatballhat authored
226 klass = Resque::Scheduler::Util.constantize(klass_name)
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
227 rescue NameError
228 klass = klass_name
229 end
5029aa2 Support hooks with resque >= 1.15.0
Jon Larkowski and Les Hill authored
230
a9a2dc2 @chrisk Fix that hashes in worker arguments were being converted to arrays
chrisk authored
231 params = args.is_a?(Hash) ? [args] : Array(args)
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
232 queue = job_config['queue'] ||
233 job_config[:queue] ||
234 Resque.queue_from_class(klass)
235 # Support custom job classes like those that inherit from
236 # Resque::JobWithStatus (resque-status)
237 job_klass = job_config['custom_job_class']
238 if job_klass && job_klass != 'Resque::Job'
239 # The custom job class API must offer a static "scheduled" method. If
240 # the custom job class can not be constantized (via a requeue call
241 # from the web perhaps), fall back to enqueing normally via
242 # Resque::Job.create.
e6f1bd9 @fallwith if a custom_job_class based job class can not be constantized, attemp…
fallwith authored
243 begin
8aa1e67 @meatballhat Collapsing lib/resque_scheduler into lib/resque/scheduler
meatballhat authored
244 Resque::Scheduler::Util.constantize(job_klass).scheduled(
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
245 queue, klass_name, *params
246 )
e6f1bd9 @fallwith if a custom_job_class based job class can not be constantized, attemp…
fallwith authored
247 rescue NameError
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
248 # Note that the custom job class (job_config['custom_job_class'])
249 # is the one enqueued
e6f1bd9 @fallwith if a custom_job_class based job class can not be constantized, attemp…
fallwith authored
250 Resque::Job.create(queue, job_klass, *params)
251 end
4ea2ede @mattetti add support for scheduling custom job classes like JobWithStatus
mattetti authored
252 else
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
253 # Hack to avoid havoc for people shoving stuff into queues
5029aa2 Support hooks with resque >= 1.15.0
Jon Larkowski and Les Hill authored
254 # for non-existent classes (for example: running scheduler in
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
255 # one app that schedules for another.
5029aa2 Support hooks with resque >= 1.15.0
Jon Larkowski and Les Hill authored
256 if Class === klass
8aa1e67 @meatballhat Collapsing lib/resque_scheduler into lib/resque/scheduler
meatballhat authored
257 Resque::Scheduler::Plugin.run_before_delayed_enqueue_hooks(
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
258 klass, *params
259 )
8899079 @jonhyman Allowing custom job classes to be used in Resque.enqueue_at by callin…
jonhyman authored
260
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
261 # If the class is a custom job class, call self#scheduled on it.
262 # This allows you to do things like Resque.enqueue_at(timestamp,
263 # CustomJobClass). Otherwise, pass off to Resque.
8899079 @jonhyman Allowing custom job classes to be used in Resque.enqueue_at by callin…
jonhyman authored
264 if klass.respond_to?(:scheduled)
265 klass.scheduled(queue, klass_name, *params)
266 else
267 Resque.enqueue_to(queue, klass, *params)
268 end
5029aa2 Support hooks with resque >= 1.15.0
Jon Larkowski and Les Hill authored
269 else
4822308 @bvandenbos Cleaning up tests and some error handling
bvandenbos authored
270 # This will not run the before_hooks in rescue, but will at least
271 # queue the job.
5029aa2 Support hooks with resque >= 1.15.0
Jon Larkowski and Les Hill authored
272 Resque::Job.create(queue, klass, *params)
273 end
1af865d @bvandenbos Fixing documentation error for Issues 66
bvandenbos authored
274 end
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
275 end
276
277 def rufus_scheduler
d24657e @bugant Update rufus-scheduler to ~> 3.0
bugant authored
278 @rufus_scheduler ||= Rufus::Scheduler.new
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
279 end
280
281 # Stops old rufus scheduler and creates a new one. Returns the new
282 # rufus scheduler
283 def clear_schedule!
284 rufus_scheduler.stop
285 @rufus_scheduler = nil
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
286 @scheduled_jobs = {}
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
287 rufus_scheduler
288 end
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
289
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
290 def reload_schedule!
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
291 procline 'Reloading Schedule'
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
292 clear_schedule!
293 load_schedule!
294 end
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
295
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
296 def update_schedule
297 if Resque.redis.scard(:schedules_changed) > 0
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
298 procline 'Updating schedule'
299 loop do
300 schedule_name = Resque.redis.spop(:schedules_changed)
301 break unless schedule_name
571f854 @bugant reload_schedule! returns nil with no job in queue
bugant authored
302 Resque.reload_schedule!
303 if Resque.schedule.keys.include?(schedule_name)
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
304 unschedule_job(schedule_name)
305 load_schedule_job(schedule_name, Resque.schedule[schedule_name])
306 else
307 unschedule_job(schedule_name)
308 end
309 end
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
310 procline 'Schedules Loaded'
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
311 end
312 end
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
313
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
314 def unschedule_job(name)
315 if scheduled_jobs[name]
316 log "Removing schedule #{name}"
317 scheduled_jobs[name].unschedule
36d9c87 @meatballhat Addressing a bunch of RuboCop stuff, breaking the API, etc.
meatballhat authored
318 @scheduled_jobs.delete(name)
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
319 end
320 end
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
321
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
322 # Sleeps and returns true
323 def poll_sleep
cff4e02 @csfrancis Fix unsafe Resque::Scheduler shutdown in Ruby 2.0
csfrancis authored
324 handle_shutdown do
325 begin
4198b74 @meatballhat Handling signals while "sleeping"
meatballhat authored
326 poll_sleep_loop
2d01307 @csfrancis Make things a little less racey
csfrancis authored
327 ensure
328 @sleeping = false
cff4e02 @csfrancis Fix unsafe Resque::Scheduler shutdown in Ruby 2.0
csfrancis authored
329 end
330 end
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
331 true
332 end
333
4198b74 @meatballhat Handling signals while "sleeping"
meatballhat authored
334 def poll_sleep_loop
335 @sleeping = true
336 start = Time.now
337 loop do
49500d0 @yaauie signal handling: prevent high cpu by relying on Thread#wakeup
yaauie authored
338 elapsed_sleep = (Time.now - start)
339 remaining_sleep = poll_sleep_amount - elapsed_sleep
340 break if remaining_sleep <= 0
4198b74 @meatballhat Handling signals while "sleeping"
meatballhat authored
341 begin
49500d0 @yaauie signal handling: prevent high cpu by relying on Thread#wakeup
yaauie authored
342 sleep(remaining_sleep)
4198b74 @meatballhat Handling signals while "sleeping"
meatballhat authored
343 handle_signals
344 rescue Interrupt
345 if @shutdown
346 Resque.clean_schedules
71e4c72 @yaauie locking: on shutdown, only release the master lock if it belongs to us
yaauie authored
347 release_master_lock
4198b74 @meatballhat Handling signals while "sleeping"
meatballhat authored
348 end
349 break
350 end
351 end
352 end
353
30b3331 @mcspring Merge remote-tracking branch 'upsteam/master' into resque_webui_bugs
mcspring authored
354 # Sets the shutdown flag, clean schedules and exits if sleeping
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
355 def shutdown
2d01307 @csfrancis Make things a little less racey
csfrancis authored
356 return if @shutdown
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
357 @shutdown = true
ae84c7a @csfrancis Merge branch 'fix_shutdown_when_sleeping'
csfrancis authored
358 log!('Shutting down')
2d01307 @csfrancis Make things a little less racey
csfrancis authored
359 @th.raise Interrupt if @sleeping
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
360 end
361
362 def log!(msg)
018663b @meatballhat Touchup around logging + adding `.configure` convenience method
meatballhat authored
363 logger.info { msg }
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
364 end
365
85b422c @jamonkko use logger.error when logging errors from handle_errors
jamonkko authored
366 def log_error(msg)
367 logger.error { msg }
368 end
369
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
370 def log(msg)
018663b @meatballhat Touchup around logging + adding `.configure` convenience method
meatballhat authored
371 logger.debug { msg }
f320256 Adding UI for the scheduler and enqueue_at/enqueue_in functionality
Ben VandenBos authored
372 end
7dcf9bc Cleanup
Jon Larkowski and Les Hill authored
373
58105d1 @meatballhat Making Resque::Scheduler.app_name work like other settings
meatballhat authored
374 def procline(string)
375 log! string
376 argv0 = build_procline(string)
377 log "Setting procline #{argv0.inspect}"
378 $0 = argv0
379 end
380
381 private
382
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
383 attr_writer :logger
384
385 def logger
386 @logger ||= Resque::Scheduler::LoggerBuilder.new(
49bc40b @meatballhat Breaking up some complex methods in cli, collapsing MUTE/QUIET -> QUIET
meatballhat authored
387 quiet: quiet,
3525f40 @meatballhat Extracting some of Resque::Scheduler's functionality into modules
meatballhat authored
388 verbose: verbose,
389 log_dev: logfile,
390 format: logformat
391 ).build
392 end
393
a56cd0c Add process tagging for scheduler process
Nickolas Means authored
394 def app_str
395 app_name ? "[#{app_name}]" : ''
396 end
397
0beae51 @meatballhat Including env in procline kinda like #217
meatballhat authored
398 def env_str
399 env ? "[#{env}]" : ''
400 end
401
58105d1 @meatballhat Making Resque::Scheduler.app_name work like other settings
meatballhat authored
402 def build_procline(string)
0beae51 @meatballhat Including env in procline kinda like #217
meatballhat authored
403 "#{internal_name}#{app_str}#{env_str}: #{string}"
58105d1 @meatballhat Making Resque::Scheduler.app_name work like other settings
meatballhat authored
404 end
405
406 def internal_name
8aa1e67 @meatballhat Collapsing lib/resque_scheduler into lib/resque/scheduler
meatballhat authored
407 "resque-scheduler-#{Resque::Scheduler::VERSION}"
08ff38c @bvandenbos Pulling brianjlandau/resque-scheduler.git
bvandenbos authored
408 end
e240e3d Porting resque_scheduler stuff from my resque fork
Ben VandenBos authored
409 end
410 end
e82498b @gravis fix rake:scheduler task.
gravis authored
411 end
Something went wrong with that request. Please try again.