Class: OmniBot::PeriodicCommand

Inherits:
Object
  • Object
show all
Includes:
LoggedCommand
Defined in:
lib/omnibot/periodiccommand.rb

Overview

Send to jabber user result of a daily command

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggedCommand

#jabber_logged_command, #set_jabber_messenger

Constructor Details

#initialize(command) ⇒ PeriodicCommand

Returns a new instance of PeriodicCommand.



28
29
30
31
32
33
# File 'lib/omnibot/periodiccommand.rb', line 28

def initialize command 
	@command = command
	@startup_pause = 0

	raise 'Wrong command' if (@command or '') == ''
end

Instance Attribute Details

#startup_pause=(value) ⇒ Object (writeonly)

Sets the attribute startup_pause

Parameters:

  • value

    the value to set the attribute startup_pause to.



26
27
28
# File 'lib/omnibot/periodiccommand.rb', line 26

def startup_pause=(value)
  @startup_pause = value
end

#timer_provider=(value) ⇒ Object (writeonly)

Sets the attribute timer_provider

Parameters:

  • value

    the value to set the attribute timer_provider to.



25
26
27
# File 'lib/omnibot/periodiccommand.rb', line 25

def timer_provider=(value)
  @timer_provider = value
end

Instance Method Details

#on_first_timerObject



10
11
12
13
# File 'lib/omnibot/periodiccommand.rb', line 10

def on_first_timer
	on_periodic_timer
	@timer_provider.add_periodic_timer(24*3600) { on_periodic_timer }
end

#on_periodic_timerObject



15
16
17
18
19
20
21
22
# File 'lib/omnibot/periodiccommand.rb', line 15

def on_periodic_timer
	begin
		OmniLog::info "Reporting command #{@command}"
		jabber_logged_command 'Periodic command', @command
	rescue => e
		OmniLog::error "PeriodicCommand error: #{e.message}\ntrace:\n#{Helpers::backtrace e}"
	end
end

#startObject



39
40
41
42
43
44
45
# File 'lib/omnibot/periodiccommand.rb', line 39

def start
	now = Time.now
	tomorrow = DateTime.now+1
	next_report_time = Time.local(tomorrow.year, tomorrow.month, tomorrow.day, 1, 0, 0)
	next_report_time = next_report_time + @startup_pause
	@timer_provider.add_timer(next_report_time - now) { on_first_timer }
end

#to_sObject



35
36
37
# File 'lib/omnibot/periodiccommand.rb', line 35

def to_s
	"Periodic command '#{@command}'"
end