zscheduler 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZjYwMDdkYjY2MjM2MzA5ZTc1ODJiZmI4MWZjMzE2ZDgyYjgzMGQxYg==
5
+ data.tar.gz: !binary |-
6
+ ZmYzMjA3MmQ1NDAzNjk0NjcxYjEwMTQwZDU4MjY3NzM1NGRjOTFjNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmNlZTYxOTQyOWI5YjcyOWQ3ZGZiOTQyNmZlNjg0NmQ5NjExYzNmYTI4Njdm
10
+ MTBkN2MwZDE2OTI2YWI1OWQ4Njc4NzhlOGI3YTM3OTc2OTg5Yzg0ZjZjODlk
11
+ MWY0YTg5MTZmODQyMmIyMTQzMTJkYzhkYzNjZTQzMGQ1NjVlOTI=
12
+ data.tar.gz: !binary |-
13
+ YzViNTMxNGJlNjVkOGNiOWM1YTFlZDkyYjRmYWI1MDBiZTcxNzMyZDEwMjI0
14
+ NWNiNzBiZmMzMmYzYmQyNzYwYzhiYmIyZGUwNzI1NmU1YWZhNDY4MzgyODBi
15
+ MWJkNTQxZWY1ZDljOTE3N2FjMGZiODJkYzVhZWNjYmY0NzhmZTY=
@@ -1,3 +1,3 @@
1
1
  module Zscheduler
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/zscheduler.rb CHANGED
@@ -3,34 +3,115 @@ require 'zscheduler/version'
3
3
 
4
4
  module Zscheduler
5
5
  class << self
6
+ # EM::Timer, EM::PeriodicTimer wrapper
7
+ class Timer < Struct.new(:timer)
8
+ # Cancel timer
9
+ def cancel
10
+ self.timer.cancel
11
+ end
12
+ end
6
13
 
14
+ # Start new scheduler
15
+ # @param [Hash] options
16
+ # @option options [True,False] :now (false) execute the block now
17
+ # @option options [True,False] :on_shutdown (false) execute the block on shutdown
18
+ # @option options [True,False] :on_thread (false) execute the block on a separated thread
19
+ # @option options [Time] :start_at (nil) start the scheduler in a given time
20
+ # @option options [Time] :start_in (nil) start the scheduler in a given delay ( seconds )
21
+ # @return [Timer] EventMachine timer wrapper
22
+ # @example
23
+ # # Simple timer
24
+ # Zscheduler.every(10) do
25
+ # puts "Running every 10 seconds"
26
+ # end
27
+ #
28
+ # # Run the block and then start the scheduler
29
+ # Zscheduler.every(10,now: true) do
30
+ # puts "Running every 10 seconds"
31
+ # end
32
+ #
33
+ # # Run the block every 10 seconds and on shutdown
34
+ # Zscheduler.every(10,on_shutdown: true) do
35
+ # puts "Running every 10 seconds and on shutdown"
36
+ # end
37
+ #
38
+ # # Start the scheduler in a given time
39
+ # Zscheduler.every(10,start_at: Time.now + 5) do
40
+ # puts "Will run 5 seconds from now and then for every 10 seconds"
41
+ # end
42
+ #
43
+ # Zscheduler.every(10,start_in: 5) do
44
+ # puts "Will run 5 seconds from now and then for every 10 seconds"
45
+ # end
46
+ #
47
+ # # Run the block on a separated thread
48
+ # Zscheduler.every(10,on_thread: true) do
49
+ # puts "I'm running on a separated thread"
50
+ # end
51
+ #
7
52
  def every(frequency,options = {}, &block)
8
53
  block_given? or raise ArgumentError, "no block was given..."
54
+ start_reactor
9
55
 
10
- reactor_running? or start_reactor
56
+ add_shutdown_hook(&block) if options[:on_shutdown]
57
+ block.call if options[:immediately] || options[:now]
11
58
 
12
- add_shutdown_hook &block if options[:on_shutdown]
13
- block.call if options[:immediately]
59
+ options[:start_in] = (options[:start_at] - Time.now) if options[:start_at]
14
60
 
