diff --git a/lib/queue_classic/worker.rb b/lib/queue_classic/worker.rb index 05eb76df..e8de364b 100644 --- a/lib/queue_classic/worker.rb +++ b/lib/queue_classic/worker.rb @@ -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 @@ -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. diff --git a/readme.md b/readme.md index 3df8c38e..de0c8ff5 100644 --- a/readme.md +++ b/readme.md @@ -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 ```