class Honcho::Adapters::Base

Attributes

config[R]
redis[R]
runner[R]
running[R]
stopping[R]

Public Class Methods

new(config:, redis:, runner:) click to toggle source
# File lib/honcho/adapters/base.rb, line 4
def initialize(config:, redis:, runner:)
  @config = config
  @redis = redis
  @runner = runner
  @running = false
  @stopping = false
end

Public Instance Methods

check_for_work() click to toggle source
# File lib/honcho/adapters/base.rb, line 18
def check_for_work
  if run?
    start
  else
    stop
  end
end
commands() click to toggle source
# File lib/honcho/adapters/base.rb, line 30
def commands
  Array(config['command'])
end
name() click to toggle source
# File lib/honcho/adapters/base.rb, line 26
def name
  config['name']
end
path() click to toggle source
# File lib/honcho/adapters/base.rb, line 34
def path
  config['path']
end
really_stop() click to toggle source
# File lib/honcho/adapters/base.rb, line 82
def really_stop
  @stopping = false
  return unless running?
  log(name, "STOPPING\n")
  running.each do |(pid, wout)|
    Process.kill('-TERM', pid)
    wout.close
  end
  @running = false
end
run?() click to toggle source
# File lib/honcho/adapters/base.rb, line 38
def run?
  work_to_do? || work_being_done?
end
running?() click to toggle source
# File lib/honcho/adapters/base.rb, line 42
def running?
  @running != false
end
should_stop?() click to toggle source
# File lib/honcho/adapters/base.rb, line 78
def should_stop?
  stopping? && stopping <= 0
end
start() click to toggle source
# File lib/honcho/adapters/base.rb, line 50
def start
  @stopping = false
  return if running?
  log(name, "STARTING\n")
  @running = start_command
end
start_command() click to toggle source
# File lib/honcho/adapters/base.rb, line 57
def start_command
  commands.map do |cmd|
    rout, wout = IO.pipe
    pid = spawn(path, cmd, wout)
    Thread.new do
      log(name, rout.gets) until rout.eof?
    end
    [pid, wout]
  end
end
stop() click to toggle source
# File lib/honcho/adapters/base.rb, line 68
def stop
  return unless running?
  if should_stop?
    really_stop
  else
    @stopping ||= stop_delay
    @stopping -= interval
  end
end
stopping?() click to toggle source
# File lib/honcho/adapters/base.rb, line 46
def stopping?
  @stopping != false
end
total_count() click to toggle source
# File lib/honcho/adapters/base.rb, line 93
def total_count
  queued_count + busy_count
end
type() click to toggle source
# File lib/honcho/adapters/base.rb, line 14
def type
  self.class.name.split(':').last.downcase
end

Private Instance Methods

interval() click to toggle source
# File lib/honcho/adapters/base.rb, line 111
def interval
  runner.interval
end
log(*args) click to toggle source
# File lib/honcho/adapters/base.rb, line 99
def log(*args)
  runner.log(*args)
end
spawn(*args) click to toggle source
# File lib/honcho/adapters/base.rb, line 103
def spawn(*args)
  runner.spawn(*args)
end
stop_delay() click to toggle source
# File lib/honcho/adapters/base.rb, line 107
def stop_delay
  runner.stop_delay
end
work_being_done?() click to toggle source
# File lib/honcho/adapters/base.rb, line 119
def work_being_done?
  busy_count > 0
end
work_to_do?() click to toggle source
# File lib/honcho/adapters/base.rb, line 115
def work_to_do?
  queued_count > 0
end