class Qbot::Cron

Private Class Methods

timers() click to toggle source
# File lib/qbot/embed/cron.rb, line 21
def self.timers
  @timers ||= {}
end

Private Instance Methods

format(id, cron, text) click to toggle source
# File lib/qbot/embed/cron.rb, line 62
def format(id, cron, text)
  "#{id.to_s.rjust(3, '0')}: #{cron} #{text}"
end
list_all() click to toggle source
# File lib/qbot/embed/cron.rb, line 40
def list_all
  resp = StringIO.new
  cache.each do |id, (cron, text)|
    next unless cron && text
    resp.puts format(id, cron, text)
  end

  resp.string.chomp
end
schedule(id, cron, text) click to toggle source
# File lib/qbot/embed/cron.rb, line 51
def schedule(id, cron, text)
  parser  = CronParser.new(cron)
  current = Time.now
  delay   = parser.next(current) - current

  Cron.timers[id] = Qbot.app.timers.after(delay) do
    post(text)
    schedule(id, cron, text)
  end
end
start(id, cron, text) click to toggle source
# File lib/qbot/embed/cron.rb, line 25
def start(id, cron, text)
  schedule(id, cron, text) rescue return

  cache[id] = [cron, text]
  "ADD #{format(id, cron, text)}"
end
stop(id) click to toggle source
# File lib/qbot/embed/cron.rb, line 32
def stop(id)
  Cron.timers[id].cancel if Cron.timers[id]
  cron, text = cache[id]

  cache[id] = nil
  "DEL #{format(id, cron, text)}"
end
unique_id() click to toggle source
# File lib/qbot/embed/cron.rb, line 66
def unique_id
  loop do
    id = (0..999).to_a.sample
    return id unless cache[id]
  end
end