Class: SolidQueue::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_queue/configuration.rb

Constant Summary collapse

WORKER_DEFAULTS =
{
  queues: "*",
  threads: 3,
  processes: 1,
  polling_interval: 0.1
}
DISPATCHER_DEFAULTS =
{
  batch_size: 500,
  polling_interval: 1,
  concurrency_maintenance: true,
  concurrency_maintenance_interval: 600,
  recurring_tasks: []
}

Instance Method Summary collapse

Constructor Details

#initialize(mode: :work, load_from: nil) ⇒ Configuration

Returns a new instance of Configuration.



20
21
22
23
# File 'lib/solid_queue/configuration.rb', line 20

def initialize(mode: :work, load_from: nil)
  @mode = mode
  @raw_config = config_from(load_from)
end

Instance Method Details

#dispatchersObject



45
46
47
48
49
50
51
52
53
# File 'lib/solid_queue/configuration.rb', line 45

def dispatchers
  if mode.in? %i[ dispatch all]
    dispatchers_options.map do |dispatcher_options|
      recurring_tasks = parse_recurring_tasks dispatcher_options[:recurring_tasks]

      Dispatcher.new **dispatcher_options.merge(recurring_tasks: recurring_tasks).with_defaults(DISPATCHER_DEFAULTS)
    end
  end
end

#max_number_of_threadsObject



55
56
57
58
# File 'lib/solid_queue/configuration.rb', line 55

def max_number_of_threads
  # At most "threads" in each worker + 1 thread for the worker + 1 thread for the heartbeat task
  workers_options.map { |options| options[:threads] }.max + 2
end

#processesObject



25
26
27
28
29
30
31
32
# File 'lib/solid_queue/configuration.rb', line 25

def processes
  case mode
  when :dispatch then dispatchers
  when :work     then workers
  when :all      then dispatchers + workers
  else           raise "Invalid mode #{mode}"
  end
end

#workersObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/solid_queue/configuration.rb', line 34

def workers
  if mode.in? %i[ work all]
    workers_options.flat_map do |worker_options|
      processes = worker_options.fetch(:processes, WORKER_DEFAULTS[:processes])
      processes.times.map { Worker.new(**worker_options.with_defaults(WORKER_DEFAULTS)) }
    end
  else
    []
  end
end