module Zhong

Constants

VERSION

Attributes

heartbeat_key[W]
logger[W]
redis[W]
tz[RW]

Public Class Methods

all_heartbeats() click to toggle source
# File lib/zhong.rb, line 43
def self.all_heartbeats
  heartbeats = redis.hgetall(heartbeat_key)
  now = redis_time

  old_beats, new_beats = heartbeats.partition do |_, v|
    Time.at(v.to_i) < (now - 15.minutes)
  end

  redis.multi do
    old_beats.each { |b| redis.hdel(heartbeat_key, b) }
  end

  new_beats.map do |k, v|
    host, pid = k.split("#", 2)
    {host: host, pid: pid, last_seen: Time.at(v.to_i)}
  end
end
any_running?(grace = 60.seconds) click to toggle source
# File lib/zhong.rb, line 35
def self.any_running?(grace = 60.seconds)
  latest_heartbeat && latest_heartbeat > (redis_time - grace)
end
heartbeat_key() click to toggle source
# File lib/zhong.rb, line 71
def self.heartbeat_key
  @heartbeat_key ||= "zhong:heartbeat"
end
latest_heartbeat() click to toggle source
# File lib/zhong.rb, line 39
def self.latest_heartbeat
  all_heartbeats.map { |h| h[:last_seen] }.sort.last
end
logger() click to toggle source
# File lib/zhong.rb, line 61
def self.logger
  @logger ||= Logger.new(STDOUT).tap do |logger|
    logger.formatter = -> (_, datetime, _, msg) { "#{datetime}: #{msg}\n" }
  end
end
redis() click to toggle source
# File lib/zhong.rb, line 67
def self.redis
  @redis ||= Redis.new(url: ENV["REDIS_URL"])
end
schedule(&block) click to toggle source
# File lib/zhong.rb, line 27
def self.schedule(&block)
  scheduler.instance_eval(&block) if block_given?
end
scheduler() click to toggle source
# File lib/zhong.rb, line 31
def self.scheduler
  @scheduler ||= Scheduler.new
end