Skip to content

Obtain advisory lock before updating or deleting jobs#28

Merged
statianzo merged 1 commit into
statianzo:masterfrom
owst:obtain_lock_before_rescheduling_or_deleting
Oct 5, 2017
Merged

Obtain advisory lock before updating or deleting jobs#28
statianzo merged 1 commit into
statianzo:masterfrom
owst:obtain_lock_before_rescheduling_or_deleting

Conversation

@owst

@owst owst commented Oct 5, 2017

Copy link
Copy Markdown
Collaborator

If we have a failed Que job and press the re-run button when the job is being retried by a worker, we end up scheduling another instance of the job. Similarly, if we try to delete a previously-failed job and it is already being retried, we won't actually delete the job despite the flash telling us that is was deleted.

Both of these scenarios are confusing, and should be avoided (and a warning shown to the user).

The former occurs as when we reschedule a running job, we change the primary key of the job as it is a composite of: (queue, priority, run_at, job_id) and by rescheduling we change the run_at value. Therefore, when the original (running) job completes and deletes the row(s) matching its primary key, the newly-created row (with the different primary key) won't be deleted.

To fix both scenarios, if we try to take the advisory lock for the job before updating/deleting, we will only reschedule/delete if the job isn't already being worked. This PR adds this attempted locking and shows a flash to accurately describe to the user what has happened.

N.B. to demonstrate the issue a job such as:

class SleepJob < Que::Job
  def run
    ActiveRecord::Base.transaction do
      raise 'error first time!' if error_count.zero?

      10.downto(1) do |i|
        print "#{i}..."
        sleep 1
      end

      destroy
    end
  end
end

will error first time, if the reschedule button is pressed when it starts running, (i.e. printing 10...9...) another job will be enqueued. Similarly if the delete button is pressed when the job runs, it won't actually be deleted and will continue to be run.

@statianzo statianzo merged commit 4ae98a7 into statianzo:master Oct 5, 2017
@statianzo

Copy link
Copy Markdown
Owner

Great catch. Thanks for the contribution @owst

@arronmabrey

Copy link
Copy Markdown

@statianzo would it be possible to get a ruby gems release of this fix? (if its ready, that is 😄)

@statianzo

Copy link
Copy Markdown
Owner

Published as 0.6.0

@arronmabrey

arronmabrey commented Oct 12, 2017 via email

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants