lib/good_job/capsule.rb in good_job-3.15.11 vs lib/good_job/capsule.rb in good_job-3.15.12

- old
+ new

@@ -13,31 +13,32 @@ # @param configuration [GoodJob::Configuration] Configuration to use for this capsule. 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) @scheduler = GoodJob::Scheduler.from_configuration(@configuration, warm_cache_on_initialize: true) @notifier.recipients << [@scheduler, :create_thread] @poller.recipients << [@scheduler, :create_thread] @cron_manager = GoodJob::CronManager.new(@configuration.cron_entries, start_on_initialize: true) if @configuration.enable_cron? - @autostart = false + @startable = false @running = true end end # Shut down the thread pool executors. @@ -48,22 +49,22 @@ # * +nil+ will trigger a shutdown but not wait for it to complete. # @return [void] 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 # Shutdown and then start the capsule again. # @param timeout [Numeric, Symbol] Seconds to wait for active threads. # @return [void] 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. def running? @running @@ -74,12 +75,18 @@ [@notifier, @poller, @scheduler, @cron_manager].compact.all?(&:shutdown?) end # 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