class BmcDaemonLib::WorkerBase

Constants

STATUS_CRASHED
STATUS_DOWN
STATUS_FINISHED
STATUS_READY
STATUS_SLEEPING
STATUS_STARTING

Statuses

STATUS_TIMEOUT
STATUS_WORKING

Attributes

pool[R]

Class options

wid[R]

Public Class Methods

new(wid, pool = nil) click to toggle source
# File lib/bmc-daemon-lib/worker.rb, line 19
def initialize wid, pool = nil
  # Logger
  log_pipe :workers
  @log_worker_status_changes = true

  # Configuration
  @config = {}

  # Set thread context
  Thread.current.thread_variable_set :wid, (@wid = wid)
  Thread.current.thread_variable_set :pool, (@pool = pool)
  Thread.current.thread_variable_set :started_at, Time.now
  worker_status STATUS_STARTING

  # Ask worker to init itself, and return if there are errors
  if worker_init_result = worker_init
    log_warn "aborting: #{worker_init_result.inspect}", @config
  else
    # We're ok, let's start out loop
    start_loop
  end
end

Protected Instance Methods

config_section(key) click to toggle source
# File lib/bmc-daemon-lib/worker.rb, line 97
def config_section key
  # Debugging
  @log_worker_status_changes = @debug

  # Set my configuration
  if (Conf[key].is_a? Hash) && Conf[key]
    @config = Conf[key]
  else
    log_error "missing [#{key}] configuration"
  end
end
start_loop() click to toggle source
# File lib/bmc-daemon-lib/worker.rb, line 61
def start_loop
  log_info "worker loop starting", @config
  loop do
    begin
      # Announce we're waiting for work
      worker_status STATUS_READY

      # Do the hard work
      worker_process

      # Should we sleep ?
      worker_sleep @config[:timer]

    rescue StandardError => e
      log_error "WORKER EXCEPTION: #{e.inspect}", e.backtrace
      sleep 1
    end
  end
end
worker_after() click to toggle source
# File lib/bmc-daemon-lib/worker.rb, line 47
def worker_after
end
worker_config() click to toggle source
# File lib/bmc-daemon-lib/worker.rb, line 51
def worker_config
end
worker_init() click to toggle source

Worker methods prototypes

# File lib/bmc-daemon-lib/worker.rb, line 45
def worker_init
end
worker_process() click to toggle source
# File lib/bmc-daemon-lib/worker.rb, line 49
def worker_process
end
worker_sleep(seconds) click to toggle source
# File lib/bmc-daemon-lib/worker.rb, line 54
def worker_sleep seconds
  return if seconds.nil? || seconds.to_f == 0.0
  worker_status STATUS_SLEEPING
  # log_debug "worker_sleep: #{seconds}"
  sleep(seconds)
end
worker_status(status) click to toggle source
# File lib/bmc-daemon-lib/worker.rb, line 81
def worker_status status
  # Update thread variables
  Thread.current.thread_variable_set :status, status
  Thread.current.thread_variable_set :updated_at, Time.now

  # Nothin' to log if "silent"
  return unless @log_worker_status_changes

  # Log this status change
  # if defined?'Job' && job.is_a?(Job)
  #   log_info "status [#{status}] on job[#{job.id}] status[#{job.status}] error[#{job.error}]"
  # else
  log_info "status [#{status}]"
  # end
end