module Zscheduler

Constants

VERSION

Public Class Methods

add_shutdown_hook(&block) click to toggle source

Add a new shutdown hook @example

Zscheduler.add_shutdown_hook do
  puts "someone called to Zscheduler.stop"
end
Zscheduler.stop
# File lib/zscheduler.rb, line 111
def add_shutdown_hook(&block)
  shutdown_hooks.push block
end
every(frequency,options = {}, &block) click to toggle source

Start new scheduler @param [Hash] options @option options [True,False] :now (false) execute the block now @option options [True,False] :on_shutdown (false) execute the block on shutdown @option options [True,False] :on_thread (false) execute the block on a separated thread @option options [Time] :start_at (nil) start the scheduler in a given time @option options [Time] :start_in (nil) start the scheduler in a given delay ( seconds ) @return [Timer] EventMachine timer wrapper @example

# Simple timer
Zscheduler.every(10) do
  puts "Running every 10 seconds"
end

# Run the block and then start the scheduler
Zscheduler.every(10,now: true) do
  puts "Running every 10 seconds"
end

# Run the block every 10 seconds and on shutdown
Zscheduler.every(10,on_shutdown: true) do
  puts "Running every 10 seconds and on shutdown"
end

# Start the scheduler in a given time
Zscheduler.every(10,start_at: Time.now + 5) do
  puts "Will run 5 seconds from now and then for every 10 seconds"
end

Zscheduler.every(10,start_in: 5) do
  puts "Will run 5 seconds from now and then for every 10 seconds"
end

# Run the block on a separated thread
Zscheduler.every(10,on_thread: true) do
  puts "I'm running on a separated thread"
end
# File lib/zscheduler.rb, line 52
def every(frequency,options = {}, &block)
  block_given? or raise ArgumentError, "no block was given..."
  block = wrap block
  start_reactor

  add_shutdown_hook(&block) if options[:on_shutdown]
  block.call if options[:immediately] || options[:now]

  options[:start_in] = (options[:start_at] - Time.now) if options[:start_at]

  action = proc { options[:on_thread] ? Thread.new(&block) : block.call }
  periodic = proc { EM::PeriodicTimer.new(frequency.to_i,&action) }

  obj = Timer.new
  obj.timer = if options[:start_in]
    EM::Timer.new(options[:start_in]) do
      action.call
      obj.timer = periodic.call
    end
  else
    periodic.call
  end

  timers.push obj
  obj
end
init_reactor?() click to toggle source
# File lib/zscheduler.rb, line 120
def init_reactor?
  !!wrapper
end
join() click to toggle source

Sleep until Zscheduler stops

# File lib/zscheduler.rb, line 116
def join
  (wrapper or EM.reactor_thread).join
end
once(seconds, &block) click to toggle source

Run callback once @param [Time,Integer] seconds @example

Zscheduler.once(Time.now + 10) do
  puts "I'm running 10 seconds from now"
end

# Same as above
Zscheduler.once(10) do
  puts "I'm running 10 seconds from now"
end
# File lib/zscheduler.rb, line 90
def once(seconds, &block)
  start_reactor
  seconds = (seconds - Time.now) if seconds.kind_of?(Time)
  timers.push(Timer.new(EM::Timer.new(seconds.to_i,&wrap(block)))).last
end
shutdown()
Alias for: stop
stop() click to toggle source

Stop the scheduler, cancel all timers and run all the shutdown hooks

# File lib/zscheduler.rb, line 97
def stop
  timers.each(&:cancel)
  shutdown_hooks.each(&:call)
  wrapper and EM.reactor_running? and EM.stop
end
Also aliased as: shutdown

Private Class Methods

shutdown_hooks() click to toggle source
# File lib/zscheduler.rb, line 154
def shutdown_hooks
  @shutdown_hooks ||= []
end
start_reactor() click to toggle source
# File lib/zscheduler.rb, line 139
def start_reactor
  return if EM.reactor_running?
  @wrapper = Thread.new(&EM.method(:run))
  wrapper.abort_on_exception = true
  Thread.pass until EM.reactor_running?
end
timers() click to toggle source
# File lib/zscheduler.rb, line 150
def timers
  @timers ||= []
end
wrap(block) click to toggle source
# File lib/zscheduler.rb, line 126
def wrap block
  proc do
    begin
      block.call
    rescue Exception => e
      init_reactor? ?
        raise :
        STDERR.puts("[ZScheduler][#{block.source_location.join(":")}]" + 
                    "#{e.message}\n#{e.backtrace.join("\n")}")
    end
  end
end
wrapper() click to toggle source
# File lib/zscheduler.rb, line 146
def wrapper
  @wrapper
end