Class: Exekutor::Configuration

Inherits:
Object
  • Object
show all
Includes:
Internal::ConfigurationBuilder
Defined in:
lib/exekutor/configuration.rb

Overview

Configuration for the Exekutor library

Defined Under Namespace

Classes: Error, SerializerValidator

Instance Method Summary collapse

Methods included from Internal::ConfigurationBuilder

#set, #validate_option_enum!, #validate_option_presence!, #validate_option_range!, #validate_option_type!

Instance Method Details

#base_record_classClass

Gets the base class for database records. Is derived from the #base_record_class_name option.

Returns:

  • (Class)

Raises:

  • (Error)

    when the class cannot be found



48
49
50
51
52
53
54
55
56
57
# File 'lib/exekutor/configuration.rb', line 48

def base_record_class
  const_get :base_record_class_name
rescue ::StandardError
  # A nicer message for the default value
  if base_record_class_name == DEFAULT_BASE_RECORD_CLASS
    raise Error, "Cannot find ActiveRecord, did you install and load the gem?"
  end

  raise
end

#base_record_class_nameString

Gets the base class name for database records.

Default value:

“ActiveRecord::Base”

Returns:

  • (String)


43
# File 'lib/exekutor/configuration.rb', line 43

define_option :base_record_class_name, default: DEFAULT_BASE_RECORD_CLASS, required: true, type: String

#base_record_class_name=(value) ⇒ self

Sets the base class name for database records. The validity of this value will not be checked immediately. (Ie. When the specified class does not exist, an error will raised when a database record is used for the first time.)

Parameters:

  • value (String)

    the class name

Returns:

  • (self)

Raises:

  • (Error)

    When the name is blank



43
# File 'lib/exekutor/configuration.rb', line 43

define_option :base_record_class_name, default: DEFAULT_BASE_RECORD_CLASS, required: true, type: String

#default_queue_priorityInteger

Gets the default queue priority. Is used when enqueueing jobs that don’t have a priority set.

Default value:

16,383

Returns:

  • (Integer)


27
28
# File 'lib/exekutor/configuration.rb', line 27

define_option :default_queue_priority, default: 16_383, required: true, type: Integer,
range: Exekutor::Queue::VALID_PRIORITIES

#default_queue_priority=(value) ⇒ self

Sets the default queue priority. Is used when enqueueing jobs that don’t have a priority set. Should be between 1 and 32,767.

Parameters:

  • value (Integer)

    the priority

Returns:

  • (self)

Raises:

  • (Error)

    When the priority is nil or invalid



27
28
# File 'lib/exekutor/configuration.rb', line 27

define_option :default_queue_priority, default: 16_383, required: true, type: Integer,
range: Exekutor::Queue::VALID_PRIORITIES

#delete_completed_jobs=(value) ⇒ self

Sets whether the worker should delete jobs after completion

Parameters:

  • value (Boolean)

    whether to delete completed jobs

Returns:

  • (self)


152
153
# File 'lib/exekutor/configuration.rb', line 152

define_option :delete_completed_jobs, reader: :delete_completed_jobs?, required: true,
type: [TrueClass, FalseClass], default: false

#delete_completed_jobs?Boolean

Whether the worker should delete jobs after completion.

Default value:

false

Returns:

  • (Boolean)


152
153
# File 'lib/exekutor/configuration.rb', line 152

define_option :delete_completed_jobs, reader: :delete_completed_jobs?, required: true,
type: [TrueClass, FalseClass], default: false

#delete_discarded_jobs=(value) ⇒ self

Sets whether the worker should delete discarded jobs

Parameters:

  • value (Boolean)

    whether to delete discarded jobs

Returns:

  • (self)


165
166
# File 'lib/exekutor/configuration.rb', line 165

define_option :delete_discarded_jobs, reader: :delete_discarded_jobs?, required: true,
type: [TrueClass, FalseClass], default: false

#delete_discarded_jobs?Boolean

Whether the worker should delete discarded jobs.

Default value:

false

Returns:

  • (Boolean)


165
166
# File 'lib/exekutor/configuration.rb', line 165

define_option :delete_discarded_jobs, reader: :delete_discarded_jobs?, required: true,
type: [TrueClass, FalseClass], default: false

#delete_failed_jobs=(value) ⇒ self

Sets whether the worker should delete jobs after they failed to execute

Parameters:

  • value (Boolean)

    whether to delete failed jobs

Returns:

  • (self)


178
179
# File 'lib/exekutor/configuration.rb', line 178

