Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error handling for deleted tasks #293

Merged
merged 2 commits into from
Jan 11, 2021
Merged

Conversation

etiennebarrie
Copy link
Member

@etiennebarrie etiennebarrie commented Jan 8, 2021

If a Task is enqueued and is then deleted before being performed, or if a Task is interrupted and deleted, it results in a crash.

To repro:

$ bundle add sidekiq
$ $EDITOR test/dummy/config/application.rb
# add this line
# config.active_job.queue_adapter = :sidekiq

Start a server, visit http://localhost:3000/maintenance_tasks/tasks/Maintenance::UpdatePostsTask, start a run then:

$ rm test/dummy/app/tasks/maintenance/update_posts_task.rb
$ (cd test/dummy; bundle exec sidekiq)
# crash

This is fixed by the first commit.

$ git checkout task-deleted-during-run^
$ (cd test/dummy; bundle exec sidekiq)
# this should pick up the retry, otherwise clean up and redo the steps above
$ git restore -- test/dummy/app/tasks/maintenance/update_posts_task.rb
$ (cd test/dummy; bundle exec sidekiq)
# enqueue the job, once it starts, hit Ctrl-C to stop sidekiq and interrupt the task
$ rm test/dummy/app/tasks/maintenance/update_posts_task.rb
$ (cd test/dummy; bundle exec sidekiq)
# crash

This is fixed by the second commit:

$ git checkout task-deleted-during-run^
$ (cd test/dummy; bundle exec sidekiq)
# this should pick up the retry, otherwise clean up and redo the steps above

@@ -90,7 +90,7 @@ def after_perform
end

def on_error(error)
@ticker.persist
@ticker.persist if defined?(@ticker)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the variable would not be defined? Because before_perform did run?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before_perform runs but has an exception before @ticker is set, on line 57:

def before_perform
@run = arguments.first
@task = Task.named(@run.task_name).new
@run.job_id = job_id
@run.running! unless @run.stopping?
@ticker = Ticker.new(MaintenanceTasks.ticker_delay) do |ticks, duration|

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I just used @ticker&.persist but it didn't pass the test because we use verbose mode. We could also add a @ticker = nil at the beginning of before_perform too but I felt that would feel weird.

@etiennebarrie
Copy link
Member Author

Worth noting that a similar exception could happen if there's a job for a run that is then removed, except the exception would be during deserialization, but then in on_error we would fail because we access @run. This is not something that can be done without accessing a console or removing a row in the DB though.

This PR solves an issue that can happen with a simple rollback before the job starts running or while the job is still running (it will be interrupted, then would fail upon resuming).

Copy link
Contributor

@adrianna-chang-shopify adrianna-chang-shopify left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find Étienne! Changes LGTM, all was as expected on the tophat 👍

One small request, could we tweak:

# enqueued -> errored occurs when the task job fails to be enqueued.

to add

 # enqueued -> errored occurs when the task job fails to be enqueued,
or if the Task is deleted before is starts running.

@etiennebarrie etiennebarrie force-pushed the task-deleted-during-run branch from 42fd4ee to dbe3234 Compare January 8, 2021 22:10
@etiennebarrie etiennebarrie merged commit 614d1e4 into main Jan 11, 2021
@etiennebarrie etiennebarrie deleted the task-deleted-during-run branch January 11, 2021 18:48
@adrianna-chang-shopify adrianna-chang-shopify temporarily deployed to rubygems February 11, 2021 21:05 Inactive
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