class Robut::Plugin::Cron

Public Instance Methods

create(cron, message) click to toggle source
# File lib/robut-cron.rb, line 39
def create(cron, message)
  begin
    scheduler = Rufus::Scheduler.start_new
    job = scheduler.cron cron do |job|

      if @@skeds.has_value? job.t
        connection = self.connection
        reply(message)
      else
        job.unschedule
      end

    end
    @@skeds[message] = job.t
    store['jobs'] ||= []
    store['jobs'] = @@skeds
    reply "new cron job with id: #{message}"
  rescue ArgumentError
    reply "argument error, perhaps the cronline?"
  end
end
handle(time, sender_nick, message) click to toggle source
# File lib/robut-cron.rb, line 14
def handle(time, sender_nick, message)
  if sent_to_me?(message) && !message.include?(" - ") && !message.include?("help")
    # Need to separate the cron schedule from the message
    # @bo cron '* * * * *' message
                  
    a = message.split(" ")
    if a[1] == "cron"
      command = a[2]
      
      case command
      when "new"  
        create(a[3..7].join(" "), a[8..a.count()-1].join(" "))
      when "list"
        reply list
      when "stop"
        reply stop(a[3..a.count()-1].join(" "))
      when "load"
        load_from_file(a[3..a.count()-1].join(" "))
      else 
        reply "i don't understand that cron command"
      end
    end       
  end
end
list() click to toggle source
# File lib/robut-cron.rb, line 61
def list()
  if @@skeds.empty?
    "no jobs!"
  else
    r = ""
    store['jobs'].each do |k,v|
      r = r + "[#{v}:#{k}] "
    end
    r
  end
end
load_from_file(file) click to toggle source

load the jobs from the .robut store it’s a bit of a chicken/egg prob so we’ll create if it doesn’t exist and leave it empty.

# File lib/robut-cron.rb, line 88
def load_from_file(file)
  if !File.exist? file
    reply "file does not exist"
  else
    begin
      s = YAML.load_file(file)
      if s
        s['jobs'].each do |key, val|
          create val,key
        end
      else
        reply "not a valid robut config file"
      end
    rescue
      reply "not a valid robut config file"
    end
  end
end
stop(id) click to toggle source
# File lib/robut-cron.rb, line 73
def stop(id)
  if @@skeds.has_key?(id)
    @@skeds.delete(id)
    store['jobs'] ||= []
    store['jobs'] = @@skeds
    "stopping #{id}"
  else
    "invalid id: #{id}, can't stop that"
  end
end
usage() click to toggle source

Returns a description of how to use this plugin

# File lib/robut-cron.rb, line 9
def usage
  ["#{at_nick} cron new * * * * * <message> - will send this message on the cron schedule",
   "#{at_nick} cron list - will return the list of job ids",
   "#{at_nick} cron stop <id> - will remove a job"]
end