class Utils::ProbeServer

def cmd(job)

def cmd(job)
  call = []
  if ENV.key?('BUNDLE_GEMFILE') and bundle = `which bundle`.full?(:chomp)
    call << bundle << 'exec'
  end
  call.push($0, *job)
  output_message "Executing #{call.inspect} now.", :type => :info
  call
end

def docs

def docs
  annotations = self.class.doc_annotations.sort_by(&:first)
  max_size = annotations.map { |a| a.first.size }.max
  annotations.map { |n, v| "#{n.to_s.ljust(max_size + 1)}#{v}" }
end

def env

def env
  ENV
end

def history_clear

def history_clear
  @history = []
  true
end

def history_list

def history_list
  @history.dup
end

def initialize

def initialize
  @history    = [].freeze
  @jobs_queue = Queue.new
  @current_job_id = 0
  Thread.new { work_loop }
end

def job

def job
  queue_synchronize do
    @job
  end
end

def job_continue

def job_continue
  @pid and Process.kill :CONT, @pid
end

def job_enqueue(job_args)

def job_enqueue(job_args)
  job = Job.new(self, job_args)
  output_message "#{job.inspect} enqueued."
  @jobs_queue.push job
end

def job_repeat(job_id)

def job_repeat(job_id)
  Job === job_id and job_id = job.id
  if old_job = @history.find { |job| job.id == job_id }
    job_enqueue old_job.args
    true
  else
    false
  end
end

def job_stop

def job_stop
  @pid and Process.kill :STOP, @pid
end

def jobs_clear

def jobs_clear
  queue_synchronize do
    unless @jobs_queue.empty?
      @jobs_queue.clear
      output_message "Cleared all queued jobs.", :type => :warn
      true
    else
      false
    end
  end
end

def jobs_list

def jobs_list
  @jobs_queue.instance_variable_get(:@que).dup
end

def next_job_id

def next_job_id
  @current_job_id += 1
end

def output_message(msg, opts = { :type => :info })

def output_message(msg, opts = { :type => :info })
  msg =
    case opts[:type]
    when :success
      msg.on_green.black
    when :info, nil
      msg.on_color(118).black
    when :warn
      msg.on_color(166).black
    when :failure
      msg.on_red.blink.white
    end
  STDOUT.puts msg
  STDOUT.flush
end

def queue_synchronize(&block)

def queue_synchronize(&block)
  @jobs_queue.instance_variable_get(:@mutex).synchronize(&block)
end

def run_job(job)

def run_job(job)
  output_message "#{job.inspect} about to run now.", :type => :info
  @pid = fork { exec(*cmd(job.args)) }
  output_message "#{job.inspect} now running with pid #@pid.", :type => :info
  Process.wait @pid
  message = "#{job.inspect} was just run"
  if $?.success?
    job.ok = true
    message << " successfully."
    output_message message, :type => :success
  else
    job.ok = false
    message << " and failed with exit status #{$?.exitstatus}!"
    output_message message, :type => :failure
  end
  @history += [ @job.freeze ]
  @history.freeze
  @job = nil
end

def server_shutdown

def server_shutdown
  output_message "Server was shutdown down – HARD!", :type => :warn
  exit! 23
end

def work_loop

def work_loop
  loop do
    @job = @jobs_queue.shift
    run_job @job
  end
end