Skip to content

Commit

Permalink
Do not allow GoodJob to automatically start after Rails initializatio…
Browse files Browse the repository at this point in the history
…n if previously shutdown (#976)
  • Loading branch information
bensheldon authored Jun 9, 2023
1 parent 99944b4 commit 19dd39f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/good_job/capsule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ def initialize(configuration: GoodJob.configuration)
self.class.instances << self
@configuration = configuration

@autostart = true
@startable = true
@running = false
@mutex = Mutex.new
end

# Start executing jobs (if not already running).
def start
return if @running
# Start the capsule once. After a shutdown, {#restart} must be used to start again.
# @return [nil, Boolean] Whether the capsule was started.
def start(force: false)
return unless startable?(force: force)

@mutex.synchronize do
return if @running
return unless startable?(force: force)

@notifier = GoodJob::Notifier.new(enable_listening: @configuration.enable_listen_notify)
@poller = GoodJob::Poller.new(poll_interval: @configuration.poll_interval)
Expand All @@ -35,7 +36,7 @@ def start

@cron_manager = GoodJob::CronManager.new(@configuration.cron_entries, start_on_initialize: true) if @configuration.enable_cron?

@autostart = false
@startable = false
@running = true
end
end
Expand All @@ -50,7 +51,7 @@ def start
def shutdown(timeout: :default)
timeout = timeout == :default ? @configuration.shutdown_timeout : timeout
GoodJob._shutdown_all([@notifier, @poller, @scheduler, @cron_manager].compact, timeout: timeout)
@autostart = false
@startable = false
@running = false
end

Expand All @@ -61,7 +62,7 @@ def restart(timeout: :default)
raise ArgumentError, "Capsule#restart cannot be called with a timeout of nil" if timeout.nil?

shutdown(timeout: timeout)
start
start(force: true)
end

# @return [Boolean] Whether the capsule is currently running.
Expand All @@ -76,10 +77,16 @@ def shutdown?

# Creates an execution thread(s) with the given attributes.
# @param job_state [Hash, nil] See {GoodJob::Scheduler#create_thread}.
# @return [Boolean, nil] Whether work was started.
# @return [Boolean, nil] Whether the thread was created.
def create_thread(job_state = nil)
start if !running? && @autostart
start if startable?
@scheduler&.create_thread(job_state)
end

private

def startable?(force: false)
!@running && (@startable || force)
end
end
end
26 changes: 26 additions & 0 deletions spec/lib/good_job/capsule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,35 @@
capsule.shutdown
expect(GoodJob::Scheduler.instances.size).to eq 1
end

it 'will not start if previously shutdown' do
capsule = described_class.new
capsule.shutdown

expect { capsule.start }.not_to change(capsule, :running?).from(false)
end
end

describe '#restart' do
it 'can start a previously shutdown capsule' do
capsule = described_class.new
capsule.shutdown

expect { capsule.restart }.to change(capsule, :running?).from(false).to(true)
expect { capsule.restart }.not_to change(capsule, :running?).from(true)
expect { capsule.shutdown }.to change(capsule, :running?).from(true).to(false)
end
end

describe '#shutdown' do
it 'shuts down the capsule' do
capsule = described_class.new
capsule.start

expect { capsule.shutdown }.to change(capsule, :running?).from(true).to(false)
expect(GoodJob::Notifier.instances).to all be_shutdown
end

it 'operates if the capsule has not been started' do
capsule = described_class.new
expect { capsule.shutdown }.not_to raise_error
Expand Down

0 comments on commit 19dd39f

Please sign in to comment.