class Fasten::Worker

Attributes

child_read[RW]
child_write[RW]
name[RW]
parent_read[RW]
parent_write[RW]
runner[RW]
running_task[RW]
spinner[RW]

Public Class Methods

new(runner:, name: nil, use_threads: nil) click to toggle source
# File lib/fasten/worker.rb, line 23
def initialize(runner:, name: nil, use_threads: nil)
  if use_threads
    extend Fasten::Support::ThreadWorker
  else
    extend Fasten::Support::ForkWorker
  end

  self.runner = runner
  self.name = name
  self.spinner = 0

  initialize_logger(log_file: runner.log_file) if runner
end

Public Instance Methods

kind() click to toggle source
# File lib/fasten/worker.rb, line 47
def kind
  'worker'
end
perform(task) click to toggle source
# File lib/fasten/worker.rb, line 37
def perform(task)
  if task.ruby
    perform_ruby(task)
  elsif task.shell
    perform_shell(task)
  elsif task.block
    perform_block(task, task.block)
  end
end
to_s() click to toggle source
# File lib/fasten/worker.rb, line 51
def to_s
  name
end

Protected Instance Methods

perform_block(task, block) click to toggle source
# File lib/fasten/worker.rb, line 69
def perform_block(task, block)
  task.response = instance_exec task.request, &block
end
perform_ruby(task) click to toggle source
# File lib/fasten/worker.rb, line 57
def perform_ruby(task)
  task.response = eval task.ruby # rubocop:disable Security/Eval we trust our users ;-)
end
perform_shell(task) click to toggle source
# File lib/fasten/worker.rb, line 61
def perform_shell(task)
  shell_pid = spawn task.shell, out: @redirect_log, err: @redirect_log
  Process.wait shell_pid
  result = $CHILD_STATUS

  raise "Command failed with exit code: #{result.exitstatus}" unless result.exitstatus.zero?
end
perform_task(task) click to toggle source
# File lib/fasten/worker.rb, line 97
def perform_task(task)
  log_ini task, 'perform_task'

  perform(task)
  task.state = :DONE
rescue StandardError => e
  task.state = :FAIL
  task.error = WorkerError.new(e)
ensure
  log_fin task, 'perform_task'
  send_response_to_parent(task)
end
process_incoming_requests() click to toggle source
# File lib/fasten/worker.rb, line 73
def process_incoming_requests
  log_ini self, 'process_incoming_requests'

  while (object = receive_request_from_parent)
    run_task(object) if object.is_a? Fasten::Task
  end

  log_fin self, 'process_incoming_requests'
rescue EOFError
  log_info 'Terminating on EOF'
end
run_task(task) click to toggle source
# File lib/fasten/worker.rb, line 85
def run_task(task)
  log_ini task, 'run_task'
  redirect_std "#{runner.fasten_dir}/log/task/#{task.name}.log"

  perform_task task
ensure
  restore_std

  logger.reopen(log_file)
  log_fin task, 'run_task'
end