-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
GoodJob.current_thread_running?
and `GoodJob.current_thread_shu…
…tting_down?` for graceful shutdowns
- Loading branch information
1 parent
ab51290
commit 4ad1805
Showing
5 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module GoodJob | ||
# Provides methods for determining the status of the | ||
# current job execution thread. This is useful for determining | ||
# whether to continue processing a job or to shut down gracefully. | ||
module ThreadStatus | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
# Whether the current job execution thread is in a running state. | ||
# @return [Boolean] | ||
def current_thread_running? | ||
scheduler = Thread.current[:good_job_scheduler] | ||
scheduler ? scheduler.running? : true | ||
end | ||
|
||
# Whether the current job execution thread is shutting down | ||
# (the opposite of running). | ||
# @return [Boolean] | ||
def current_thread_shutting_down? | ||
scheduler = Thread.current[:good_job_scheduler] | ||
scheduler && !scheduler.running? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe GoodJob::ThreadStatus do | ||
describe ".current_thread_running?" do | ||
context "when called outside of an execution context" do | ||
it "returns true" do | ||
expect(GoodJob.current_thread_running?).to eq(true) | ||
end | ||
end | ||
end | ||
|
||
describe ".current_thread_shutting_down?" do | ||
context "when called outside of an execution context" do | ||
it "returns nil" do | ||
expect(GoodJob.current_thread_shutting_down?).to eq(nil) | ||
end | ||
end | ||
|
||
end | ||
|
||
context "when inside of an execution context" do | ||
let(:capsule) { GoodJob::Capsule.new } | ||
let(:adapter) { GoodJob::Adapter.new(execution_mode: :async_all, _capsule: capsule) } | ||
|
||
before do | ||
stub_const "JOB_RUNNING_EVENT", Concurrent::Event.new | ||
stub_const "START_SHUTDOWN_EVENT", Concurrent::Event.new | ||
stub_const "STATUSES", [] | ||
stub_const "TestJob", (Class.new(ActiveJob::Base) do def perform | ||
STATUSES << GoodJob.current_thread_running? | ||
STATUSES << GoodJob.current_thread_shutting_down? | ||
JOB_RUNNING_EVENT.set | ||
START_SHUTDOWN_EVENT.wait(5) | ||
STATUSES << GoodJob.current_thread_running? | ||
STATUSES << GoodJob.current_thread_shutting_down? | ||
end | ||
end) | ||
|
||
TestJob.queue_adapter = adapter | ||
end | ||
|
||
it "returns proper values" do | ||
TestJob.perform_later | ||
|
||
JOB_RUNNING_EVENT.wait(5) | ||
capsule.shutdown(timeout: nil) # don't wait for the shutdown to complete | ||
START_SHUTDOWN_EVENT.set | ||
capsule.shutdown | ||
|
||
expect(STATUSES).to eq([true, false, false, true]) | ||
end | ||
end | ||
end |