Class: Lita::Handlers::Cron

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/cron.rb

Constant Summary collapse

REDIS_KEY =
"cron"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robot) ⇒ Cron

Need to initialize previous jobs for redis when starting



19
20
21
22
# File 'lib/lita/handlers/cron.rb', line 19

def initialize(robot)
  super

end

Class Method Details

.default_config(config) ⇒ Object



24
25
# File 'lib/lita/handlers/cron.rb', line 24

def self.default_config(config)
end

Instance Method Details

#delete(response) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lita/handlers/cron.rb', line 89

def delete(response)
  if redis.hexists(REDIS_KEY, response.matches[0][0])
    job = JSON.parse(redis.hget(REDIS_KEY, response.matches[0][0]))
    log.info "DELETE: #{response.matches[0][0]}"

    Lita::Handlers.get_scheduler.unschedule(job["j_id"])
    redis.hdel(REDIS_KEY, response.matches[0][0]) >= 1
    response.reply("Deleted #{response.matches[0][0]}.")
  else
    response.reply("#{response.matches[0][0]} isn't an existing cron job.")
  end
end

#list(response) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lita/handlers/cron.rb', line 102

def list(response)
  log.info "LISTing all cron jobs"
  keys = redis.hgetall(REDIS_KEY)
  jobs = Lita::Handlers.get_scheduler.cron_jobs
  if jobs.empty?
    response.reply("No cron jobs currently running.")
  else
    keys.each do |k, v|
      j = JSON.parse v
      cron_line = [j['cron_line']]

      response.reply("#{k}=>#{cron_line}")
    end
  end
end

#load_on_start(payload) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lita/handlers/cron.rb', line 41

def load_on_start(payload)
  jobs = redis.hgetall(REDIS_KEY)
  jobs.each do |k,v|
    j = JSON.parse(v)
    begin
      job = Lita::Handlers.get_scheduler.cron j['cron_line'] do |job|
        target = Source.new(user: j['u_id'], room: j['room'])
        robot.send_messages(target, k)
        log.info "SENDING: #{k} -> #{target}"
      end

      log.info "Created cron job: #{j['cron_line']} #{k}."
    rescue ArgumentError => e
      response.reply "argument error, perhaps the cronline? #{e.message}"
    end
  end
end

#new(response) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lita/handlers/cron.rb', line 59

def new(response)
log.info "NEW: #{response.matches} from #{response.message.source.user.id} in #{response.message.source.room}"
  input = response.matches[0][0].split(" ")
  cron = input[0..4].join(" ")
  message = input[5..input.count()-1].join(" ")

  if(redis.hkeys(REDIS_KEY).include?(message))
    response.reply "#{message} already exists, delete first."
  else
    begin
      job = Lita::Handlers.get_scheduler.cron cron do |job|
        log.info("SENDING: #{message}")
        response.reply(message)
      end

      redis.hset(REDIS_KEY, message, {
        :cron_line => job.cron_line.original,
        :j_id => job.job_id,
        :u_id => response.message.source.user.id,
        :room => response.message.source.room }.to_json
      )
      jobs = Lita::Handlers.get_scheduler.cron_jobs

      response.reply("New cron job: #{cron} #{message}")
    rescue ArgumentError => e
      response.reply "argument error, perhaps the cronline? #{e.message}"
    end
  end
end