Class Rufus::Scheduler::EmScheduler

  1. lib/rufus/sc/scheduler.rb

A rufus-scheduler that uses an EventMachine periodic timer instead of a loop.

Methods

public class

  1. new

public instance

  1. join
  2. start
  3. stop

protected instance

  1. trigger_job

Public class methods

new (opts={})
[show source]
     # File lib/rufus/sc/scheduler.rb, line 383
383:     def initialize (opts={})
384: 
385:       raise LoadError.new(
386:         'EventMachine missing, "require \'eventmachine\'" might help'
387:       ) unless defined?(EM)
388: 
389:       super
390:     end

Public instance methods

join ()

Joins this scheduler. Will actually join it only if it started the underlying EventMachine.

[show source]
     # File lib/rufus/sc/scheduler.rb, line 427
427:     def join
428: 
429:       @em_thread.join if @em_thread
430:     end
start ()
[show source]
     # File lib/rufus/sc/scheduler.rb, line 392
392:     def start
393: 
394:       @em_thread = nil
395: 
396:       unless EM.reactor_running?
397:         @em_thread = Thread.new { EM.run }
398:         while (not EM.reactor_running?)
399:           Thread.pass
400:         end
401:       end
402: 
403:       #unless EM.reactor_running?
404:       #  t = Thread.current
405:       #  @em_thread = Thread.new { EM.run { t.wakeup } }
406:       #  Thread.stop # EM will wake us up when it's ready
407:       #end
408: 
409:       @timer = EM::PeriodicTimer.new(@frequency) { step }
410:     end
stop (opts={})

Stops the scheduler.

If the :stop_em option is passed and set to true, it will stop the EventMachine (but only if it started the EM by itself !).

[show source]
     # File lib/rufus/sc/scheduler.rb, line 417
417:     def stop (opts={})
418: 
419:       @timer.cancel
420: 
421:       EM.stop if opts[:stop_em] and @em_thread
422:     end

Protected instance methods

trigger_job (blocking, &block)

If ‘blocking’ is set to true, the block will get called at the ‘next_tick’. Else the block will get called via ‘defer’ (own thread).

[show source]
     # File lib/rufus/sc/scheduler.rb, line 437
437:     def trigger_job (blocking, &block)
438: 
439:       m = blocking ? :next_tick : :defer
440:         #
441:         # :next_tick monopolizes the EM
442:         # :defer executes its block in another thread
443: 
444:       EM.send(m) { block.call }
445:     end