Skip to content

Commit

Permalink
Feature: Unlock jobs when INT signal is handled.
Browse files Browse the repository at this point in the history
We no longer are deleting jobs from the queue in the event
of a job exception. If this behavior is desired, it should be defined
in the Worker#handle_failure method.

In the case our worker traps an INT signal and the job is not finished
we will unlock it in the queue.
  • Loading branch information
ryandotsmith committed Jan 7, 2014
1 parent 28c8fe6 commit 7672451
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
7 changes: 7 additions & 0 deletions lib/queue_classic/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def lock
end
end

def unlock(id)
QC.log_yield(:measure => 'queue.unlock') do
s = "UPDATE #{TABLE_NAME} set locked_at = null where id = $1"
conn_adapter.execute(s, id)
end
end

def delete(id)
QC.log_yield(:measure => 'queue.delete') do
conn_adapter.execute("DELETE FROM #{TABLE_NAME} where id = $1", id)
Expand Down
21 changes: 15 additions & 6 deletions lib/queue_classic/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,26 @@ def lock_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.
# if the job is evaluated with no exceptions
# then it is deleted from the queue.
# If the job has raised an exception the responsibility of what
# to do with the job is delegated to Worker#handle_failure.
# If the job is not finished and an INT signal is traped,
# this method will unlock the job in the queue.
def process(queue, job)
finished = false
begin
call(job)
call(job).tap do
queue.delete(job[:id])
finished = true
end
rescue => e
handle_failure(job, e)
finished = true
ensure
queue.delete(job[:id])
log(:at => "delete_job", :job => job[:id])
if !finished
queue.unlock(job[:id])
end
end
end

Expand Down

0 comments on commit 7672451

Please sign in to comment.