eventmachinealignedperiodic 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,27 @@
1
+ # History for EventMachineAlignedPeriodic
2
+
3
+ ## v2.1.1 (April 13 2013)
4
+
5
+ * forgot to interpolate a token in a log message
6
+
7
+ ## v2.1.0 (April 13 2013)
8
+
9
+ * add a fourth arg to the constructor, an optional logger that must respond to #debug. If provided, it logs exceptions in the periodic scheduler and the time of the next event after scheduling
10
+
11
+ ## v2.0.1 (April 7 2013)
12
+
13
+ * use thread mutex in periodic caller as well
14
+
15
+ ## v2.0.0 (April 4 2013)
16
+
17
+ * changed invocation style. You now call start on the object rather than runner.go
18
+ * added poke method to kick off the periodic event outside of normal times. Note that attempting to poke when the periodic scheduler is not running throws an exception
19
+ * added stop method to stop event scheduling. This calls the periodic event one last time with a true arg
20
+ * protected public methods with a thread mutex
21
+ * made private that which should be private
22
+ * added an reader :nextevent with the time the next event should kick off
23
+ * fixed arg to periodic event: should always reflect whether the method represents a full period or not
24
+
25
+ ## v1.0.0 (March 15 2013)
26
+
27
+ * initial version released on Github
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012, 2013 James FitzGibbon <james@nadt.net>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to
7
+ deal in the Software without restriction, including without limitation the
8
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9
+ sell copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
+ IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ LICENSE.txt
5
+ Rakefile
6
+ lib/eventmachine_alignedperiodic.rb
7
+ eventmachinealignedperiodic.gemspec
@@ -0,0 +1,85 @@
1
+ # EventMachineAlignedPeriodic
2
+
3
+ home :: https://github.com/jf647/EventMachineAlignedPeriodic
4
+
5
+ ## SUMMARY:
6
+
7
+ EventMachine helper to call a block on a periodic schedule with clock alignment.
8
+
9
+ ## DESCRIPTION
10
+
11
+ EventMachineAlignedPeriodic helps to kick off a block of code on a periodic
12
+ schedule aligned with a time period. Rather than have something starting at
13
+ 2 seconds, 17 seconds, 32 seconds and so on you can have it consistently
14
+ launch at 0, 15, 30.
15
+
16
+ ## SYNOPSIS:
17
+
18
+ require 'eventmachine'
19
+ require 'eventmachine_alignedperiodic'
20
+ require 'logger'
21
+
22
+ log = Logger.new(STDOUT)
23
+ log.level = Logger::DEBUG
24
+
25
+ periodic = EventMachine::AlignedPeriodic.new(interval, proc {
26
+ |partial| do_something(partial)
27
+ }, offset, log)
28
+
29
+ def do_something(partial)
30
+ puts "Yay! I just did something"
31
+ if partial
32
+ puts "... but only partially"
33
+ end
34
+ end
35
+
36
+ EM.run {
37
+ periodic.start
38
+ puts "next event is at #{periodic.nextevent}"
39
+ periodic.poke
40
+ periodic.stop
41
+ }
42
+
43
+ Assuming that interval was 900 (15 minutes) and offset was 30, do_something will be called
44
+ at the next aligned 15 minute interval. So if you started your program at 10:07:06, it would
45
+ be called at 10:15:30, then at 10:30:30, 10:45:30 and so on.
46
+
47
+ Clock resolution and EventMachine latency means that the proc is usually kicked off tens or
48
+ hundreds of microseconds after the interval.
49
+
50
+ The first time the proc is called and the last time (when the EventMachine loop shuts down) the
51
+ partial flag will be true. Every other time it will be false.
52
+
53
+ ## LICENSE:
54
+
55
+ The MIT License (MIT)
56
+
57
+ Copyright (c) 2012, 2013 James FitzGibbon <james@nadt.net>
58
+
59
+ Permission is hereby granted, free of charge, to any person obtaining a copy
60
+ of this software and associated documentation files (the "Software"), to
61
+ deal in the Software without restriction, including without limitation the
62
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
63
+ sell copies of the Software, and to permit persons to whom the Software is
64
+ furnished to do so, subject to the following conditions:
65
+
66
+ The above copyright notice and this permission notice shall be included in
67
+ all copies or substantial portions of the Software.
68
+
69
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
70
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
71
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
72
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
73
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
74
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
75
+ IN THE SOFTWARE.
76
+
77
+ ## Contributing to EventMachineAlignedPeriodic
78
+
79
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
80
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
81
+ * Fork the project
82
+ * Start a feature/bugfix branch
83
+ * Commit and push until you are happy with your contribution
84
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
85
+ * Please try not to mess with the version or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
@@ -0,0 +1,31 @@
1
+ require 'rake/testtask'
2
+ require 'hoe'
3
+
4
+ Hoe.plugin :gemspec
5
+ Hoe.plugin :bundler
6
+
7
+ Hoe.spec 'eventmachinealignedperiodic' do
8
+ developer("James FitzGibbon", "james@nadt.net")
9
+ license "MIT"
10
+ dependency 'eventmachine', '~> 1.0.3'
11
+ dependency 'hoe', '~> 3.7.1', :dev
12
+ dependency 'hoe-gemspec', '~> 1.0.0', :dev
13
+ dependency 'hoe-bundler', '~> 1.2.0', :dev
14
+ dependency 'simplecov', '~> 0.7.1', :dev
15
+ dependency 'simplecov-console', '~> 0.1.1', :dev
16
+ dependency 'minitest', '~> 5.0.8', :dev
17
+ dependency 'minitest-debugger', '~> 1.0.2', :dev
18
+ end
19
+
20
+ task :default => [:unit_tests]
21
+
22
+ task :package => [ 'gem:spec', 'bundler:gemfile' ]
23
+
24
+ desc "Run basic tests"
25
+ Rake::TestTask.new("unit_tests") { |t|
26
+ t.libs.push 'lib'
27
+ t.libs.push 'test'
28
+ t.pattern = 'test/test_*.rb'
29
+ t.verbose = true
30
+ t.warning = false
31
+ }
@@ -0,0 +1,58 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "eventmachinealignedperiodic"
5
+ s.version = "2.1.1.20131112160551"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["James FitzGibbon"]
9
+ s.date = "2013-11-13"
10
+ s.description = "EventMachineAlignedPeriodic helps to kick off a block of code on a periodic\nschedule aligned with a time period. Rather than have something starting at\n2 seconds, 17 seconds, 32 seconds and so on you can have it consistently\nlaunch at 0, 15, 30."
11
+ s.email = ["james@nadt.net"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt", "LICENSE.txt"]
13
+ s.files = ["History.txt", "Manifest.txt", "README.txt", "LICENSE.txt", "Rakefile", "lib/eventmachine_alignedperiodic.rb", "eventmachinealignedperiodic.gemspec", "test/test_eventmachine_alignedperiodic.rb", ".gemtest"]
14
+ s.homepage = "https://github.com/jf647/EventMachineAlignedPeriodic"
15
+ s.licenses = ["MIT"]
16
+ s.rdoc_options = ["--main", "README.txt"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = "eventmachinealignedperiodic"
19
+ s.rubygems_version = "1.8.23"
20
+ s.summary = "EventMachineAlignedPeriodic helps to kick off a block of code on a periodic schedule aligned with a time period"
21
+ s.test_files = ["test/test_eventmachine_alignedperiodic.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<eventmachine>, ["~> 1.0.3"])
28
+ s.add_development_dependency(%q<rdoc>, ["~> 4.0"])
29
+ s.add_development_dependency(%q<hoe>, ["~> 3.7.1"])
30
+ s.add_development_dependency(%q<hoe-gemspec>, ["~> 1.0.0"])
31
+ s.add_development_dependency(%q<hoe-bundler>, ["~> 1.2.0"])
32
+ s.add_development_dependency(%q<simplecov>, ["~> 0.7.1"])
33
+ s.add_development_dependency(%q<simplecov-console>, ["~> 0.1.1"])
34
+ s.add_development_dependency(%q<minitest>, ["~> 5.0.8"])
35
+ s.add_development_dependency(%q<minitest-debugger>, ["~> 1.0.2"])
36
+ else
37
+ s.add_dependency(%q<eventmachine>, ["~> 1.0.3"])
38
+ s.add_dependency(%q<rdoc>, ["~> 4.0"])
39
+ s.add_dependency(%q<hoe>, ["~> 3.7.1"])
40
+ s.add_dependency(%q<hoe-gemspec>, ["~> 1.0.0"])
41
+ s.add_dependency(%q<hoe-bundler>, ["~> 1.2.0"])
42
+ s.add_dependency(%q<simplecov>, ["~> 0.7.1"])
43
+ s.add_dependency(%q<simplecov-console>, ["~> 0.1.1"])
44
+ s.add_dependency(%q<minitest>, ["~> 5.0.8"])
45
+ s.add_dependency(%q<minitest-debugger>, ["~> 1.0.2"])
46
+ end
47
+ else
48
+ s.add_dependency(%q<eventmachine>, ["~> 1.0.3"])
49
+ s.add_dependency(%q<rdoc>, ["~> 4.0"])
50
+ s.add_dependency(%q<hoe>, ["~> 3.7.1"])
51
+ s.add_dependency(%q<hoe-gemspec>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<hoe-bundler>, ["~> 1.2.0"])
53
+ s.add_dependency(%q<simplecov>, ["~> 0.7.1"])
54
+ s.add_dependency(%q<simplecov-console>, ["~> 0.1.1"])
55
+ s.add_dependency(%q<minitest>, ["~> 5.0.8"])
56
+ s.add_dependency(%q<minitest-debugger>, ["~> 1.0.2"])
57
+ end
58
+ end
@@ -0,0 +1,98 @@
1
+ require 'eventmachine'
2
+ require 'date'
3
+ require 'thread'
4
+
5
+ module EventMachine
6
+
7
+ class AlignedPeriodic
8
+
9
+ VERSION = '2.1.1'
10
+
11
+ attr_reader :nextevent
12
+
13
+ def initialize(interval, p, offset = 0, logger = nil)
14
+ @interval = interval
15
+ @offset = offset
16
+ @p = p
17
+ @logger = logger
18
+ @partial = true
19
+ @running = false
20
+ @mutex = Mutex.new
21
+ end
22
+
23
+ def start
24
+ @mutex.synchronize {
25
+ EventMachine.add_shutdown_hook { @p.call(true) }
26
+ @running = true
27
+ schedule_first_event
28
+ }
29
+ end
30
+
31
+ def poke
32
+ @mutex.synchronize {
33
+ # if we aren't running, we can't be poked
34
+ unless @running
35
+ raise "periodic schedulder not running; can't be poked"
36
+ end
37
+ EventMachine.cancel_timer(@timer)
38
+ @p.call(true)
39
+ @partial = true
40
+ schedule_next_event
41
+ }
42
+ end
43
+
44
+ def stop
45
+ @mutex.synchronize {
46
+ EventMachine.cancel_timer(@timer)
47
+ @running = false
48
+ @p.call(true)
49
+ }
50
+ end
51
+
52
+ private
53
+
54
+ def do_periodic
55
+ @mutex.synchronize {
56
+ # we can't remove shutdown hooks, so if we aren't running, do nothing
57
+ return unless @running
58
+ begin
59
+ @p.call(@partial)
60
+ rescue => e
61
+ if ! @logger.nil?
62
+ @logger.debug("exception in do_periodic: #{e}")
63
+ end
64
+ end
65
+ @partial = false if @partial
66
+ @lastevent = @nextevent
67
+ schedule_next_event
68
+ }
69
+ end
70
+
71
+ def schedule_first_event
72
+ now = Time.now
73
+ if @lastevent.nil?
74
+ @lastevent = now.to_date.to_time + @offset
75
+ while @lastevent + @interval < now
76
+ @lastevent += @interval
77
+ end
78
+ end
79
+ @nextevent = @lastevent + @interval
80
+ schedule
81
+ end
82
+
83
+ def schedule_next_event
84
+ @nextevent = @lastevent + @interval
85
+ schedule
86
+ end
87
+
88
+ def schedule
89
+ if ! @logger.nil?
90
+ @logger.debug("next event is at #{@nextevent}")
91
+ end
92
+ nextevent_in = @nextevent - Time.now
93
+ @timer = EventMachine.add_timer(nextevent_in) { do_periodic() }
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,8 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestEVMAAlignedPeriodic < Minitest::Test
4
+ def test_constructor
5
+ aligned = EventMachine::AlignedPeriodic.new(900, proc { puts "foo" })
6
+ assert_instance_of EventMachine::AlignedPeriodic, aligned, "constructor works"
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventmachinealignedperiodic
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James FitzGibbon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: eventmachine
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '4.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '4.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: hoe
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 3.7.1
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.7.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: hoe-gemspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: hoe-bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.2.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.2.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.7.1
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.7.1
110
+ - !ruby/object:Gem::Dependency
111
+ name: simplecov-console
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.1.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.1.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: minitest
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 5.0.8
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 5.0.8
142
+ - !ruby/object:Gem::Dependency
143
+ name: minitest-debugger
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 1.0.2
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 1.0.2
158
+ description: ! 'EventMachineAlignedPeriodic helps to kick off a block of code on a
159
+ periodic
160
+
161
+ schedule aligned with a time period. Rather than have something starting at
162
+
163
+ 2 seconds, 17 seconds, 32 seconds and so on you can have it consistently
164
+
165
+ launch at 0, 15, 30.'
166
+ email:
167
+ - james@nadt.net
168
+ executables: []
169
+ extensions: []
170
+ extra_rdoc_files:
171
+ - History.txt
172
+ - Manifest.txt
173
+ - README.txt
174
+ - LICENSE.txt
175
+ files:
176
+ - History.txt
177
+ - Manifest.txt
178
+ - README.txt
179
+ - LICENSE.txt
180
+ - Rakefile
181
+ - lib/eventmachine_alignedperiodic.rb
182
+ - eventmachinealignedperiodic.gemspec
183
+ - test/test_eventmachine_alignedperiodic.rb
184
+ - .gemtest
185
+ homepage: https://github.com/jf647/EventMachineAlignedPeriodic
186
+ licenses:
187
+ - MIT
188
+ post_install_message:
189
+ rdoc_options:
190
+ - --main
191
+ - README.txt
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ none: false
196
+ requirements:
197
+ - - ! '>='
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ requirements: []
207
+ rubyforge_project: eventmachinealignedperiodic
208
+ rubygems_version: 1.8.23
209
+ signing_key:
210
+ specification_version: 3
211
+ summary: EventMachineAlignedPeriodic helps to kick off a block of code on a periodic
212
+ schedule aligned with a time period
213
+ test_files:
214
+ - test/test_eventmachine_alignedperiodic.rb