define_option :delete_failed_jobs, reader: :delete_failed_jobs?, required: true,
type: [TrueClass, FalseClass], default: false

#delete_failed_jobs?Boolean

Whether the worker should delete jobs after they failed to execute.

Default value:

false

Returns:

  • (Boolean)


178
179
# File 'lib/exekutor/configuration.rb', line 178

define_option :delete_failed_jobs, reader: :delete_failed_jobs?, required: true,
type: [TrueClass, FalseClass], default: false

#enable_listener=(value) ⇒ self

Sets whether the worker should use LISTEN/NOTIFY to listen for jobs

Parameters:

  • value (Boolean)

    whether to enable the listener

Returns:

  • (self)


139
140
# File 'lib/exekutor/configuration.rb', line 139

define_option :enable_listener, reader: :enable_listener?, default: true, type: [TrueClass, FalseClass],
required: true

#enable_listener?Boolean?

Whether the worker should use LISTEN/NOTIFY to listen for jobs.

Default value:

true

Returns:

  • (Boolean, nil)


139
140
# File 'lib/exekutor/configuration.rb', line 139

define_option :enable_listener, reader: :enable_listener?, default: true, type: [TrueClass, FalseClass],
required: true

#healthcheck_timeout=(value) ⇒ self

Sets the heartbeat timeout for the ‘/live` endpoint of the status server. Must be between 1 minute and 24 hours.

Parameters:

  • value (ActiveSupport::Duration)

    The timeout in minutes

Returns:

  • (self)


291
292
# File 'lib/exekutor/configuration.rb', line 291

define_option :healthcheck_timeout, default: 30.minutes, type: ActiveSupport::Duration,
range: (1.minute)..(1.day)

#healthcheck_timeout?ActiveSupport::Duration

The heartbeat timeout for the ‘/live` endpoint of the status server. If the heartbeat of a worker is older than this timeout, the status server will respond with a 503 status indicating the service is down.

Default value:

30 minutes

Returns:

  • (ActiveSupport::Duration)


291
292
# File 'lib/exekutor/configuration.rb', line 291

define_option :healthcheck_timeout, default: 30.minutes, type: ActiveSupport::Duration,
range: (1.minute)..(1.day)

#inverse_priority=(value) ⇒ self

Sets whether the the priority should be inverted. When true, the job with the highest value as the priority is the most important; when false, the job with the lowest value as the priority is the most important.

Parameters:

  • value (Boolean)

    whether the job with the highest priority value is the most important.

Returns:

  • (self)


306
307
# File 'lib/exekutor/configuration.rb', line 306

define_option :inverse_priority, reader: :inverse_priority?, type: [TrueClass, FalseClass], required: true,
default: false

#inverse_priority?Boolean

Whether the priority should be inverted. If true, the job with the highest value as the priority is the most important. If false, the job with the lowest value as the priority is the most important.

Default value:

false

Returns:

  • (Boolean)


306
307
# File 'lib/exekutor/configuration.rb', line 306

define_option :inverse_priority, reader: :inverse_priority?, type: [TrueClass, FalseClass], required: true,
default: false

#json_serializerString, ...

Gets the unconverted JSON serializer value. This can be either a String, a Symbol, a Proc, or the serializer.

Default value:

JSON

Returns:

  • (String, Symbol, Proc, Object)


76
77
78
79
80
# File 'lib/exekutor/configuration.rb', line 76

define_option :json_serializer, default: "::JSON", required: true do |value|
  unless value.is_a?(String) || value.is_a?(Symbol) || value.respond_to?(:call) || SerializerValidator.valid?(value)
    raise Error, "#json_serializer must either be a String, a Proc, or respond to #dump and #load"
  end
end

#json_serializer=(value) ⇒ self

Sets the JSON serializer. This can be either a String, a Symbol, a Proc, or the serializer. If a String, Symbol, or Proc is given, the serializer will be loaded when it is needed for the first time. If the loaded class does not respond to #dump and #load an Error will be raised whenever the serializer is loaded. If the value is neither a String, Symbol, nor a Proc and it does not respond to #dump and #load, the error will be thrown immediately.

Parameters:

  • value (String, Symbol, Proc, Object)

    the serializer

Returns:

  • (self)

Raises:

  • (Error)

    When the value is neither a String, Symbol, nor a Proc and it does not respond to #dump and #load



76
77
78
79
80
# File 'lib/exekutor/configuration.rb', line 76

