Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:11:15+00:00 2026-06-12T08:11:15+00:00

When a Heroku worker is restarted (either on command or as the result of

  • 0

When a Heroku worker is restarted (either on command or as the result of a deploy), Heroku sends SIGTERM to the worker process. In the case of delayed_job, the SIGTERM signal is caught and then the worker stops executing after the current job (if any) has stopped.

If the worker takes to long to finish, then Heroku will send SIGKILL. In the case of delayed_job, this leaves a locked job in the database that won’t get picked up by another worker.

I’d like to ensure that jobs eventually finish (unless there’s an error). Given that, what’s the best way to approach this?

I see two options. But I’d like to get other input:

  1. Modify delayed_job to stop working on the current job (and release the lock) when it receives a SIGTERM.
  2. Figure out a (programmatic) way to detect orphaned locked jobs and then unlock them.

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:11:16+00:00Added an answer on June 12, 2026 at 8:11 am

    TLDR:

    Put this at the top of your job method:

    begin
      term_now = false
      old_term_handler = trap 'TERM' do
        term_now = true
        old_term_handler.call
      end
    

    AND

    Make sure this is called at least once every ten seconds:

      if term_now
        puts 'told to terminate'
        return true
      end
    

    AND

    At the end of your method, put this:

    ensure
      trap 'TERM', old_term_handler
    end
    

    Explanation:

    I was having the same problem and came upon this Heroku article.

    The job contained an outer loop, so I followed the article and added a trap('TERM') and exit. However delayed_job picks that up as failed with SystemExit and marks the task as failed.

    With the SIGTERM now trapped by our trap the worker’s handler isn’t called and instead it immediately restarts the job and then gets SIGKILL a few seconds later. Back to square one.

    I tried a few alternatives to exit:

    • A return true marks the job as successful (and removes it from the queue), but suffers from the same problem if there’s another job waiting in the queue.

    • Calling exit! will successfully exit the job and the worker, but it doesn’t allow the worker to remove the job from the queue, so you still have the ‘orphaned locked jobs’ problem.

    My final solution was the one given at at the top of my answer, it comprises of three parts:

    1. Before we start the potentially long job we add a new interrupt handler for 'TERM' by doing a trap (as described in the Heroku article), and we use it to set term_now = true.

      But we must also grab the old_term_handler which the delayed job worker code set (which is returned by trap) and remember to call it.

    2. We still must ensure that we return control to Delayed:Job:Worker with sufficient time for it to clean up and shutdown, so we should check term_now at least (just under) every ten seconds and return if it is true.

      You can either return true or return false depending on whether you want the job to be considered successful or not.

    3. Finally it is vital to remember to remove your handler and install back the Delayed:Job:Worker one when you have finished. If you fail to do this you will keep a dangling reference to the one we added, which can result in a memory leak if you add another one on top of that (for example, when the worker starts this job again).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm designing a daemon (specifically a worker process for Heroku) and the code I
Let assume that I have scaled Heroku application to 1 worker process, but how
If I connect to a Heroku worker dyno with heroku run (e.g. heroku run
I am trying to deploy this app https://bitbucket.org/hybird/creme_crm-1.2 on Heroku. I followed the guide
Seems like everything worked fine when I pushed to Heroku as shown below. Then
I'd like to know how to communicate between processes on a Heroku worker dyno.
When asked by the admin, my Heroku worker generates a report, compressed as a
We're working on a Rails project on Heroku that needs to scrape and process
I am using delayed_job to handle my background jobs with heroku. Occasionally I will
Rails 3.1 I have a heroku app made with the following command heroku create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.