Giter Club home page Giter Club logo

Comments (5)

bensheldon avatar bensheldon commented on September 28, 2024

hmmm. I just doublechecked and it seems like the retry_on handler will be called after the perform callbacks:

https://github.com/rails/rails/blob/4bb73233413f30fd7217bd7f08af44963f5832b1/activejob/lib/active_job/execution.rb#L51-L53

I'm not sure, though, in what order Active Job callbacks run. Could you try defining your around_perform before you include GoodJob::ActiveJobExtensions::InterruptErrors. That itself is an around_perform:

included do
around_perform do |_job, block|
raise InterruptError, "Interrupted after starting perform at '#{CurrentThread.execution_interrupted}'" if CurrentThread.execution_interrupted.present?
block.call

from good_job.

bensheldon avatar bensheldon commented on September 28, 2024

Aha, found it:

Calls the before and around callbacks in the order they were set

So if I'm understanding this correctly because these are wrapping, you'll want your own custom around_perform wrapper to be invoked before you include GoodJob::ActiveJobExtensions::InterruptErrors.

If that works, that might be an nice note to add to the Readme.

from good_job.

jgrau avatar jgrau commented on September 28, 2024

I think the reordering of callbacks does allow me to rescue the interrupt error but it also seems as if it's the second execution that is raising the exception (that I can now rescue). Does that make sense? I think I need to be able to rescue (run something) when the first execution is shutting down... 🤔

from good_job.

bensheldon avatar bensheldon commented on September 28, 2024

Oh! Yes, the InterruptError is only invoked on the 2nd execution because the first execution was interrupted (SIGKILL).

You can't call code during a SIGKILL. The best you can do is put your code in an ensure.

Unless I'm misunderstanding the circumstances under which your job is being interrupted.

from good_job.

jgrau avatar jgrau commented on September 28, 2024

Thank you! I think I have something working:

  around_perform do |_job, block|
      # slug and monitor configs are irrelevant for this example but they exist :)
      check_in_id = Sentry.capture_check_in(slug, :in_progress, monitor_config:)

      start = Sentry.utc_now.to_i
      result = :ok

      begin
        block.call
      rescue StandardError
        result = :error

        raise
      ensure
        duration = Sentry.utc_now.to_i - start

        Sentry.capture_check_in(
          slug,
          result,
          check_in_id:,
          duration:,
          monitor_config:,
        )
      end
    end

This does require setting a shutdown_timeout that is lower than the grace period or timeout of the system that runs the ruby process - eg. I believe kubernetes will have a default grace period of 30 seconds.

Rails.application.configure do
  ...
    # Wait 20 seconds for jobs to finish before shutting down. The kubernetes grace
  # period is 30 seconds so forcing a shutdown after 20 seconds will allow good_job
  # to handle the shutdown somewhat gracefully.
  config.good_job.shutdown_timeout = 20
end

I also think my attempts to reproduce production-behaviour locally was flawed: I was using foreman which has a default timeout of 5 seconds before sending SIGKILL so until I set config.good_job.shutdown_timeout = 1 my ensure block would not run.

Anyways - I believe this is working. Thanks for a great gem and your quick support :)

from good_job.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.