Class: GoodJob::SharedExecutor

Inherits:
Object
  • Object
show all
Includes:
Concurrent::ExecutorService
Defined in:
lib/good_job/shared_executor.rb

Constant Summary collapse

MAX_THREADS =
2

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSharedExecutor

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

.instancesArray<GoodJob::SharedExecutor>? (readonly)

List of all instantiated SharedExecutor in the current process.

Returns:



16
# File 'lib/good_job/shared_executor.rb', line 16

cattr_reader :instances, default: Concurrent::Array.new, instance_reader: false

Instance Attribute Details

#executorObject (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

Returns:

  • (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.

Parameters:

  • timeout (Numeric, nil) (defaults to: -1))

    Seconds to wait for active threads.

    • nil, the scheduler will trigger a shutdown but not wait for it to complete.

    • -1, the scheduler will wait until the shutdown is complete.

    • 0, the scheduler will immediately shutdown and stop any threads.

    • A positive number will wait that many seconds before stopping any remaining active threads.



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.

Returns:

  • (Boolean, nil)


44
45
46
# File 'lib/good_job/shared_executor.rb', line 44

def shutdown?
  @executor.nil? || (@executor.shutdown? && !@executor.shuttingdown?)
end