Class: EvilEvents::Core::Events::Notifier::Worker::Executor Private

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/evil_events/core/events/notifier/worker/executor.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

since 0.3.0

Since:

  • 0.3.0

Constant Summary collapse

FALLBACK_POLICIES =

Since:

  • 0.3.0

{
  exception:   :abort,
  ignorance:   :discard,
  main_thread: :caller_runs
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log_activity, #log_failure, #log_success

Constructor Details

#initialize(min_threads:, max_threads:, max_queue:, fallback_policy:) ⇒ Executor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Executor.

Parameters:

  • min_threads (Hash)

    a customizable set of options

  • max_threads (Hash)

    a customizable set of options

  • max_queue (Hash)

    a customizable set of options

  • fallback_policy (Hash)

    a customizable set of options

Options Hash (min_threads:):

  • (Integer)

Options Hash (max_threads:):

  • (Integer)

Options Hash (max_queue:):

  • (Integer)

Options Hash (fallback_policy:):

  • (Symbol)

Raises:

Since:

  • 0.3.0



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/evil_events/core/events/notifier/worker/executor.rb', line 37

def initialize(min_threads:, max_threads:, max_queue:, fallback_policy:)
  raise EvilEvents::IncorrectFallbackPolicyError unless FALLBACK_POLICIES[fallback_policy]

  @options = {
    min_threads:     min_threads,
    max_threads:     max_threads,
    max_queue:       max_queue,
    fallback_policy: FALLBACK_POLICIES[fallback_policy]
  }.freeze

  initialize_raw_executor!(**@options)
end

Instance Attribute Details

#optionsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash)

Since:

  • 0.3.0



27
28
29
# File 'lib/evil_events/core/events/notifier/worker/executor.rb', line 27

def options
  @options
end

#raw_executorConcurrent::ThreadPoolExecutor (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Concurrent::ThreadPoolExecutor)

Since:

  • 0.3.0



21
22
23
# File 'lib/evil_events/core/events/notifier/worker/executor.rb', line 21

def raw_executor
  @raw_executor
end

Instance Method Details

#execute(job) ⇒ Concurrent::Promise

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize, Style/MultilineBlockChain

Parameters:

  • job (EvilEvents::Core::Events::Notifier::Job)

Returns:

  • (Concurrent::Promise)

Raises:

Since:

  • 0.3.0



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/evil_events/core/events/notifier/worker/executor.rb', line 57

def execute(job)
  Concurrent::Promise.new(executor: raw_executor) do
    job.perform
  end.on_success do
    log_success(job.event, job.subscriber)
  end.on_error do |error|
    log_failure(job.event, job.subscriber)
    job.event.__call_on_error_hooks__(error)
  end.execute
rescue Concurrent::RejectedExecutionError
  raise EvilEvents::WorkerDisabledOrBusyError
end

#initialize_raw_executor!(**options) ⇒ Concurrent::ThreadPoolExecutor (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • min_threads (Hash)

    a customizable set of options

  • max_threads (Hash)

    a customizable set of options

  • max_queue (Hash)

    a customizable set of options

  • fallback_policy (Hash)

    a customizable set of options

Returns:

  • (Concurrent::ThreadPoolExecutor)

Since:

  • 0.3.0



99
100
101
# File 'lib/evil_events/core/events/notifier/worker/executor.rb', line 99

def initialize_raw_executor!(**options)
  @raw_executor = Concurrent::ThreadPoolExecutor.new(**options)
end

#restart!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns void.

Returns:

  • void

Since:

  • 0.3.0



84
85
86
87
# File 'lib/evil_events/core/events/notifier/worker/executor.rb', line 84

def restart!
  shutdown!
  initialize_raw_executor!(**options)
end

#shutdown!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns void.

Returns:

  • void

Since:

  • 0.3.0



75
76
77
78
# File 'lib/evil_events/core/events/notifier/worker/executor.rb', line 75

def shutdown!
  raw_executor.shutdown
  raw_executor.wait_for_termination
end