module Kuroko2::Command::Executor

Constants

DEFAULT_NUM_WORKERS
NUM_SYSTEM_WORKERS

Public Class Methods

new() click to toggle source
# File lib/autoload/kuroko2/command/executor.rb, line 11
def initialize
  @stop = ServerEngine::BlockingFlag.new

  @hostname = ENV['HOSTNAME'] || Socket.gethostname
  @queue    = ENV['QUEUE'] || Execution::DEFAULT_QUEUE

  @command = if worker_id == 0
               Command::Kill.new(@hostname, worker_id)
             elsif worker_id == (Command::Executor.num_workers - 1)
               Command::Monitor.new(hostname: @hostname, worker_id: worker_id)
             else
               @worker = Worker.where(hostname: @hostname, worker_id: worker_id, queue: @queue).first_or_initialize
               @worker.update!(suspendable: true)
               Command::Shell.new(hostname: @hostname, worker_id: worker_id, worker: @worker, queue: @queue)
             end
end
num_workers() click to toggle source
# File lib/autoload/kuroko2/command/executor.rb, line 7
def self.num_workers
  @num_workers ||= (ENV['NUM_WORKERS'] || DEFAULT_NUM_WORKERS).to_i + NUM_SYSTEM_WORKERS
end

Public Instance Methods

run() click to toggle source
# File lib/autoload/kuroko2/command/executor.rb, line 28
def run
  Kuroko2.logger = logger
  Kuroko2.logger.info "[#{@hostname}-#{worker_id}] Start worker"
  toggle_worker_status(true)
  $0 = "command-executor (worker_id=#{worker_id} command=#{@command.class.name})"

  sleep worker_id

  until @stop.wait(1 + rand)
    @command.execute
  end
rescue Exception => e
  Kuroko2.logger.fatal("[#{@hostname}-#{worker_id}] #{e.class}: #{e.message}\n" +
    e.backtrace.map { |trace| "    #{trace}" }.join("\n"))

  raise e
end
stop() click to toggle source
# File lib/autoload/kuroko2/command/executor.rb, line 46
def stop
  Kuroko2.logger.info "[#{@hostname}-#{worker_id}] Stop worker"
  toggle_worker_status(false)

  @stop.set!
end

Private Instance Methods

toggle_worker_status(status) click to toggle source
# File lib/autoload/kuroko2/command/executor.rb, line 55
def toggle_worker_status(status)
  return false unless @command.kind_of?(Command::Shell)

  @worker.working = status
  @worker.save
end