lib/good_job/capsule.rb in good_job-3.28.2 vs lib/good_job/capsule.rb in good_job-3.29.3

- old
+ new

@@ -9,17 +9,24 @@ # @!scope class # List of all instantiated Capsules in the current process. # @return [Array<GoodJob::Capsule>, nil] cattr_reader :instances, default: Concurrent::Array.new, instance_reader: false + delegate :register, :renew, :unregister, :id_for_lock, to: :@tracker, prefix: :_tracker + + attr_reader :tracker + # @param configuration [GoodJob::Configuration] Configuration to use for this capsule. def initialize(configuration: nil) @configuration = configuration @startable = true @started_at = nil @mutex = Mutex.new + @shared_executor = GoodJob::SharedExecutor.new + @tracker = GoodJob::CapsuleTracker.new(executor: @shared_executor) + self.class.instances << self end # Start the capsule once. After a shutdown, {#restart} must be used to start again. # @return [nil, Boolean] Whether the capsule was started. @@ -27,19 +34,17 @@ return unless startable?(force: force) @mutex.synchronize do return unless startable?(force: force) - @shared_executor = GoodJob::SharedExecutor.new - @notifier = GoodJob::Notifier.new(enable_listening: configuration.enable_listen_notify, executor: @shared_executor.executor) + @notifier = GoodJob::Notifier.new(enable_listening: configuration.enable_listen_notify, capsule: self, executor: @shared_executor) @poller = GoodJob::Poller.new(poll_interval: configuration.poll_interval) - @multi_scheduler = GoodJob::MultiScheduler.from_configuration(configuration, warm_cache_on_initialize: true) + @multi_scheduler = GoodJob::MultiScheduler.from_configuration(configuration, capsule: self, warm_cache_on_initialize: true) @notifier.recipients.push([@multi_scheduler, :create_thread]) @poller.recipients.push(-> { @multi_scheduler.create_thread({ fanout: true }) }) - @cron_manager = GoodJob::CronManager.new(configuration.cron_entries, start_on_initialize: true, executor: @shared_executor.executor) if configuration.enable_cron? - + @cron_manager = GoodJob::CronManager.new(configuration.cron_entries, start_on_initialize: true, executor: @shared_executor) if configuration.enable_cron? @startable = false @started_at = Time.current end end @@ -50,11 +55,11 @@ # * +N+ will wait at most N seconds and then interrupt active threads. # * +nil+ will trigger a shutdown but not wait for it to complete. # @return [void] def shutdown(timeout: NONE) timeout = configuration.shutdown_timeout if timeout == NONE - GoodJob._shutdown_all([@shared_executor, @notifier, @poller, @multi_scheduler, @cron_manager].compact, timeout: timeout) + GoodJob._shutdown_all([@notifier, @poller, @multi_scheduler, @cron_manager].compact, after: [@shared_executor], timeout: timeout) @startable = false @started_at = nil end # Shutdown and then start the capsule again. @@ -72,11 +77,11 @@ @started_at.present? end # @return [Boolean] Whether the capsule has been shutdown. def shutdown? - [@shared_executor, @notifier, @poller, @multi_scheduler, @cron_manager].compact.all?(&:shutdown?) + [@notifier, @poller, @multi_scheduler, @cron_manager].compact.all?(&:shutdown?) end # @param duration [nil, Numeric] Length of idleness to check for (in seconds). # @return [Boolean] Whether the capsule is idle def idle?(duration = nil) @@ -95,9 +100,15 @@ # @param job_state [Hash, nil] See {GoodJob::Scheduler#create_thread}. # @return [Boolean, nil] Whether the thread was created. def create_thread(job_state = nil) start if startable? @multi_scheduler&.create_thread(job_state) + end + + # UUID for this capsule; to be used for inspection (not directly for locking jobs). + # @return [String] + def process_id + @tracker.process_id end private def configuration