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
-
.add_shutdown_hook(&block) ⇒ Object
Add a new shutdown hook.
-
.every(frequency, options = {}, &block) ⇒ Timer
Start new scheduler.
- .init_reactor? ⇒ Boolean
-
.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
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
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, = {}, &block) block_given? or raise ArgumentError, "no block was given..." block = wrap block 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 |
.init_reactor? ⇒ Boolean
120 121 122 |
# File 'lib/zscheduler.rb', line 120 def init_reactor? !!wrapper end |
.join ⇒ Object
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
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 |
.stop ⇒ Object 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 |