Class: Cinch::Timer
Overview
It is possible to directly create instances of this class, but the referenced methods should suffice.
Timers are used for executing code in the future, either repeatedly or only once.
In Cinch, two ways for creating timers are available:
-
The first way is by declaring them for a plugin, in which case they will start as soon as the bot connects to a server.
-
The second way is to dynamically create new timers in response to user input. A common example for this is an alarm clock plugin, which has to execute at a specific time.
Instance Attribute Summary collapse
-
#block ⇒ Proc
readonly
-
#bot ⇒ Bot
readonly
-
#interval ⇒ Numeric
The interval (in seconds) of the timer.
-
#shots ⇒ Integer
The remaining number of shots before this timer will stop.
-
#started ⇒ Boolean
(also: #started?)
readonly
-
#thread_group ⇒ ThreadGroup
readonly
private
-
#threaded ⇒ Boolean
(also: #threaded?)
If true, each invocation will be executed in a thread of its own.
Instance Method Summary collapse
-
#initialize(bot, options, &block) ⇒ Timer
constructor
A new instance of Timer.
-
#start
Start the timer.
-
#stop
Stop the timer.
-
#stopped? ⇒ Boolean
-
#to_s ⇒ String
Methods included from Helpers
#Channel, #Format, #Sanitize, #Target, #Timer, #Unformat, #User, #debug, #error, #exception, #fatal, #incoming, #info, #log, #outgoing, #rescue_exception, sanitize, #warn
Constructor Details
#initialize(bot, options, &block) ⇒ Timer
Returns a new instance of Timer.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cinch/timer.rb', line 64 def initialize(bot, , &block) = {:threaded => true, :shots => Float::INFINITY, :start_automatically => true, :stop_automatically => true}.merge() @bot = bot @interval = [:interval].to_f @threaded = [:threaded] @orig_shots = [:shots] # Setting @shots here so the attr_reader won't return nil @shots = @orig_shots @block = block @started = false @thread_group = ThreadGroup.new if [:start_automatically] @bot.on :connect, //, self do |m, timer| timer.start end end if [:stop_automatically] @bot.on :disconnect, //, self do |m, timer| timer.stop end end end |
Instance Attribute Details
#block ⇒ Proc (readonly)
35 36 37 |
# File 'lib/cinch/timer.rb', line 35 def block @block end |
#interval ⇒ Numeric
Returns The interval (in seconds) of the timer.
28 29 30 |
# File 'lib/cinch/timer.rb', line 28 def interval @interval end |
#shots ⇒ Integer
Returns The remaining number of shots before this timer will stop. This value will automatically reset after restarting the timer.
43 44 45 |
# File 'lib/cinch/timer.rb', line 43 def shots @shots end |
#started ⇒ Boolean (readonly) Also known as: started?
38 39 40 |
# File 'lib/cinch/timer.rb', line 38 def started @started end |
#thread_group ⇒ ThreadGroup (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
49 50 51 |
# File 'lib/cinch/timer.rb', line 49 def thread_group @thread_group end |
#threaded ⇒ Boolean Also known as: threaded?
Returns If true, each invocation will be executed in a thread of its own.
32 33 34 |
# File 'lib/cinch/timer.rb', line 32 def threaded @threaded end |
Instance Method Details
#start
This method returns an undefined value.
Start the timer
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/cinch/timer.rb', line 99 def start return if @started @bot.loggers.debug "[timer] Starting timer #{self}" @shots = @orig_shots @thread_group.add Thread.new { while @shots > 0 do sleep @interval if threaded? Thread.new do rescue_exception do @block.call end end else rescue_exception do @block.call end end @shots -= 1 end } @started = true end |
#stop
This method returns an undefined value.
Stop the timer
131 132 133 134 135 136 137 138 |
# File 'lib/cinch/timer.rb', line 131 def stop return unless @started @bot.loggers.debug "[timer] Stopping timer #{self}" @thread_group.list.each { |thread| thread.kill } @started = false end |
#stopped? ⇒ Boolean
92 93 94 |
# File 'lib/cinch/timer.rb', line 92 def stopped? !@started end |
#to_s ⇒ String
141 142 143 |
# File 'lib/cinch/timer.rb', line 141 def to_s "<Cinch::Timer %s/%s shots, %ds interval, %sthreaded, %sstarted, block: %s>" % [@orig_shots - @shots, @orig_shots, @interval, @threaded ? "" : "not ", @started ? "" : "not ", @block] end |