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 ⇒ void
Start the timer.
-
#stop ⇒ void
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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/cinch/timer.rb', line 66 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)
37 38 39 |
# File 'lib/cinch/timer.rb', line 37 def block @block end |
#interval ⇒ Numeric
Returns The interval (in seconds) of the timer.
30 31 32 |
# File 'lib/cinch/timer.rb', line 30 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.
45 46 47 |
# File 'lib/cinch/timer.rb', line 45 def shots @shots end |
#started ⇒ Boolean (readonly) Also known as: started?
40 41 42 |
# File 'lib/cinch/timer.rb', line 40 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.
51 52 53 |
# File 'lib/cinch/timer.rb', line 51 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.
34 35 36 |
# File 'lib/cinch/timer.rb', line 34 def threaded @threaded end |
Instance Method Details
#start ⇒ void
This method returns an undefined value.
Start the timer
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 127 128 |
# File 'lib/cinch/timer.rb', line 101 def start return if @started @bot.loggers.debug "[timer] Starting timer #{self}" @shots = @orig_shots @thread_group.add Thread.new { while @shots > 0 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 ⇒ void
This method returns an undefined value.
Stop the timer
133 134 135 136 137 138 139 140 |
# File 'lib/cinch/timer.rb', line 133 def stop return unless @started @bot.loggers.debug "[timer] Stopping timer #{self}" @thread_group.list.each { |thread| thread.kill } @started = false end |
#stopped? ⇒ Boolean
94 95 96 |
# File 'lib/cinch/timer.rb', line 94 def stopped? !@started end |
#to_s ⇒ String
143 144 145 |
# File 'lib/cinch/timer.rb', line 143 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 |