Module: Barbeque::Worker

Defined in:
lib/barbeque/worker.rb

Defined Under Namespace

Classes: UnexpectedMessageType

Constant Summary collapse

DEFAULT_QUEUE =
ENV['BARBEQUE_DEFAULT_QUEUE'] || 'default'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(worker_type: 'process', workers: 4, daemonize: false, log: $stdout, log_level: :info, pid_path: '/tmp/barbeque_worker.pid', supervisor: true) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/barbeque/worker.rb', line 13

def self.run(
  worker_type: 'process',
  workers:     4,
  daemonize:   false,
  log:         $stdout,
  log_level:   :info,
  pid_path:    '/tmp/barbeque_worker.pid',
  supervisor:  true
)
  options = {
    worker_type: worker_type,
    workers:     workers,
    daemonize:   daemonize,
    log:         log,
    log_level:   log_level,
    pid_path:    pid_path,
    supervisor:  supervisor,
  }

  worker = ServerEngine.create(nil, Barbeque::Worker, options)
  worker.run
end

Instance Method Details

#execute_commandObject



68
69
70
# File 'lib/barbeque/worker.rb', line 68

def execute_command
  @command.run
end

#initializeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/barbeque/worker.rb', line 36

def initialize
  queue_name = ENV['BARBEQUE_QUEUE'] || DEFAULT_QUEUE
  queue      = Barbeque::JobQueue.find_by!(name: queue_name)

  @command =
    case worker_id
    when 0
      ExecutionPoller.new(queue)
    when 1
      RetryPoller.new(queue)
    else
      Runner.new(queue)
    end
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/barbeque/worker.rb', line 51

def run
  $0 = "barbeque-worker (worker_id=#{worker_id} command=#{@command.class.name})"
  until @stop
    begin
      execute_command
    rescue => e
      Barbeque::ExceptionHandler.handle_exception(e)
    end
    Barbeque::ExceptionHandler.clear_context
  end
end

#stopObject



63
64
65
66
# File 'lib/barbeque/worker.rb', line 63

def stop
  @stop = true
  @command.stop
end