Class: Botolo::Bot::Engine
- Inherits:
-
Object
- Object
- Botolo::Bot::Engine
- Defined in:
- lib/botolo/bot/engine.rb
Instance Method Summary collapse
- #calc_sleep_time(task) ⇒ Object
- #infinite_loop ⇒ Object
-
#initialize(options = {}) ⇒ Engine
constructor
A new instance of Engine.
- #logfile ⇒ Object
- #name ⇒ Object
- #online? ⇒ Boolean
- #read_conf(filename = nil) ⇒ Object
- #run ⇒ Object
- #start_task(name, sleep) ⇒ Object
- #stop ⇒ Object
- #uptime ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Engine
Returns a new instance of Engine.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/botolo/bot/engine.rb', line 7 def initialize(={}) @start_time = Time.now @online = false @config = read_conf([:config]) $twitter_api = nil behaviour_path = File.dirname([:config]) if @config['twitter']['enabled'] $twitter_api = Botolo::API::Tweet.instance $twitter_api.authenticate(@config['twitter']) @online = true unless $twitter_api.twitters.empty? end @tasks = @config['task'] @task_pids = [] behaviour = File.join(behaviour_path, @config['bot']['behaviour']) unless @config['bot']['behaviour'].nil? $logger.helo name, version $logger.filename = File.join(".", logfile) unless logfile.nil? $logger.info "#{@tasks.size} tasks loaded" begin load behaviour $logger.info "using #{behaviour} as bot behaviour" @behaviour = Botolo::Bot::Behaviour.new(@config) @start_time = Time.now rescue => e $logger.err(e.) require 'botolo/bot/behaviour' $logger.info "reverting to default dummy behaviour" @behaviour = Botolo::Bot::Behaviour.new(@config) @start_time = Time.now end end |
Instance Method Details
#calc_sleep_time(task) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/botolo/bot/engine.rb', line 45 def calc_sleep_time(task) s = /every (\d) (s|m|h|d|w|y)/.match task return 300 if s.nil? # safe fallback is 5 minutes sleeping return s[1].to_i if s[2] == 's' return s[1].to_i * 60 if s[2] == 'm' return s[1].to_i * 60 * 60 if s[2] == 'h' return s[1].to_i * 60 * 60 * 24 if s[2] == 'd' return s[1].to_i * 60 * 60 * 24 * 7 if s[2] == 'w' return s[1].to_i * 60 * 60 * 24 * 7 * 52 if s[2] == 'y' end |
#infinite_loop ⇒ Object
68 69 70 71 72 73 |
# File 'lib/botolo/bot/engine.rb', line 68 def infinite_loop loop do sleep(3600) # => 1 h $logger.info " --- mark --- (bot: #{@behaviour.name}, uptime: #{uptime})" end end |
#logfile ⇒ Object
117 118 119 |
# File 'lib/botolo/bot/engine.rb', line 117 def logfile return @config['bot']['logfile'] end |
#name ⇒ Object
121 122 123 |
# File 'lib/botolo/bot/engine.rb', line 121 def name return @config['bot']['name'] end |
#online? ⇒ Boolean
113 114 115 |
# File 'lib/botolo/bot/engine.rb', line 113 def online? @online end |
#read_conf(filename = nil) ⇒ Object
132 133 134 135 |
# File 'lib/botolo/bot/engine.rb', line 132 def read_conf(filename=nil) return {} if filename.nil? or ! File.exist?(filename) return YAML.load_file(filename) end |
#run ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/botolo/bot/engine.rb', line 59 def run $logger.info "entering main loop" @tasks.each do |task| @task_pids << Thread.start do start_task(task['action'], task['schedule']) end end end |
#start_task(name, sleep) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/botolo/bot/engine.rb', line 89 def start_task(name, sleep) while true begin @behaviour.send(name.to_sym) if @behaviour.respond_to? name.to_sym rescue => e $logger.err "#{name} failed (#{e.})" end sleep calc_sleep_time(sleep) end end |
#stop ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/botolo/bot/engine.rb', line 101 def stop $logger.info "shutting down threads" @task_pids.each do |pid| Thread.kill(pid) sleep 0.5 $logger.info "pid #{pid} killed" if ! pid.alive? $logger.err "pid #{pid} not killed" if pid.alive? end true end |
#uptime ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/botolo/bot/engine.rb', line 75 def uptime seconds_diff = (Time.now - @start_time).to_i.abs days = seconds_diff / 86400 seconds_diff -= days * 86400 hours = seconds_diff / 3600 seconds_diff -= hours * 3600 minutes = seconds_diff / 60 "#{days.to_s} days, #{hours.to_s.rjust(2, '0')}:#{minutes.to_s.rjust(2, '0')}" end |
#version ⇒ Object
124 125 126 |
# File 'lib/botolo/bot/engine.rb', line 124 def version return @config['bot']['version'] end |