Class: Servolux::Threaded::ThreadContainer
- Inherits:
-
Struct
- Object
- Struct
- Servolux::Threaded::ThreadContainer
- Defined in:
- lib/servolux/threaded.rb
Instance Attribute Summary collapse
-
#continue_on_error ⇒ Object
Returns the value of attribute continue_on_error.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#iterations ⇒ Object
Returns the value of attribute iterations.
-
#maximum_iterations ⇒ Object
Returns the value of attribute maximum_iterations.
-
#running ⇒ Object
(also: #running?)
Returns the value of attribute running.
-
#thread ⇒ Object
Returns the value of attribute thread.
-
#use_strict_interval ⇒ Object
Returns the value of attribute use_strict_interval.
Instance Method Summary collapse
- #finished_iterations? ⇒ Boolean
- #join(limit = nil) ⇒ Object
-
#mark_time ⇒ Object
Mark the start time of the run loop.
- #run(threaded) ⇒ Object
-
#sleep ⇒ Object
Sleep for "interval" seconds adjusting for the run time of the "run" method if the "use_strict_interval" flag is set.
- #start(threaded) ⇒ Object
- #stop ⇒ Object
Instance Attribute Details
#continue_on_error ⇒ Object
Returns the value of attribute continue_on_error
227 228 229 |
# File 'lib/servolux/threaded.rb', line 227 def continue_on_error @continue_on_error end |
#interval ⇒ Object
Returns the value of attribute interval
227 228 229 |
# File 'lib/servolux/threaded.rb', line 227 def interval @interval end |
#iterations ⇒ Object
Returns the value of attribute iterations
227 228 229 |
# File 'lib/servolux/threaded.rb', line 227 def iterations @iterations end |
#maximum_iterations ⇒ Object
Returns the value of attribute maximum_iterations
227 228 229 |
# File 'lib/servolux/threaded.rb', line 227 def maximum_iterations @maximum_iterations end |
#running ⇒ Object Also known as: running?
Returns the value of attribute running
227 228 229 |
# File 'lib/servolux/threaded.rb', line 227 def running @running end |
#thread ⇒ Object
Returns the value of attribute thread
227 228 229 |
# File 'lib/servolux/threaded.rb', line 227 def thread @thread end |
#use_strict_interval ⇒ Object
Returns the value of attribute use_strict_interval
227 228 229 |
# File 'lib/servolux/threaded.rb', line 227 def use_strict_interval @use_strict_interval end |
Instance Method Details
#finished_iterations? ⇒ Boolean
278 279 280 281 |
# File 'lib/servolux/threaded.rb', line 278 def finished_iterations? return true if maximum_iterations and (iterations >= maximum_iterations) return false end |
#join(limit = nil) ⇒ Object
273 274 275 276 |
# File 'lib/servolux/threaded.rb', line 273 def join( limit = nil ) return if thread.nil? limit ? thread.join(limit) : thread.join end |
#mark_time ⇒ Object
Mark the start time of the run loop.
287 288 289 |
# File 'lib/servolux/threaded.rb', line 287 def mark_time @mark = Time.now if use_strict_interval end |
#run(threaded) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/servolux/threaded.rb', line 240 def run( threaded ) loop do begin mark_time break unless running? threaded.run if maximum_iterations self.iterations += 1 if finished_iterations? self.running = false break end end sleep if running? rescue SystemExit; raise rescue Exception => err if continue_on_error threaded.logger.error err else threaded.logger.fatal err raise err end end end ensure if threaded.respond_to?(:after_stopping) and !self.running threaded.after_stopping end self.running = false end |
#sleep ⇒ Object
Sleep for "interval" seconds adjusting for the run time of the "run" method if the "use_strict_interval" flag is set. If the run time of the "run" method exceeds our sleep "interval", then log a warning and just use the interval as normal for this sleep period.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/servolux/threaded.rb', line 296 def sleep time_to_sleep = interval if use_strict_interval and interval > 0 diff = Time.now - @mark time_to_sleep = interval - diff if time_to_sleep < 0 time_to_sleep = interval logger.warn "Run time [#{diff} s] exceeded strict interval [#{interval} s]" end end ::Kernel.sleep time_to_sleep end |
#start(threaded) ⇒ Object
228 229 230 231 232 233 |
# File 'lib/servolux/threaded.rb', line 228 def start( threaded ) self.running = true self.iterations = 0 self.thread = Thread.new { run threaded } Thread.pass end |
#stop ⇒ Object
235 236 237 238 |
# File 'lib/servolux/threaded.rb', line 235 def stop self.running = false thread.wakeup if thread.alive? end |