Class: Rufus::Scheduler::EmScheduler

Inherits:
SchedulerCore show all
Defined in:
lib/rufus/sc/scheduler.rb

Overview

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

Instance Attribute Summary

Attributes inherited from SchedulerCore

#options

Instance Method Summary collapse

Methods inherited from SchedulerCore

#all_jobs, #at, #cron, #cron_jobs, #do_handle_exception, #every, #find, #find_by_tag, #in, #jobs, #pause, #resume, #running_jobs, start_new, #trigger_threads, #unschedule, #unschedule_by_tag

Methods included from LegacyMethods

#at_job_count, #cron_job_count, #every_job_count, #find_jobs, #pending_job_count, #precision

Constructor Details

#initialize(opts = {}) ⇒ EmScheduler

Returns a new instance of EmScheduler.

Raises:

  • (LoadError)


489
490
491
492
493
494
495
496
# File 'lib/rufus/sc/scheduler.rb', line 489

def initialize(opts={})

  raise LoadError.new(
    'EventMachine missing, "require \'eventmachine\'" might help'
  ) unless defined?(EM)

  super(opts)
end

Instance Method Details

#joinObject

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



533
534
535
536
# File 'lib/rufus/sc/scheduler.rb', line 533

def join

  @em_thread.join if @em_thread
end

#startObject



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/rufus/sc/scheduler.rb', line 498

def start

  @em_thread = nil

  unless EM.reactor_running?
    @em_thread = Thread.new { EM.run }
    while (not EM.reactor_running?)
      Thread.pass
    end
  end

  #unless EM.reactor_running?
  #  t = Thread.current
  #  @em_thread = Thread.new { EM.run { t.wakeup } }
  #  Thread.stop # EM will wake us up when it's ready
  #end

  @timer = EM::PeriodicTimer.new(@frequency) { step }
end

#stop(opts = {}) ⇒ Object

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



523
524
525
526
527
528
# File 'lib/rufus/sc/scheduler.rb', line 523

def stop(opts={})

  @timer.cancel

  EM.stop if opts[:stop_em] and @em_thread
end