em-scheduled-timer 0.1.0
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.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +6 -0
- data/em-scheduled-timer.gemspec +26 -0
- data/lib/em-scheduled-timer.rb +26 -0
- data/lib/em-scheduled-timer/version.rb +5 -0
- data/spec/em-scheduled-timer_spec.rb +73 -0
- data/spec/support/eventmachine_helper.rb +15 -0
- metadata +129 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Leo Cassarani
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# EM::ScheduledTimer
|
2
|
+
|
3
|
+
EventMachine timers are great, but they work by waiting for a specified
|
4
|
+
time interval before firing. Instead, `EM::ScheduledTimer` lets you
|
5
|
+
specify a `Time`, `Date` or `DateTime` object (or indeed anything that will
|
6
|
+
respond to `to_time`).
|
7
|
+
|
8
|
+
## Why is this useful?
|
9
|
+
|
10
|
+
Imagine you're polling an HTTP-based API for changes. Because the API is
|
11
|
+
nice, it will set `Expires` response headers so you know when to make
|
12
|
+
the next request. With `EM::ScheduledTimer` (and the
|
13
|
+
[em-http-request](https://github.com/igrigorik/em-http-request) gem),
|
14
|
+
this becomes very easy:
|
15
|
+
|
16
|
+
def poll_api
|
17
|
+
http = EM::HttpRequest.new("http://api.example.com/changes")
|
18
|
+
http.callback do
|
19
|
+
expires = http.response_header['EXPIRES']
|
20
|
+
time = Time.httpdate(expires)
|
21
|
+
EM::ScheduledTimer.new(time) { poll_api }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Generally speaking, the API for a `ScheduledTimer` is modelled after
|
28
|
+
that of a regular `EM::Timer`.
|
29
|
+
|
30
|
+
You can create `EM::ScheduledTimer` instances and pass in a block:
|
31
|
+
|
32
|
+
EM::ScheduledTimer.new(some_future_time) do
|
33
|
+
puts "Fire!"
|
34
|
+
end
|
35
|
+
|
36
|
+
Alternatively, you can pass in any object that responds to `#call`
|
37
|
+
(including a `Proc`):
|
38
|
+
|
39
|
+
callback = -> { puts "Fire!" }
|
40
|
+
EM::ScheduledTimer.new(some_future_time, callback)
|
41
|
+
|
42
|
+
A `ScheduledTimer` can also be cancelled:
|
43
|
+
|
44
|
+
timer = EM::ScheduledTimer.new(some_future_time) do
|
45
|
+
puts "Fire!"
|
46
|
+
end
|
47
|
+
|
48
|
+
timer.cancel # The timer won't fire
|
49
|
+
|
50
|
+
As with regular timers, a convenience method is available on the
|
51
|
+
`EventMachine` module:
|
52
|
+
|
53
|
+
EM.add_scheduled_timer(some_future_time) do
|
54
|
+
puts "Fire!"
|
55
|
+
end
|
56
|
+
|
57
|
+
Note that in the latter case, you won't be able to cancel a timer that
|
58
|
+
you've scheduled.
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'em-scheduled-timer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "em-scheduled-timer"
|
8
|
+
spec.version = EventMachine::ScheduledTimer::VERSION
|
9
|
+
spec.authors = ["Leo Cassarani"]
|
10
|
+
spec.email = ["leo.cassarani@me.com"]
|
11
|
+
spec.description = %q{EM::ScheduledTimer provides EventMachine timers that let you specify a time, rather than an interval, at which to fire.}
|
12
|
+
spec.summary = %q{EventMachine timers that fire at a given time in the future}
|
13
|
+
spec.homepage = "https://github.com/leocassarani/em-scheduled-timer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "eventmachine", "~> 1.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.0"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "eventmachine"
|
2
|
+
require "em-scheduled-timer/version"
|
3
|
+
|
4
|
+
module EventMachine
|
5
|
+
class ScheduledTimer
|
6
|
+
def initialize(time, callback = nil, &block)
|
7
|
+
@signature = EventMachine.add_timer(interval(time), callback || block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def cancel
|
11
|
+
EventMachine.cancel_timer(@signature)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def interval(time)
|
17
|
+
time.to_time - Time.now
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.add_scheduled_timer(*args, &block)
|
22
|
+
interval = args.shift
|
23
|
+
callback = args.shift || block
|
24
|
+
EventMachine::ScheduledTimer.new(interval, callback)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'em-scheduled-timer'
|
2
|
+
require 'time'
|
3
|
+
require File.expand_path('../support/eventmachine_helper', __FILE__)
|
4
|
+
|
5
|
+
describe EM::ScheduledTimer do
|
6
|
+
include EventMachineHelper
|
7
|
+
|
8
|
+
let(:soon) { Time.now + 0.1 }
|
9
|
+
|
10
|
+
describe "instantiating new ScheduledTimers directly" do
|
11
|
+
it "runs a block at the given time" do
|
12
|
+
EM.run {
|
13
|
+
EventMachine::ScheduledTimer.new(soon) { pass }
|
14
|
+
fail_after(0.2)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
it "can be given a callable object instead of a block" do
|
19
|
+
EM.run {
|
20
|
+
callback = lambda { pass }
|
21
|
+
EventMachine::ScheduledTimer.new(soon, callback)
|
22
|
+
fail_after(0.2)
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
it "can be cancelled" do
|
27
|
+
EM.run {
|
28
|
+
timer = EventMachine::ScheduledTimer.new(soon) do
|
29
|
+
fail "Expected timer not to fire, but it did"
|
30
|
+
end
|
31
|
+
|
32
|
+
timer.cancel
|
33
|
+
|
34
|
+
pass_after(0.2)
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "using the EM.add_scheduled_timer convenience method" do
|
40
|
+
it "runs a block at the given time" do
|
41
|
+
EM.run {
|
42
|
+
EM.add_scheduled_timer(soon) { pass }
|
43
|
+
fail_after(0.2)
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
it "can be given a callable object instead of a block" do
|
48
|
+
EM.run {
|
49
|
+
callback = lambda { pass }
|
50
|
+
EM.add_scheduled_timer(soon, callback)
|
51
|
+
fail_after(0.2)
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "specifying a firing time" do
|
57
|
+
it "supports DateTime objects" do
|
58
|
+
EM.run {
|
59
|
+
datetime = soon.to_datetime
|
60
|
+
EventMachine::ScheduledTimer.new(datetime) { pass }
|
61
|
+
fail_after(0.2)
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
it "supports any object that responds to #to_time" do
|
66
|
+
EM.run {
|
67
|
+
future = mock(:future, to_time: soon)
|
68
|
+
EventMachine::ScheduledTimer.new(future) { pass }
|
69
|
+
fail_after(0.2)
|
70
|
+
}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module EventMachineHelper
|
2
|
+
def fail_after(interval)
|
3
|
+
EM.add_timer(interval) do
|
4
|
+
fail "Expected timer to fire at the scheduled time, but it didn't"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def pass
|
9
|
+
EM.stop
|
10
|
+
end
|
11
|
+
|
12
|
+
def pass_after(interval)
|
13
|
+
EM.add_timer(interval) { pass }
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-scheduled-timer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Leo Cassarani
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-06 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'
|
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'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.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: '1.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
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: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '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: '0'
|
78
|
+
description: EM::ScheduledTimer provides EventMachine timers that let you specify
|
79
|
+
a time, rather than an interval, at which to fire.
|
80
|
+
email:
|
81
|
+
- leo.cassarani@me.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- em-scheduled-timer.gemspec
|
92
|
+
- lib/em-scheduled-timer.rb
|
93
|
+
- lib/em-scheduled-timer/version.rb
|
94
|
+
- spec/em-scheduled-timer_spec.rb
|
95
|
+
- spec/support/eventmachine_helper.rb
|
96
|
+
homepage: https://github.com/leocassarani/em-scheduled-timer
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: -3497934660945191801
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
hash: -3497934660945191801
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.23
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: EventMachine timers that fire at a given time in the future
|
127
|
+
test_files:
|
128
|
+
- spec/em-scheduled-timer_spec.rb
|
129
|
+
- spec/support/eventmachine_helper.rb
|