Rufus::Scheduler::EmScheduler

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

Public Class Methods

new(opts={}) click to toggle source
     # File lib/rufus/sc/scheduler.rb, line 410
410:     def initialize(opts={})
411: 
412:       raise LoadError.new(
413:         'EventMachine missing, "require \eventmachine\" might help'
414:       ) unless defined?(EM)
415: 
416:       super(opts)
417:     end

Public Instance Methods

join() click to toggle source

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

     # File lib/rufus/sc/scheduler.rb, line 454
454:     def join
455: 
456:       @em_thread.join if @em_thread
457:     end
start() click to toggle source
     # File lib/rufus/sc/scheduler.rb, line 419
419:     def start
420: 
421:       @em_thread = nil
422: 
423:       unless EM.reactor_running?
424:         @em_thread = Thread.new { EM.run }
425:         while (not EM.reactor_running?)
426:           Thread.pass
427:         end
428:       end
429: 
430:       #unless EM.reactor_running?
431:       #  t = Thread.current
432:       #  @em_thread = Thread.new { EM.run { t.wakeup } }
433:       #  Thread.stop # EM will wake us up when it's ready
434:       #end
435: 
436:       @timer = EM::PeriodicTimer.new(@frequency) { step }
437:     end
stop(opts={}) click to toggle source

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 !).

     # File lib/rufus/sc/scheduler.rb, line 444
444:     def stop(opts={})
445: 
446:       @timer.cancel
447: 
448:       EM.stop if opts[:stop_em] and @em_thread
449:     end

Protected Instance Methods

trigger_job(blocking, &block) click to toggle source

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).

     # File lib/rufus/sc/scheduler.rb, line 464
464:     def trigger_job(blocking, &block)
465: 
466:       m = blocking ? :next_tick : :defer
467:         #
468:         # :next_tick monopolizes the EM
469:         # :defer executes its block in another thread
470: 
471:       EM.send(m) { block.call }
472:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.