class LoneWolf::Worker

Public Class Methods

new(**options) click to toggle source
# File lib/lone_wolf.rb, line 6
def initialize(**options)
  loop! if options[:loop]
  @job = options[:job] if options[:job]
  @input_stream = Slipstream.create
  @output_stream = Slipstream.create
  if options[:start]
    started = start! 
    warn "Unable to start worker!" unless started
  end
end

Public Instance Methods

input() click to toggle source
# File lib/lone_wolf.rb, line 71
def input
  @input_stream
end
job(&block) click to toggle source
# File lib/lone_wolf.rb, line 27
def job(&block)
  return @job unless block_given?
  warn "Specifying the job now won't work unless you restart the worker!" if @pid
  @job = block
end
job=(prc) click to toggle source
# File lib/lone_wolf.rb, line 22
def job=(prc)
  warn "Specifying the job now won't work unless you restart the worker!" if @pid
  @job = prc
end
job?() click to toggle source
# File lib/lone_wolf.rb, line 17
def job?
  return true if @job
  false
end
kill!() click to toggle source
# File lib/lone_wolf.rb, line 79
def kill!
  return false if @pid.nil?
  @pid = nil if Process.kill('KILL', @pid) == 1
  return true if @pid.nil?
  false
end
killed?() click to toggle source
# File lib/lone_wolf.rb, line 86
def killed?
  return true if @pid.nil?
  false
end
loop!() click to toggle source
# File lib/lone_wolf.rb, line 33
def loop!
  warn "Specifying the loop now won't work unless you restart the worker!" if @pid
  @loop = true
end
loop?() click to toggle source
# File lib/lone_wolf.rb, line 38
def loop?
  return true if @loop
  false
end
output() click to toggle source
# File lib/lone_wolf.rb, line 75
def output
  @output_stream
end
pid() click to toggle source
# File lib/lone_wolf.rb, line 91
def pid
  @pid
end
pid?() click to toggle source
# File lib/lone_wolf.rb, line 95
def pid?
  return true if @pid
  false
end
restart!() click to toggle source
# File lib/lone_wolf.rb, line 66
def restart!
  kill!
  start!
end
start!() click to toggle source
# File lib/lone_wolf.rb, line 43
def start!
  return false if @pid
  return false unless @job
  @pid = fork do
    trap "SIGINT" do
      exit 0
    end
    if loop?
      while true do
        iput = input.read
        next if iput.nil?
        iput = iput.strip
        next if iput.empty?
        result = @job.call(iput)
        output.write result
      end
    else
      output.write @job.call(input.read)
    end
  end
  return true 
end