define_option :json_serializer, default: "::JSON", required: true do |value|
  unless value.is_a?(String) || value.is_a?(Symbol) || value.respond_to?(:call) || SerializerValidator.valid?(value)
    raise Error, "#json_serializer must either be a String, a Proc, or respond to #dump and #load"
  end
end

#load_json_serializerObject

Gets the JSON serializer. Is derived from the #json_serializer option.

Returns:

  • (Object)

Raises:

  • (Error)

    when the class cannot be found, or does not respond to #dump and #load



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/exekutor/configuration.rb', line 85

def load_json_serializer
  raw_value = json_serializer
  if defined?(@json_serializer_instance) && @json_serializer_instance[0] == raw_value
    return @json_serializer_instance[1]
  end

  serializer = const_get :json_serializer
  serializer = SerializerValidator.convert! serializer unless SerializerValidator.valid? serializer
  @json_serializer_instance = [raw_value, serializer]
  serializer
end

#loggerActiveSupport::Logger

Gets the logger.

Default value:

Rails.active_job.logger

Returns:

  • (ActiveSupport::Logger)


107
# File 'lib/exekutor/configuration.rb', line 107

define_option :logger, default: -> { Rails.active_job.logger }

#logger=(value) ⇒ self

Sets the logger.

Parameters:

  • value (ActiveSupport::Logger)

    the logger

Returns:

  • (self)


107
# File 'lib/exekutor/configuration.rb', line 107

define_option :logger, default: -> { Rails.active_job.logger }

#max_execution_thread_idletimeActiveSupport::Duration

The maximum duration a thread may be idle before being stopped.

Default value:

60 seconds

Returns:

  • (ActiveSupport::Duration)


251
252
# File 'lib/exekutor/configuration.rb', line 251

define_option :max_execution_thread_idletime, default: 1.minute, type: ActiveSupport::Duration,
range: (1.second)..(1.day)

#max_execution_thread_idletime=(value) ⇒ self

Sets the maximum duration a thread may be idle before being stopped

Parameters:

  • value (ActiveSupport::Duration)

    the number of threads

Returns:

  • (self)


251
252
# File 'lib/exekutor/configuration.rb', line 251

define_option :max_execution_thread_idletime, default: 1.minute, type: ActiveSupport::Duration,
range: (1.second)..(1.day)

#max_execution_threadsInteger

The maximum number of execution threads that may be active.

Default value:

Active record pool size minus 1, with a minimum of 1

Returns:

  • (Integer)


237
238
239
# File 'lib/exekutor/configuration.rb', line 237

define_option :max_execution_threads,
default: -> { (Internal::BaseRecord.connection_db_config.pool.to_i - 1).clamp(1, 999) },
type: Integer, range: 1...999

#max_execution_threads=(value) ⇒ self

Sets the maximum number of execution threads that may be active. Be aware that if you set this to a value greater than connection_db_config.pool, workers may have to wait for database connections to become available because all connections are occupied by other threads. This may result in an ActiveRecord::ConnectionTimeoutError if the thread has to wait too long.

Parameters:

  • value (Integer)

    the number of threads

Returns:

  • (self)


237
238
239
# File 'lib/exekutor/configuration.rb', line 237

define_option :max_execution_threads,
default: -> { (Internal::BaseRecord.connection_db_config.pool.to_i - 1).clamp(1, 999) },
type: Integer, range: 1...999

#min_execution_threadsInteger

The minimum number of execution threads that should be active.

Default value:

1

Returns:

  • (Integer)


222
# File 'lib/exekutor/configuration.rb', line 222

define_option :min_execution_threads, default: 1, type: Integer, range: 1...999

#min_execution_threads=(value) ⇒ self

Sets the minimum number of execution threads that should be active

Parameters:

  • value (Integer)

    the number of threads

Returns:

  • (self)


222
# File 'lib/exekutor/configuration.rb', line 222

define_option :min_execution_threads, default: 1, type: Integer, range: 1...999

#polling_intervalActiveSupport::Duration

The polling interval. When set, the worker will poll the database with this interval to check for any pending jobs that a listener might have missed (if enabled).

Default value:

60 seconds

Returns:

  • (ActiveSupport::Duration)


194
195
# File 'lib/exekutor/configuration.rb', line 194

define_option :polling_interval, default: 1.minute, type: [ActiveSupport::Duration, nil],
range: (1.second)...(1.day)

#polling_interval=(value) ⇒ self

