Skip to content

Commit 6b615b8

Browse files
committed
add verbose logging
1 parent 0101b39 commit 6b615b8

1 file changed

Lines changed: 9 additions & 25 deletions

File tree

lib/delayed/worker.rb

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,33 +88,38 @@ def work_off(num = 100)
8888

8989
while total < num
9090
start = clock_time
91+
say "Attempting to reserve up to #{num - total} job(s)", 'debug'
9192
jobs = reserve_jobs
93+
say 'No jobs reserved; exiting work_off loop', 'debug' if jobs.empty?
9294
break if jobs.empty?
9395

9496
total += jobs.length
95-
pool = Concurrent::FixedThreadPool.new(thread_pool_size(jobs.length))
97+
say "Reserved #{jobs.length} job(s); dispatching batch", 'debug'
98+
pool = Concurrent::FixedThreadPool.new(jobs.length)
9699
jobs.each do |job|
100+
job_say job, 'Queued for thread execution', 'debug'
97101
pool.post do
98-
thread_started = false
99102
self.class.lifecycle.run_callbacks(:thread, self) do
100-
thread_started = true
101103
success.increment if perform(job)
102104
rescue DeserializationError => e
103105
handle_unrecoverable_error(job, e)
104106
rescue Exception => e # rubocop:disable Lint/RescueException
105107
handle_erroring_job(job, e)
106108
end
107109
rescue Exception => e # rubocop:disable Lint/RescueException
108-
handle_thread_error(job, e, thread_started)
110+
job_say job, "thread crashed with #{e.class.name}: #{e.message}", 'error'
109111
end
110112
end
111113

114+
say 'Waiting for worker threads to finish', 'debug'
112115
pool.shutdown
113116
pool.wait_for_termination
117+
say "Batch finished with #{success.value} successful job(s) out of #{total} attempted so far", 'debug'
114118

115119
break if stop? # leave if we're exiting
116120

117121
elapsed = clock_time - start
122+
say format('Batch elapsed %.4f seconds', elapsed), 'debug'
118123
interruptable_sleep(self.class.min_reserve_interval - elapsed)
119124
end
120125

@@ -217,16 +222,6 @@ def handle_unrecoverable_error(job, error)
217222
failed(job)
218223
end
219224

220-
def handle_thread_error(job, error, thread_started)
221-
phase = thread_started ? 'after perform' : 'before perform'
222-
job_say job, "thread crashed #{phase} with #{error.class.name}: #{error.message}", 'error'
223-
return if thread_started
224-
225-
handle_erroring_job(job, error)
226-
rescue Exception => inner_error # rubocop:disable Lint/RescueException
227-
job_say job, "could not record pre-perform thread crash: #{inner_error.class.name}: #{inner_error.message}", 'error'
228-
end
229-
230225
# The backend adapter may return either a list or a single job
231226
# In some backends, this can be controlled with the `max_claims` config
232227
# Either way, we map this to an array of job instances
@@ -247,17 +242,6 @@ def reload!
247242
Rails.application.reloader.reload! if defined?(Rails.application.reloader) && Rails.application.reloader.check!
248243
end
249244

250-
def thread_pool_size(job_count)
251-
return job_count unless Delayed::Job.respond_to?(:connection_pool)
252-
253-
pool_size = Delayed::Job.connection_pool.size
254-
return job_count unless pool_size
255-
256-
[job_count, [pool_size - 1, 1].max].min
257-
rescue StandardError
258-
job_count
259-
end
260-
261245
def clock_time
262246
Process.clock_gettime(Process::CLOCK_MONOTONIC)
263247
end

0 commit comments

Comments
 (0)