Class: GoodJob::Capsule
- Inherits:
-
Object
- Object
- GoodJob::Capsule
- Defined in:
- lib/good_job/capsule.rb
Overview
A GoodJob::Capsule contains the resources necessary to execute jobs, including a Scheduler, Poller, Notifier, and CronManager. GoodJob creates a default capsule on initialization.
Class Attribute Summary collapse
-
.instances ⇒ Array<GoodJob::Capsule>?
readonly
List of all instantiated Capsules in the current process.
Instance Attribute Summary collapse
-
#tracker ⇒ Object
readonly
Returns the value of attribute tracker.
Instance Method Summary collapse
-
#create_thread(job_state = nil) ⇒ Boolean?
Creates an execution thread(s) with the given attributes.
-
#idle?(duration = nil) ⇒ Boolean
Whether the capsule is idle.
-
#initialize(configuration: nil) ⇒ Capsule
constructor
A new instance of Capsule.
-
#process_id ⇒ String
UUID for this capsule; to be used for inspection (not directly for locking jobs).
-
#restart(timeout: NONE) ⇒ void
Shutdown and then start the capsule again.
-
#running? ⇒ Boolean
Whether the capsule is currently running.
-
#shutdown(timeout: NONE) ⇒ void
Shut down the thread pool executors.
-
#shutdown? ⇒ Boolean
Whether the capsule has been shutdown.
-
#start(force: false) ⇒ nil, Boolean
Start the capsule once.
Constructor Details
#initialize(configuration: nil) ⇒ Capsule
Returns a new instance of Capsule.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/good_job/capsule.rb', line 19 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 |
Class Attribute Details
.instances ⇒ Array<GoodJob::Capsule>? (readonly)
List of all instantiated Capsules in the current process.
12 |
# File 'lib/good_job/capsule.rb', line 12 cattr_reader :instances, default: Concurrent::Array.new, instance_reader: false |
Instance Attribute Details
#tracker ⇒ Object (readonly)
Returns the value of attribute tracker.
16 17 18 |
# File 'lib/good_job/capsule.rb', line 16 def tracker @tracker end |
Instance Method Details
#create_thread(job_state = nil) ⇒ Boolean?
Creates an execution thread(s) with the given attributes.
102 103 104 105 |
# File 'lib/good_job/capsule.rb', line 102 def create_thread(job_state = nil) start if startable? @multi_scheduler&.create_thread(job_state) end |
#idle?(duration = nil) ⇒ Boolean
Returns Whether the capsule is idle.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/good_job/capsule.rb', line 87 def idle?(duration = nil) scheduler_stats = @multi_scheduler&.stats || {} is_idle = scheduler_stats.fetch(:active_execution_thread_count, 0).zero? if is_idle && duration active_at = scheduler_stats.fetch(:execution_at, nil) || @started_at active_at.nil? || (Time.current - active_at >= duration) else is_idle end end |
#process_id ⇒ String
UUID for this capsule; to be used for inspection (not directly for locking jobs).
109 110 111 |
# File 'lib/good_job/capsule.rb', line 109 def process_id @tracker.process_id end |
#restart(timeout: NONE) ⇒ void
This method returns an undefined value.
Shutdown and then start the capsule again.
68 69 70 71 72 73 |
# File 'lib/good_job/capsule.rb', line 68 def restart(timeout: NONE) raise ArgumentError, "Capsule#restart cannot be called with a timeout of nil" if timeout.nil? shutdown(timeout: timeout) start(force: true) end |
#running? ⇒ Boolean
Returns Whether the capsule is currently running.
76 77 78 |
# File 'lib/good_job/capsule.rb', line 76 def running? @started_at.present? end |
#shutdown(timeout: NONE) ⇒ void
This method returns an undefined value.
Shut down the thread pool executors.
58 59 60 61 62 63 |
# File 'lib/good_job/capsule.rb', line 58 def shutdown(timeout: NONE) timeout = configuration.shutdown_timeout if timeout == NONE GoodJob._shutdown_all([@notifier, @poller, @multi_scheduler, @cron_manager].compact, after: [@shared_executor], timeout: timeout) @startable = false @started_at = nil end |
#shutdown? ⇒ Boolean
Returns Whether the capsule has been shutdown.
81 82 83 |
# File 'lib/good_job/capsule.rb', line 81 def shutdown? [@notifier, @poller, @multi_scheduler, @cron_manager].compact.all?(&:shutdown?) end |
#start(force: false) ⇒ nil, Boolean
Start the capsule once. After a shutdown, #restart must be used to start again.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/good_job/capsule.rb', line 33 def start(force: false) return unless startable?(force: force) @mutex.synchronize do return unless startable?(force: force) @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, 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) if configuration.enable_cron? @startable = false @started_at = Time.current end end |