Class: GoodJob::SharedExecutor
- Inherits:
-
Object
- Object
- GoodJob::SharedExecutor
- Includes:
- Concurrent::ExecutorService
- Defined in:
- lib/good_job/shared_executor.rb
Constant Summary collapse
- MAX_THREADS =
2
Class Attribute Summary collapse
-
.instances ⇒ Array<GoodJob::SharedExecutor>?
readonly
List of all instantiated SharedExecutor in the current process.
Instance Attribute Summary collapse
-
#executor ⇒ Object
readonly
Returns the value of attribute executor.
Instance Method Summary collapse
-
#initialize ⇒ SharedExecutor
constructor
A new instance of SharedExecutor.
- #post(*args, &task) ⇒ Object
- #restart(timeout: -1)) ⇒ Object
- #running? ⇒ Boolean
-
#shutdown(timeout: -1)) ⇒ void
Shut down the SharedExecutor.
-
#shutdown? ⇒ Boolean?
Tests whether the scheduler is shutdown and no tasks are running.
Constructor Details
#initialize ⇒ SharedExecutor
Returns a new instance of SharedExecutor.
20 21 22 23 24 |
# File 'lib/good_job/shared_executor.rb', line 20 def initialize @mutex = Mutex.new self.class.instances << self end |
Class Attribute Details
.instances ⇒ Array<GoodJob::SharedExecutor>? (readonly)
List of all instantiated SharedExecutor in the current process.
16 |
# File 'lib/good_job/shared_executor.rb', line 16 cattr_reader :instances, default: Concurrent::Array.new, instance_reader: false |
Instance Attribute Details
#executor ⇒ Object (readonly)
Returns the value of attribute executor.
18 19 20 |
# File 'lib/good_job/shared_executor.rb', line 18 def executor @executor end |
Instance Method Details
#post(*args, &task) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/good_job/shared_executor.rb', line 26 def post(*args, &task) unless running? @mutex.synchronize do next if running? create_executor end end @executor&.post(*args, &task) end |
#restart(timeout: -1)) ⇒ Object
70 71 72 73 |
# File 'lib/good_job/shared_executor.rb', line 70 def restart(timeout: -1) shutdown(timeout: timeout) if running? create_executor end |
#running? ⇒ Boolean
38 39 40 |
# File 'lib/good_job/shared_executor.rb', line 38 def running? @executor&.running? end |
#shutdown(timeout: -1)) ⇒ void
This method returns an undefined value.
Shut down the SharedExecutor. Use #shutdown? to determine whether threads have stopped.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/good_job/shared_executor.rb', line 56 def shutdown(timeout: -1) return if @executor.nil? || (@executor.shutdown? && !@executor.shuttingdown?) @executor.shutdown if @executor.running? if @executor.shuttingdown? && timeout # rubocop:disable Style/GuardClause executor_wait = timeout.negative? ? nil : timeout return if @executor.wait_for_termination(executor_wait) @executor.kill @executor.wait_for_termination end end |
#shutdown? ⇒ Boolean?
Tests whether the scheduler is shutdown and no tasks are running.
44 45 46 |
# File 'lib/good_job/shared_executor.rb', line 44 def shutdown? @executor.nil? || (@executor.shutdown? && !@executor.shuttingdown?) end |