yakischloba-em-timers 0.0.1
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/README +39 -0
- data/lib/em-timers.rb +7 -0
- data/lib/em-timers/em-timers.rb +54 -0
- data/lib/em-timers/numericmixable.rb +56 -0
- metadata +65 -0
data/README
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
em-timers
|
2
|
+
|
3
|
+
helper methods for timers in EventMachine
|
4
|
+
|
5
|
+
examples:
|
6
|
+
|
7
|
+
# Once per hour, starting now. Note the :now symbol must be passed for your block to be called immediately.
|
8
|
+
EM.do_hourly(:starting => :now) { puts "drink a beer" }
|
9
|
+
|
10
|
+
# Once per day, starting in 1 day.
|
11
|
+
EM.do_daily { puts "take a shower" }
|
12
|
+
|
13
|
+
# Once per week, starting in 10 hours
|
14
|
+
EM.do_weekly(:starting => 10.hours.from_now) { puts "take out the garbage" }
|
15
|
+
|
16
|
+
# Once per month, starting at this time tomorrow.
|
17
|
+
EM.do_monthly(:starting => 1.day.from_now) { puts "pay the bills" }
|
18
|
+
|
19
|
+
# Or you can simply pass a number of seconds til starting
|
20
|
+
EM.do_monthly(:starting => 2592000) { puts "pay the bills, in seconds!" }
|
21
|
+
|
22
|
+
# Leveraging the Chronic time parsing library. em-timers knows if you have required
|
23
|
+
# Chronic and uses it to parse strings if you have. Otherwise, it uses Time.parse.
|
24
|
+
#
|
25
|
+
# Every 2 hours, starting next Tuesday at 9AM.
|
26
|
+
require 'chronic'
|
27
|
+
EM.do(:every => 2.hours, :starting => 'next tuesday at 9am') { puts "go pee" }
|
28
|
+
|
29
|
+
# You can add up units of time as well
|
30
|
+
EM.do(:every => 1.minutes + 3.seconds) { puts "hi!" }
|
31
|
+
|
32
|
+
|
33
|
+
TODO:
|
34
|
+
|
35
|
+
Maintain a list of task signatures and their associated timer signatures so tasks can be canceled.
|
36
|
+
|
37
|
+
|
38
|
+
jakecdouglas@gmail.com
|
39
|
+
yakischloba on Freenode #eventmachine
|
data/lib/em-timers.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module EventMachine
|
2
|
+
|
3
|
+
def self.do(options={}, &blk)
|
4
|
+
increment = options[:every]
|
5
|
+
starting = options[:starting]
|
6
|
+
|
7
|
+
reschedule = proc {|inc, block| EM.add_timer(inc) { reschedule.call(inc, block) } ; block.call }
|
8
|
+
|
9
|
+
if starting == :now
|
10
|
+
EM.add_timer(increment) { reschedule.call(increment, blk) }
|
11
|
+
blk.call
|
12
|
+
return nil
|
13
|
+
else
|
14
|
+
if starting.is_a?(String)
|
15
|
+
starting = Object.const_defined?("Chronic") ? Chronic.parse(starting) : Time.parse(starting)
|
16
|
+
elsif starting.is_a?(Time) || starting.kind_of?(Numeric)
|
17
|
+
starting = starting
|
18
|
+
else
|
19
|
+
starting = Time.now
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if starting.is_a?(Time)
|
24
|
+
time = starting - Time.now
|
25
|
+
else
|
26
|
+
time = starting
|
27
|
+
end
|
28
|
+
|
29
|
+
while time < 0
|
30
|
+
time += increment
|
31
|
+
end
|
32
|
+
|
33
|
+
EM.add_timer(time) { reschedule.call(increment, blk) }
|
34
|
+
|
35
|
+
return nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.do_hourly(options={}, &blk)
|
39
|
+
self.do({:every => 1.hour}.merge(options), &blk)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.do_daily(options={}, &blk)
|
43
|
+
self.do({:every => 1.day}.merge(options), &blk)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.do_weekly(options={}, &blk)
|
47
|
+
self.do({:every => 1.week}.merge(options), &blk)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.do_monthly(options={}, &blk)
|
51
|
+
self.do({:every => 1.month}.merge(options), &blk)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# Stolen directly from ramaze/snippets
|
2
|
+
module NumericMixable
|
3
|
+
def seconds
|
4
|
+
self
|
5
|
+
end
|
6
|
+
alias second seconds
|
7
|
+
|
8
|
+
# 60 seconds in a minute
|
9
|
+
def minutes
|
10
|
+
self * 60
|
11
|
+
end
|
12
|
+
alias minute minutes
|
13
|
+
|
14
|
+
# 60 minutes in an hour
|
15
|
+
def hours
|
16
|
+
self * 3600
|
17
|
+
end
|
18
|
+
alias hour hours
|
19
|
+
|
20
|
+
# 24 hours in a day
|
21
|
+
def days
|
22
|
+
self * 86400
|
23
|
+
end
|
24
|
+
alias day days
|
25
|
+
|
26
|
+
# 7 days in a week
|
27
|
+
def weeks
|
28
|
+
self * 604800
|
29
|
+
end
|
30
|
+
alias week weeks
|
31
|
+
|
32
|
+
# 30 days in a month
|
33
|
+
def months
|
34
|
+
self * 2592000
|
35
|
+
end
|
36
|
+
alias month months
|
37
|
+
|
38
|
+
# 365.25 days in a year
|
39
|
+
def years
|
40
|
+
self * 883612800
|
41
|
+
end
|
42
|
+
alias year years
|
43
|
+
|
44
|
+
# Time in the past, i.e. 3.days.ago
|
45
|
+
def ago t = Time.now
|
46
|
+
t - self
|
47
|
+
end
|
48
|
+
alias before ago
|
49
|
+
|
50
|
+
# Time in the future, i.e. 3.days.from_now
|
51
|
+
def from_now t = Time.now
|
52
|
+
t + self
|
53
|
+
end
|
54
|
+
alias since from_now
|
55
|
+
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yakischloba-em-timers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jake Douglas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-27 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: eventmachine
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: helper methods for timers in EventMachine
|
26
|
+
email: jakecdouglas@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- README
|
35
|
+
- lib/em-timers.rb
|
36
|
+
- lib/em-timers/numericmixable.rb
|
37
|
+
- lib/em-timers/em-timers.rb
|
38
|
+
has_rdoc: false
|
39
|
+
homepage: http://www.github.com/yakischloba/em-timers
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: helper methods for timers in EventMachine
|
64
|
+
test_files: []
|
65
|
+
|