15
- timers.push EM::PeriodicTimer.new(Integer frequency) do
16
- options[:on_thread] ? Thread.new(&block) : block.call
61
+ action = proc { options[:on_thread] ? Thread.new(&block) : block.call }
62
+ periodic = proc { EM::PeriodicTimer.new(frequency.to_i,&action) }
63
+
64
+ obj = Timer.new
65
+ obj.timer = if options[:start_in]
66
+ EM::Timer.new(options[:start_in]) do
67
+ action.call
68
+ obj.timer = periodic.call
69
+ end
70
+ else
71
+ periodic.call
17
72
  end
18
73
 
19
- timers.last
74
+ timers.push obj
75
+ obj
20
76
  end
21
77
 
78
+ # Run callback once
79
+ # @param [Time,Integer] seconds
80
+ # @example
81
+ # Zscheduler.once(Time.now + 10) do
82
+ # puts "I'm running 10 seconds from now"
83
+ # end
84
+ #
85
+ # # Same as above
86
+ # Zscheduler.once(10) do
87
+ # puts "I'm running 10 seconds from now"
88
+ # end
89
+ def once(seconds, &block)
90
+ start_reactor
91
+ seconds = (seconds - Time.now) if seconds.kind_of?(Time)
92
+ timers.push(Timer.new(EM::Timer.new(seconds.to_i,&block))).last
93
+ end
94
+
95
+ # Stop the scheduler, cancel all timers and run all the shutdown hooks
22
96
  def stop
23
97
  timers.each(&:cancel)
24
98
  shutdown_hooks.each(&:call)
25
99
  wrapper and EM.stop
26
100
  end
27
101
 
28
- alias_method :showdown, :stop
102
+ alias_method :shutdown, :stop
29
103
 
104
+ # Add a new shutdown hook
105
+ # @example
106
+ # Zscheduler.add_shutdown_hook do
107
+ # puts "someone called to Zscheduler.stop"
108
+ # end
109
+ # Zscheduler.stop
30
110
  def add_shutdown_hook(&block)
31
111
  shutdown_hooks.push block
32
112
  end
33
113
 
114
+ # Sleep until Zscheduler stops
34
115
  def join
35
116
  (wrapper or EM.reactor_thread).join
36
117
  end
@@ -38,13 +119,10 @@ module Zscheduler
38
119
  private
39
120
 
40
121
  def start_reactor
41
- @wrapper = Thread.new { EventMachine.run }
122
+ return if EM.reactor_running?
123
+ @wrapper = Thread.new(&EM.method(:run))
42
124
  wrapper.abort_on_exception = true
43
- Thread.pass until reactor_running?
44
- end
45
-
46
- def reactor_running?
47
- EventMachine.reactor_running?
125
+ Thread.pass until EM.reactor_running?
48
126
  end
49
127
 
50
128
  def wrapper
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zscheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Eran Barak Levi
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-09 00:00:00.000000000 Z
11
+ date: 2014-01-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: eventmachine
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -38,26 +35,26 @@ files:
38
35
  - lib/zscheduler.rb
39
36
  homepage: http://github.com/eranb/zscheduler
40
37
  licenses: []
38
+ metadata: {}
41
39
  post_install_message:
42
40
  rdoc_options: []
43
41
  require_paths:
44
42
  - lib
45
43
  required_ruby_version: !ruby/object:Gem::Requirement
46
- none: false
47
44
  requirements:
48
45
  - - ! '>='
49
46
  - !ruby/object:Gem::Version
50
47
  version: 1.8.5
51
48
  required_rubygems_version: !ruby/object:Gem::Requirement
52
- none: false
53
49
  requirements:
54
50
  - - ! '>='
55
51
  - !ruby/object:Gem::Version
56
52
  version: '0'
57
53
  requirements: []
58
54
  rubyforge_project: zscheduler
59
- rubygems_version: 1.8.25
55
+ rubygems_version: 2.1.9
60
56
  signing_key:
61
- specification_version: 3
57
+ specification_version: 4
62
58
  summary: minimalistic scheduler on top of event-machine
63
59
  test_files: []
60
+ has_rdoc: