later 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +19 -19
- data/later.gemspec +1 -0
- data/lib/later.rb +51 -6
- data/lib/later/version.rb +1 -1
- data/test/lib/later.rb +2 -2
- metadata +18 -2
data/README.md
CHANGED
@@ -1,37 +1,37 @@
|
|
1
1
|
# Later
|
2
2
|
|
3
|
-
[**Later**](erol.github.com/later) is a Redis-backed event scheduling library for Ruby.
|
3
|
+
[**Later**](erol.github.com/later) is a lightweight Redis-backed event scheduling library for Ruby.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
|
-
Later allows you to
|
7
|
+
Later allows you to set unique events on a schedule and run them in the future:
|
8
8
|
|
9
9
|
require 'later'
|
10
10
|
|
11
|
+
Later[:reservations].set 'event-1', Time.now + 60
|
12
|
+
Later[:reservations].set 'event-2', Time.now + 120
|
13
|
+
Later[:reservations].set 'event-3', Time.now + 180
|
14
|
+
|
15
|
+
Rescheduling an event is simple:
|
16
|
+
|
17
|
+
Later[:reservations].set 'event-1', Time.now + 240
|
18
|
+
|
19
|
+
And an event can also be unset:
|
20
|
+
|
21
|
+
Later[key].unset 'event-1'
|
22
|
+
|
23
|
+
You can manage multiple schedules using different keys:
|
24
|
+
|
11
25
|
Later[:reservations]
|
12
26
|
Later[:appointments]
|
13
27
|
|
14
|
-
The
|
28
|
+
The schedules are stored on the default Redis instance. If you need a schedule which must reside on a different Redis instance, you can pass a [Nest](github.com/soveran/nest) object when referencing a schedule set.
|
15
29
|
|
16
30
|
redis = Redis.new host: host, port: port
|
17
31
|
key = Nest.new :reservations, redis
|
18
32
|
|
19
33
|
Later[key]
|
20
34
|
|
21
|
-
Setting an event schedule is simple:
|
22
|
-
|
23
|
-
Later[key].set 'my-unique-event', Time.now + 60
|
24
|
-
|
25
|
-
And can be reschedule with equal simplicity:
|
26
|
-
|
27
|
-
Later[key].set 'my-unique-event', Time.now + 120
|
28
|
-
|
29
|
-
An event schedule can also be unset:
|
30
|
-
|
31
|
-
Later[key].unset 'my-unique-event'
|
32
|
-
|
33
|
-
Note that event names should be unique in the scope of its schedule set.
|
34
|
-
|
35
35
|
### Workers
|
36
36
|
|
37
37
|
Workers are Ruby processes that run forever. They allow you to process event schedules in the background:
|
@@ -49,10 +49,10 @@ If for some reason, a worker has to stop itself from running:
|
|
49
49
|
Later[:reservations].each do |event|
|
50
50
|
# Do something with the event.
|
51
51
|
|
52
|
-
Later[:reservations].stop if stop?
|
52
|
+
Later[:reservations].stop! if stop?
|
53
53
|
end
|
54
54
|
|
55
|
-
# This line is reached
|
55
|
+
# This line is reached when stop? is true and Later[:reservations].stop! is called.
|
56
56
|
|
57
57
|
## Installation
|
58
58
|
|
data/later.gemspec
CHANGED
data/lib/later.rb
CHANGED
@@ -2,9 +2,14 @@ require 'later/version'
|
|
2
2
|
require 'redis'
|
3
3
|
require 'nest'
|
4
4
|
require 'json'
|
5
|
+
require 'predicates'
|
5
6
|
|
6
7
|
module Later
|
7
8
|
class Schedule
|
9
|
+
extend Predicates
|
10
|
+
|
11
|
+
predicate :stop?
|
12
|
+
|
8
13
|
def initialize(key)
|
9
14
|
if key.is_a?(Nest)
|
10
15
|
@key = key
|
@@ -13,46 +18,86 @@ module Later
|
|
13
18
|
end
|
14
19
|
end
|
15
20
|
|
21
|
+
# Get the Nest key of the schedule.
|
22
|
+
#
|
23
|
+
# Later[:reservations].key #=> Later::reservations
|
24
|
+
|
16
25
|
def key
|
17
26
|
@key
|
18
27
|
end
|
19
28
|
|
29
|
+
# Get the Nest key of the schedule exception list.
|
30
|
+
#
|
31
|
+
# Later[:reservations].exceptions #=> Later::reservations::exceptions
|
32
|
+
|
20
33
|
def exceptions
|
21
34
|
@exceptions ||= key[:exceptions]
|
22
35
|
end
|
23
36
|
|
37
|
+
# Get the schedule time of a unique event.
|
38
|
+
#
|
39
|
+
# Later[:reservations].set 'event-1', Time.parse('2012-09-28 11:36:17 +0800')
|
40
|
+
# Later[:reservations]['event-1'] #=> 2012-09-28 11:36:17 +0800
|
41
|
+
|
24
42
|
def [](event)
|
25
43
|
Time.at key[:schedule].zscore(event) rescue nil
|
26
44
|
end
|
27
45
|
|
46
|
+
# Gets the number of unique scheduled events.
|
47
|
+
#
|
48
|
+
# Later[:reservations].set 'event-1', Time.now + 60
|
49
|
+
# Later[:reservations].set 'event-2', Time.now + 120
|
50
|
+
# Later[:reservations].set 'event-3', Time.now + 180
|
51
|
+
# Later[:reservations].count #=> 3
|
52
|
+
|
28
53
|
def count
|
29
54
|
key[:schedule].zcard
|
30
55
|
end
|
31
56
|
|
57
|
+
# Set a unique event to the schedule.
|
58
|
+
#
|
59
|
+
# Later[:reservations].set 'event-1', Time.now + 60
|
60
|
+
|
32
61
|
def set(event, time)
|
33
62
|
key[:schedule].zadd time.to_i, event
|
34
63
|
end
|
35
64
|
|
65
|
+
# Unset a unique event from the schedule.
|
66
|
+
#
|
67
|
+
# Later[:reservations].unset 'event-2'
|
68
|
+
|
36
69
|
def unset(event)
|
37
70
|
key[:schedule].zrem event
|
38
71
|
end
|
39
72
|
|
40
|
-
|
73
|
+
# When called inside an `each` block, `stop!` signals the block to halt processing of the schedule.
|
74
|
+
#
|
75
|
+
# Later[:reservations].each do |event|
|
76
|
+
# Later[:reservations].stop!
|
77
|
+
# end
|
78
|
+
|
79
|
+
def stop!
|
41
80
|
@stop = true
|
42
81
|
end
|
43
82
|
|
83
|
+
# Process each event on the schedule. The block only gets called when an event is due to run based on the current time.
|
84
|
+
#
|
85
|
+
# Later[:reservations].each do |event|
|
86
|
+
# # Do something with the event
|
87
|
+
# end
|
88
|
+
|
44
89
|
def each(&block)
|
45
90
|
@stop = false
|
46
91
|
|
47
92
|
loop do
|
48
|
-
break if
|
93
|
+
break if stop?
|
49
94
|
|
50
95
|
time = Time.now.to_i
|
51
96
|
|
52
|
-
schedule.redis.multi
|
53
|
-
|
54
|
-
|
55
|
-
|
97
|
+
ids = schedule.redis.multi do
|
98
|
+
schedule.zrangebyscore '-inf', time
|
99
|
+
schedule.zremrangebyscore '-inf', time
|
100
|
+
end.first
|
56
101
|
|
57
102
|
key.redis.multi do
|
58
103
|
ids.each { |id| queue.lpush id }
|
data/lib/later/version.rb
CHANGED
data/test/lib/later.rb
CHANGED
@@ -59,7 +59,7 @@ class LaterTest < MiniTest::Unit::TestCase
|
|
59
59
|
|
60
60
|
Thread.new do
|
61
61
|
sleep 2 + 10 + (start - Time.now).to_i
|
62
|
-
Later[@key].stop
|
62
|
+
Later[@key].stop!
|
63
63
|
end
|
64
64
|
|
65
65
|
Later[@key].each do |event|
|
@@ -81,7 +81,7 @@ class LaterTest < MiniTest::Unit::TestCase
|
|
81
81
|
|
82
82
|
Thread.new do
|
83
83
|
sleep 2 + 3 + (start - Time.now).to_i
|
84
|
-
Later[@key].stop
|
84
|
+
Later[@key].stop!
|
85
85
|
end
|
86
86
|
|
87
87
|
Later[@key].each do |event|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: later
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
@@ -59,6 +59,22 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: predicates
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
62
78
|
- !ruby/object:Gem::Dependency
|
63
79
|
name: minitest
|
64
80
|
requirement: !ruby/object:Gem::Requirement
|