Module: Zscheduler

Defined in:
lib/zscheduler.rb,
lib/zscheduler/version.rb

Defined Under Namespace

Classes: Timer

Constant Summary collapse

VERSION =
"0.0.8"

Class Method Summary collapse

Class Method Details

.add_shutdown_hook(&block) ⇒ Object

Add a new shutdown hook

Examples:

Zscheduler.add_shutdown_hook do
  puts "someone called to Zscheduler.stop"
end
Zscheduler.stop


111
112
113
# File 'lib/zscheduler.rb', line 111

def add_shutdown_hook(&block)
  shutdown_hooks.push block
end

.every(frequency, options = {}, &block) ⇒ Timer

Start new scheduler

Examples:

# 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

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :now (True, False) — default: false

    execute the block now

  • :on_shutdown (True, False) — default: false

    execute the block on shutdown

  • :on_thread (True, False) — default: false

    execute the block on a separated thread

  • :start_at (Time) — default: nil

    start the scheduler in a given time

  • :start_in (Time) — default: nil

    start the scheduler in a given delay ( seconds )

Returns:

  • (Timer)

    EventMachine timer wrapper



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/zscheduler.rb', line 120

def init_reactor?
  !!wrapper
end

.joinObject

Sleep until Zscheduler stops



116
117
118
# File 'lib/zscheduler.rb', line 116

def join
  (wrapper or EM.reactor_thread).join
end

.once(seconds, &block) ⇒ Object

Run callback once

Examples:

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

Parameters:

  • seconds (Time, Integer)


90
91
92
93
94
# 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

.stopObject Also known as: shutdown

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



97
98
99
100
101
# File 'lib/zscheduler.rb', line 97

def stop
  timers.each(&:cancel)
  shutdown_hooks.each(&:call)
  wrapper and EM.reactor_running? and EM.stop
end