Class: GoodJob::MultiScheduler
- Inherits:
-
Object
- Object
- GoodJob::MultiScheduler
- Defined in:
- lib/good_job/multi_scheduler.rb
Overview
Delegates the interface of a single Scheduler to multiple Schedulers.
Instance Attribute Summary collapse
-
#schedulers ⇒ Array<Scheduler>
readonly
List of the scheduler delegates.
Class Method Summary collapse
-
.from_configuration(configuration, capsule: GoodJob.capsule, warm_cache_on_initialize: false) ⇒ GoodJob::MultiScheduler
Creates MultiScheduler from a GoodJob::Configuration instance.
Instance Method Summary collapse
-
#create_thread(state = nil) ⇒ Boolean?
Delegates to Scheduler#create_thread.
-
#initialize(schedulers) ⇒ MultiScheduler
constructor
A new instance of MultiScheduler.
- #lower_thread_priority=(value) ⇒ Object
-
#restart(timeout: -1)) ⇒ void
Delegates to Scheduler#restart.
-
#running? ⇒ Boolean?
Delegates to Scheduler#running?.
-
#shutdown(timeout: -1)) ⇒ void
Delegates to Scheduler#shutdown.
-
#shutdown? ⇒ Boolean?
Delegates to Scheduler#shutdown?.
- #stats ⇒ Object
Constructor Details
#initialize(schedulers) ⇒ MultiScheduler
Returns a new instance of MultiScheduler.
34 35 36 |
# File 'lib/good_job/multi_scheduler.rb', line 34 def initialize(schedulers) @schedulers = schedulers end |
Instance Attribute Details
#schedulers ⇒ Array<Scheduler> (readonly)
Returns List of the scheduler delegates.
31 32 33 |
# File 'lib/good_job/multi_scheduler.rb', line 31 def schedulers @schedulers end |
Class Method Details
.from_configuration(configuration, capsule: GoodJob.capsule, warm_cache_on_initialize: false) ⇒ GoodJob::MultiScheduler
Creates MultiScheduler from a GoodJob::Configuration instance.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/good_job/multi_scheduler.rb', line 10 def self.from_configuration(configuration, capsule: GoodJob.capsule, warm_cache_on_initialize: false) schedulers = configuration.queue_string.split(';').map(&:strip).map do |queue_string_and_max_threads| queue_string, max_threads = queue_string_and_max_threads.split(':').map { |str| str.strip.presence } max_threads = (max_threads || configuration.max_threads).to_i job_performer = GoodJob::JobPerformer.new(queue_string, capsule: capsule) GoodJob::Scheduler.new( job_performer, max_threads: max_threads, max_cache: configuration.max_cache, warm_cache_on_initialize: warm_cache_on_initialize, cleanup_interval_seconds: configuration.cleanup_interval_seconds, cleanup_interval_jobs: configuration.cleanup_interval_jobs, lower_thread_priority: configuration.lower_thread_priority ) end new(schedulers) end |
Instance Method Details
#create_thread(state = nil) ⇒ Boolean?
Delegates to Scheduler#create_thread.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/good_job/multi_scheduler.rb', line 67 def create_thread(state = nil) results = [] if state && !state[:fanout] schedulers.any? do |scheduler| scheduler.create_thread(state).tap { |result| results << result } end else schedulers.each do |scheduler| results << scheduler.create_thread(state) end end if results.any? true elsif results.any?(false) false else # rubocop:disable Style/EmptyElse nil end end |
#lower_thread_priority=(value) ⇒ Object
89 90 91 92 93 |
# File 'lib/good_job/multi_scheduler.rb', line 89 def lower_thread_priority=(value) schedulers.each do |scheduler| scheduler.lower_thread_priority = value end end |
#restart(timeout: -1)) ⇒ void
This method returns an undefined value.
Delegates to Scheduler#restart.
60 61 62 |
# File 'lib/good_job/multi_scheduler.rb', line 60 def restart(timeout: -1) GoodJob._shutdown_all(schedulers, :restart, timeout: timeout) end |
#running? ⇒ Boolean?
Delegates to Scheduler#running?.
40 41 42 |
# File 'lib/good_job/multi_scheduler.rb', line 40 def running? schedulers.all?(&:running?) end |
#shutdown(timeout: -1)) ⇒ void
This method returns an undefined value.
Delegates to Scheduler#shutdown.
53 54 55 |
# File 'lib/good_job/multi_scheduler.rb', line 53 def shutdown(timeout: -1) GoodJob._shutdown_all(schedulers, timeout: timeout) end |
#shutdown? ⇒ Boolean?
Delegates to Scheduler#shutdown?.
46 47 48 |
# File 'lib/good_job/multi_scheduler.rb', line 46 def shutdown? schedulers.all?(&:shutdown?) end |
#stats ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/good_job/multi_scheduler.rb', line 95 def stats scheduler_stats = schedulers.map(&:stats) { schedulers: scheduler_stats, empty_executions_count: scheduler_stats.sum { |stats| stats.fetch(:empty_executions_count, 0) }, errored_executions_count: scheduler_stats.sum { |stats| stats.fetch(:errored_executions_count, 0) }, succeeded_executions_count: scheduler_stats.sum { |stats| stats.fetch(:succeeded_executions_count, 0) }, total_executions_count: scheduler_stats.sum { |stats| stats.fetch(:total_executions_count, 0) }, execution_at: scheduler_stats.map { |stats| stats.fetch(:execution_at, nil) }.compact.max, active_execution_thread_count: scheduler_stats.sum { |stats| stats.fetch(:active_threads, 0) }, check_queue_at: scheduler_stats.map { |stats| stats.fetch(:check_queue_at, nil) }.compact.max, } end |