Skip to content

Commit

Permalink
Merge pull request #128 from senny/extract_perform_method_to_run_a_job
Browse files Browse the repository at this point in the history
extract #perform from #work to run a job
  • Loading branch information
♠ ace hacker committed Feb 14, 2013
2 parents f111565 + 999bda1 commit 0de099b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
28 changes: 17 additions & 11 deletions lib/queue_classic/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,11 @@ def fork_and_work
Process.wait(@cpid)
end

# This method will lock a job & evaluate the code defined by the job.
# Also, this method will make the best attempt to delete the job
# from the queue before returning.
# This method will lock a job & process the job.
def work
if job = lock_job
QC.log_yield(:at => "work", :job => job[:id]) do
begin
call(job)
rescue => e
handle_failure(job, e)
ensure
@queue.delete(job[:id])
log(:at => "delete_job", :job => job[:id])
end
process(job)
end
end
end
Expand Down Expand Up @@ -90,6 +81,21 @@ def lock_job
job
end

# A job is processed by evaluating the target code.
# Errors are delegated to the handle_failure method.
# Also, this method will make the best attempt to delete the job
# from the queue before returning.
def process(job)
begin
call(job)
rescue => e
handle_failure(job, e)
ensure
@queue.delete(job[:id])
log(:at => "delete_job", :job => job[:id])
end
end

# Each job includes a method column. We will use ruby's eval
# to grab the ruby object from memory. We send the method to
# the object and pass the args.
Expand Down
8 changes: 1 addition & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,7 @@ worker = MyWorker.new(max_attempts: 10, listening_worker: true)
loop do
job = worker.lock_job
Thread.new do
begin
Timeout::timeout(5) {worker.call(job)}
rescue => e
handle_failure(job, e)
ensure
worker.delete(job[:id])
end
Timeout::timeout(5) { worker.process(job) }
end
end
```
Expand Down

0 comments on commit 0de099b

Please sign in to comment.