Sets the polling interval. Set to nil to disable polling. If the listener is disabled, this value should be reasonably low so jobs don’t have to wait in the queue too long; if the listener is enabled, this value can be reasonably high.

Parameters:

  • value (ActiveSupport::Duration)

    the interval

Returns:

  • (self)


194
195
# File 'lib/exekutor/configuration.rb', line 194

define_option :polling_interval, default: 1.minute, type: [ActiveSupport::Duration, nil],
range: (1.second)...(1.day)

#polling_jitterFloat

The polling jitter, used to adjust the polling interval slightly so multiple workers will not query the database at the same time.

Default value:

0.1

Returns:

  • (Float)


210
# File 'lib/exekutor/configuration.rb', line 210

define_option :polling_jitter, default: 0.1, type: [Float, Integer], range: 0..0.5

#polling_jitter=(value) ⇒ self

Sets the polling jitter, which is used to slightly adjust the polling interval. Should be between 0 and 0.5. A value of 0.1 means the polling interval can vary by 10%. If the interval is set to 60 seconds and the jitter is set to 0.1, the interval can range from 57 to 63 seconds. A value of 0 disables this feature.

Parameters:

  • value (Float)

    the jitter

Returns:

  • (self)


210
# File 'lib/exekutor/configuration.rb', line 210

define_option :polling_jitter, default: 0.1, type: [Float, Integer], range: 0..0.5

#quiet=(value) ⇒ self

Sets whether the STDOUT messages should be printed

Parameters:

  • value (Boolean)

    whether to suppress STDOUT messages

Returns:

  • (self)


319
# File 'lib/exekutor/configuration.rb', line 319

define_option :quiet, reader: :quiet?, type: [TrueClass, FalseClass], required: true, default: false

#quiet?Boolean

Whether to suppress STDOUT messages

Default value:

false

Returns:

  • (Boolean)


319
# File 'lib/exekutor/configuration.rb', line 319

define_option :quiet, reader: :quiet?, type: [TrueClass, FalseClass], required: true, default: false

#set_db_connection_name=(value) ⇒ self

Sets whether the DB connection name should be set

Parameters:

  • value (Boolean)

    whether to name should be set

Returns:

  • (self)


119
# File 'lib/exekutor/configuration.rb', line 119

define_option :set_db_connection_name, type: [TrueClass, FalseClass], required: true

#set_db_connection_name?Boolean?

Whether the DB connection name should be set. Only affects the listener, unless started from the CLI.

Default value:

false (true when started from the CLI)

Returns:

  • (Boolean, nil)


119
# File 'lib/exekutor/configuration.rb', line 119

define_option :set_db_connection_name, type: [TrueClass, FalseClass], required: true

#status_server_handler=(value) ⇒ self

Sets the rack handler for the status server. The handler should respond to #shutdown or #stop.

Parameters:

  • value (String)

    the name of the handler

Returns:

  • (self)


264
# File 'lib/exekutor/configuration.rb', line 264

define_option :status_server_handler, default: "webrick", type: String

#status_server_handler?String

The rack handler for the status server

Default value:

“webrick”

Returns:

  • (String)


264
# File 'lib/exekutor/configuration.rb', line 264

define_option :status_server_handler, default: "webrick", type: String

#status_server_port=(value) ⇒ self

Sets the port number for the status server.

Parameters:

  • value (Integer)

    the port number

Returns:

  • (self)


276
# File 'lib/exekutor/configuration.rb', line 276

define_option :status_server_port, default: nil, type: Integer

#status_server_port?Integer

The port number for the status server

Default value:

nil (ie. the status server is disabled)

Returns:

  • (Integer)


276
# File 'lib/exekutor/configuration.rb', line 276

define_option :status_server_port, default: nil, type: Integer

#worker_optionsHash

Gets the options for a worker

Returns:

  • (Hash)

    the worker configuration



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/exekutor/configuration.rb', line 323

def worker_options
  {
    min_threads: min_execution_threads,
    max_threads: max_execution_threads,
    max_thread_idletime: max_execution_thread_idletime.to_f
  }.tap do |opts|
    opts[:set_db_connection_name] = set_db_connection_name? unless set_db_connection_name.nil?
    %i[enable_listener delete_completed_jobs delete_discarded_jobs delete_failed_jobs].each do |option|
      opts[option] = send(:"#{option}?") ? true : false
    end
    %i[polling_interval polling_jitter status_server_handler status_server_port healthcheck_timeout]
      .each do |option|
      opts[option] = send(option)
    end
  end
end