redis-scheduler 0.8 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/COPYING +1 -1
- data/lib/redis-scheduler.rb +31 -30
- data/test/redis-scheduler.rb +27 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d86d2f1e9780f66e41e977a2f8e282e75b50c3e8
|
4
|
+
data.tar.gz: 9df37814b8decc86caa1a3da82a8500b59653b3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e91b0f8151013796b90d91dc87cf9c12210a68f5e65309064c3ccc93eec0058da8e379ed2cbc07fe1ab154a3cf587b079c8f58d519d26049d9d379cec4abf4a0
|
7
|
+
data.tar.gz: bdb246caeb6415100a6549eb63dd79afe7709031079d33dd233aa8821f2521cdc562c999034cd7e304abe891c82eb02859cb2f4af1830ab3f55cb03fbbd06f24
|
data/COPYING
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Redis Scheduler is copyright (c) 2012 William Morgan <wmorgan@masanjin.net>
|
1
|
+
Redis Scheduler is copyright (c) 2012-2014 William Morgan <wmorgan@masanjin.net>
|
2
2
|
All rights reserved.
|
3
3
|
|
4
4
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
data/lib/redis-scheduler.rb
CHANGED
@@ -67,7 +67,10 @@ class RedisScheduler
|
|
67
67
|
@counter = [@namespace, "counter"].join
|
68
68
|
end
|
69
69
|
|
70
|
-
|
70
|
+
attr_reader :redis, :namespace, :blocking, :uniq, :queue
|
71
|
+
|
72
|
+
## Schedule an item at a specific time. Item is any Ruby object that can
|
73
|
+
## be marshalled.
|
71
74
|
def schedule! item, time
|
72
75
|
@redis.zadd @queue, time.to_f, make_entry(item)
|
73
76
|
end
|
@@ -116,7 +119,7 @@ class RedisScheduler
|
|
116
119
|
## to the schedule happen while iterating.
|
117
120
|
##
|
118
121
|
## For these reasons, this is mainly useful for debugging purposes.
|
119
|
-
def items; ItemEnumerator.new(
|
122
|
+
def items; ItemEnumerator.new(self) end
|
120
123
|
|
121
124
|
## Returns an Array of [item, timestamp, descriptor] tuples representing the
|
122
125
|
## set of in-process items. The timestamp corresponds to the time at which
|
@@ -128,28 +131,38 @@ class RedisScheduler
|
|
128
131
|
end
|
129
132
|
end
|
130
133
|
|
134
|
+
## the inverse of #make_entry below. public because it must also be called by
|
135
|
+
## ItemEnumerator.
|
136
|
+
def parse_entry entry
|
137
|
+
item = if @uniq
|
138
|
+
entry
|
139
|
+
else
|
140
|
+
delim = entry.index ":"
|
141
|
+
entry[(delim + 1) .. -1]
|
142
|
+
end
|
143
|
+
begin
|
144
|
+
Marshal.load item
|
145
|
+
rescue TypeError
|
146
|
+
## fall back to treating the item as a string--it's possible the schedule contains
|
147
|
+
## items inserted from a pre-0.9 release, in which case they'll be stored
|
148
|
+
## as pure strings.
|
149
|
+
item
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
131
153
|
private
|
132
154
|
|
133
155
|
## generate the value actually stored in redis
|
134
156
|
def make_entry item
|
157
|
+
item = Marshal.dump item
|
135
158
|
if @uniq
|
136
|
-
item
|
159
|
+
item
|
137
160
|
else
|
138
161
|
id = @redis.incr @counter
|
139
162
|
"#{id}:#{item}"
|
140
163
|
end
|
141
164
|
end
|
142
165
|
|
143
|
-
## the inverse of #make_item_value
|
144
|
-
def parse_entry entry
|
145
|
-
if @uniq
|
146
|
-
entry
|
147
|
-
else
|
148
|
-
entry =~ /^\d+:(\S+)$/ or raise InvalidEntryException, entry
|
149
|
-
$1
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
166
|
def get descriptor; @blocking ? blocking_get(descriptor) : nonblocking_get(descriptor) end
|
154
167
|
|
155
168
|
def blocking_get descriptor
|
@@ -190,10 +203,8 @@ private
|
|
190
203
|
## Supports random access with #[], with the same caveats as above.
|
191
204
|
class ItemEnumerator
|
192
205
|
include Enumerable
|
193
|
-
def initialize
|
194
|
-
@
|
195
|
-
@q = q
|
196
|
-
@uniq = uniq
|
206
|
+
def initialize scheduler
|
207
|
+
@scheduler = scheduler
|
197
208
|
end
|
198
209
|
|
199
210
|
PAGE_SIZE = 50
|
@@ -207,24 +218,14 @@ private
|
|
207
218
|
end
|
208
219
|
|
209
220
|
def [] start, num=nil
|
210
|
-
elements = @redis.zrange @
|
221
|
+
elements = @scheduler.redis.zrange @scheduler.queue, start, start + (num || 0), :withscores => true
|
211
222
|
v = elements.map do |entry, at|
|
212
|
-
item = parse_entry entry
|
223
|
+
item = @scheduler.parse_entry entry
|
213
224
|
[item, Time.at(at.to_f)]
|
214
225
|
end
|
215
226
|
num ? v : v.first
|
216
227
|
end
|
217
228
|
|
218
|
-
def size; @redis.zcard @
|
219
|
-
|
220
|
-
## duplicated :(
|
221
|
-
def parse_entry entry
|
222
|
-
if @uniq
|
223
|
-
entry
|
224
|
-
else
|
225
|
-
entry =~ /^\d+:(\S+)$/ or raise InvalidEntryException, entry
|
226
|
-
$1
|
227
|
-
end
|
228
|
-
end
|
229
|
+
def size; @scheduler.redis.zcard @scheduler.queue end
|
229
230
|
end
|
230
231
|
end
|
data/test/redis-scheduler.rb
CHANGED
@@ -115,4 +115,31 @@ class RedisSchedulerTest < Minitest::Test
|
|
115
115
|
assert_equal ["b", "a"], items
|
116
116
|
end
|
117
117
|
end
|
118
|
+
|
119
|
+
class Potato
|
120
|
+
def initialize x, y, z
|
121
|
+
@x = x
|
122
|
+
@y = y
|
123
|
+
@z = z
|
124
|
+
end
|
125
|
+
|
126
|
+
attr_reader :x, :y, :z
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_can_schedule_ruby_objects
|
130
|
+
Timecop.freeze TIME do
|
131
|
+
@scheduler.schedule! Potato.new(10, ["hello", :there], /bob/), (TIME + 99)
|
132
|
+
end
|
133
|
+
|
134
|
+
Timecop.freeze(TIME + 99) do
|
135
|
+
potato = nil
|
136
|
+
@scheduler.each do |item, ts|
|
137
|
+
potato = item
|
138
|
+
end
|
139
|
+
|
140
|
+
assert_equal 10, potato.x
|
141
|
+
assert_equal ["hello", :there], potato.y
|
142
|
+
assert_equal /bob/, potato.z
|
143
|
+
end
|
144
|
+
end
|
118
145
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-scheduler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.9'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- William Morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |-
|
14
14
|
A simple, production-ready chronological scheduler for Redis. It allows you to schedule items to be processed at arbitrary points in the future, and easily
|
@@ -23,7 +23,8 @@ files:
|
|
23
23
|
- lib/redis-scheduler.rb
|
24
24
|
- test/redis-scheduler.rb
|
25
25
|
homepage: http://gitub.com/wmorgan/redis-scheduler
|
26
|
-
licenses:
|
26
|
+
licenses:
|
27
|
+
- COPYING
|
27
28
|
metadata: {}
|
28
29
|
post_install_message:
|
29
30
|
rdoc_options:
|