Module: Zscheduler
- Defined in:
- lib/zscheduler.rb,
lib/zscheduler/version.rb
Defined Under Namespace
Classes: Timer
Constant Summary collapse
- VERSION =
"0.0.6"
Class Method Summary collapse
-
.add_shutdown_hook(&block) ⇒ Object
Add a new shutdown hook.
-
.every(frequency, options = {}, &block) ⇒ Timer
Start new scheduler.
-
.join ⇒ Object
Sleep until Zscheduler stops.
-
.once(seconds, &block) ⇒ Object
Run callback once.
-
.stop ⇒ Object
(also: shutdown)
Stop the scheduler, cancel all timers and run all the shutdown hooks.
Class Method Details
.add_shutdown_hook(&block) ⇒ Object
Add a new shutdown hook
110 111 112 |
# File 'lib/zscheduler.rb', line 110 def add_shutdown_hook(&block) shutdown_hooks.push block end |
.every(frequency, options = {}, &block) ⇒ Timer
Start new scheduler
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 |
# File 'lib/zscheduler.rb', line 52 def every(frequency, = {}, &block) block_given? or raise ArgumentError, "no block was given..." start_reactor add_shutdown_hook(&block) if [:on_shutdown] block.call if [:immediately] || [:now] [:start_in] = ([:start_at] - Time.now) if [:start_at] action = proc { [:on_thread] ? Thread.new(&block) : block.call } periodic = proc { EM::PeriodicTimer.new(frequency.to_i,&action) } obj = Timer.new obj.timer = if [:start_in] EM::Timer.new([:start_in]) do action.call obj.timer = periodic.call end else periodic.call end timers.push obj obj end |
.join ⇒ Object
Sleep until Zscheduler stops
115 116 117 |
# File 'lib/zscheduler.rb', line 115 def join (wrapper or EM.reactor_thread).join end |
.once(seconds, &block) ⇒ Object
Run callback once
89 90 91 92 93 |
# File 'lib/zscheduler.rb', line 89 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,&block))).last end |
.stop ⇒ Object Also known as: shutdown
Stop the scheduler, cancel all timers and run all the shutdown hooks
96 97 98 99 100 |
# File 'lib/zscheduler.rb', line 96 def stop timers.each(&:cancel) shutdown_hooks.each(&:call) wrapper and EM.reactor_running? and EM.stop end |