Sha256: a06473e28f831dec9831c9112a6f9b72d69a1989e9ddf085d9fa9b19197defc1

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require "daemons"

module Healthety
  extend self

  def workers(&block)
    @workers = []
    @threads = []

    instance_eval(&block)

    # Try to daemonize only if a command-line argument is given.
    if ARGV.any?
      Daemons.run_proc("healthety") { start }
    else
      puts message
      start
    end
  end

  def host(host)
    @host = host
  end

  def port(port)
    @port = port
  end

  def worker(name, &block)
    @workers << Worker.new(name, &block)
  end

  # Makes the methods defined in the helper modules given in `extensions`
  # available to the workers.
  def helpers(*extensions)
    Worker.class_eval { include(*extensions) } if extensions.any?
  end

  def start
    transmission = Transmission.new(@host, @port)

    # Catch Ctrl-C and terminate all worker threads.
    trap("INT") { @threads.map(&:kill) }

    @workers.each do |worker|
      @threads << Thread.new do
        loop do
          worker.perform
          transmission.send(worker.name, worker.value)
          sleep worker.interval
        end
      end
    end

    @threads.map(&:join)
  end

  def message
    <<-EOM
=> Workers sending to #{@host}:#{@port}
=> Ctrl-C to stop
EOM
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
healthety-0.0.7 lib/healthety/healthety.rb