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, #every, #find_by_tag, #handle_exception, #in, #jobs, start_new, #trigger_threads, #unschedule

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)


410
411
412
413
414
415
416
417
# File 'lib/rufus/sc/scheduler.rb', line 410

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.



454
455
456
457
# File 'lib/rufus/sc/scheduler.rb', line 454

def join

  @em_thread.join if @em_thread
end

#startObject



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/rufus/sc/scheduler.rb', line 419

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



444
445
446
447
448
449
# File 'lib/rufus/sc/scheduler.rb', line 444

def stop (opts={})

  @timer.cancel

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