Skip to content

Commit

Permalink
Make 'num' mean # of jobs, not # of iterations
Browse files Browse the repository at this point in the history
(like in pre-threading implementation)
  • Loading branch information
smudge committed Jun 26, 2024
1 parent 23c05f9 commit 2ce9b67
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/delayed/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,17 @@ def on_exit!
# Exit early if interrupted.
def work_off(num = 100)
success = Concurrent::AtomicFixnum.new(0)
failure = Concurrent::AtomicFixnum.new(0)
total = 0

num.times do
while total < num
jobs = reserve_jobs
break if jobs.empty?

total += jobs.length
pool = Concurrent::FixedThreadPool.new(jobs.length)
jobs.each do |job|
pool.post do
if run_job(job)
success.increment
else
failure.increment
end
success.increment if run_job(job)
end
end

Expand All @@ -112,7 +109,7 @@ def work_off(num = 100)
break if stop? # leave if we're exiting
end

[success, failure].map(&:value)
[success.value, total - success.value]
end

def run_thread_callbacks(job, &block)
Expand All @@ -139,13 +136,14 @@ def run(job)
job.destroy
end
job_say job, format('COMPLETED after %.4f seconds', run_time)
true # did work
end
true # did work
rescue DeserializationError => e
job_say job, "FAILED permanently with #{e.class.name}: #{e.message}", 'error'

job.error = e
failed(job)
false # work failed
rescue Exception => e # rubocop:disable Lint/RescueException
self.class.lifecycle.run_callbacks(:error, self, job) { handle_failed_job(job, e) }
false # work failed
Expand Down

0 comments on commit 2ce9b67

Please sign in to comment.