resque-timeframe 0.1.0 → 0.2.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/CHANGELOG +5 -0
- data/README.markdown +31 -3
- data/lib/resque-timeframe/timeframe.rb +19 -4
- data/lib/resque-timeframe/version.rb +1 -1
- data/lib/resque-timeframe.rb +1 -0
- data/spec/resque-timeframe/timeframed_job_spec.rb +110 -3
- data/spec/spec_helper.rb +9 -0
- metadata +39 -7
data/CHANGELOG
CHANGED
data/README.markdown
CHANGED
@@ -50,15 +50,41 @@ or like this
|
|
50
50
|
|
51
51
|
@queue = :timeframed_queue
|
52
52
|
|
53
|
-
def self.perform(args)
|
54
|
-
|
53
|
+
def self.perform(args); end
|
54
|
+
end
|
55
|
+
|
56
|
+
class ArchiveJob < Resque::Plugins::TimeframedJob
|
57
|
+
timeframe week - [:saturday, :sunday] => 0..9
|
58
|
+
|
59
|
+
@queue = :timeframed_queue
|
60
|
+
|
61
|
+
def self.perform(args); end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
All timeframed jobs would be delayed in 60 seconds if job out of timeframe. Delay could be configured of set to false. If delay set to false all jobs would be removed from queue.
|
66
|
+
|
67
|
+
class Job < Resque::Plugins::TimeframedJob
|
68
|
+
timeframe :recurrent => 900 # in seconds
|
69
|
+
|
70
|
+
@queue = :timeframed_queue
|
71
|
+
def self.perform(args); end
|
72
|
+
end
|
73
|
+
|
74
|
+
or
|
75
|
+
|
76
|
+
class HaveAChanceJob < Resque::Plugins::TimeframedJob
|
77
|
+
timeframe :recurrent => false
|
78
|
+
|
79
|
+
@queue = :timeframed_queue
|
80
|
+
def self.perform(args); end
|
55
81
|
end
|
56
82
|
|
57
83
|
|
58
84
|
|
59
85
|
Copyright
|
60
86
|
---------
|
61
|
-
Copyright (c) 2010 Dmitry Larkin (at [Railsware][3])
|
87
|
+
Copyright (c) 2010 Dmitry Larkin (at [Railsware][3] for [Ratepoint][4])
|
62
88
|
|
63
89
|
|
64
90
|
|
@@ -66,3 +92,5 @@ Copyright (c) 2010 Dmitry Larkin (at [Railsware][3])
|
|
66
92
|
[1]: http://help.github.com/forking/
|
67
93
|
[2]: http://github.com/dml/resque-timeframe/issues
|
68
94
|
[3]: http://railsware.com
|
95
|
+
[4]: http://ratepoint.com
|
96
|
+
[5]: http://github.com/bvandenbos/resque-scheduler
|
@@ -11,11 +11,22 @@ module Resque
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def settings
|
14
|
-
@options ||= WEEK.inject({:default => true}) {|c,v| c.merge({v => true})}
|
14
|
+
@options ||= WEEK.inject({:default => true, :recurrent => 60}) {|c,v| c.merge({v => true})}
|
15
15
|
end
|
16
16
|
|
17
|
-
def timeframe(options={})
|
18
|
-
|
17
|
+
def timeframe(options = {})
|
18
|
+
options.map do |param,value|
|
19
|
+
case param
|
20
|
+
when Array
|
21
|
+
param.map {|k| timeframe({k => value}) }
|
22
|
+
when String
|
23
|
+
timeframe({param.to_sym => value})
|
24
|
+
when Symbol
|
25
|
+
settings.merge!({param => value})
|
26
|
+
else
|
27
|
+
#
|
28
|
+
end
|
29
|
+
end
|
19
30
|
end
|
20
31
|
|
21
32
|
def allowed_at?(weekday)
|
@@ -52,7 +63,11 @@ module Resque
|
|
52
63
|
end
|
53
64
|
|
54
65
|
def before_perform_timeframe(*args)
|
55
|
-
|
66
|
+
unless allowed_at?(week[Time.new.wday])
|
67
|
+
Resque.enqueue_in(settings[:recurrent], self, *args) if settings[:recurrent]
|
68
|
+
|
69
|
+
raise Resque::Job::DontPerform
|
70
|
+
end
|
56
71
|
end
|
57
72
|
|
58
73
|
end
|
data/lib/resque-timeframe.rb
CHANGED
@@ -2,6 +2,10 @@ require File.join(File.dirname(__FILE__) + '/../spec_helper')
|
|
2
2
|
|
3
3
|
describe Resque::Plugins::Timeframe do
|
4
4
|
|
5
|
+
before(:all) do
|
6
|
+
@monday = Time.mktime(2010, 8, 9, 00, 00, 00)
|
7
|
+
end
|
8
|
+
|
5
9
|
context "Convention" do
|
6
10
|
it "should follow the convention" do
|
7
11
|
lambda {
|
@@ -30,6 +34,88 @@ describe Resque::Plugins::Timeframe do
|
|
30
34
|
end
|
31
35
|
end
|
32
36
|
|
37
|
+
context "Timeframe" do
|
38
|
+
before(:all) do
|
39
|
+
@klass = Class.new
|
40
|
+
@klass.extend Resque::Plugins::Timeframe
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should have defined weeks" do
|
44
|
+
@klass.week.should be_kind_of(Array)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should have defined settings" do
|
48
|
+
@klass.settings.should be_kind_of(Hash)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should default true for a weeks days" do
|
52
|
+
@klass.week.each do |dayoweek|
|
53
|
+
@klass.settings[dayoweek].should be_true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "defining a day timeframe" do
|
58
|
+
before(:each) do
|
59
|
+
@day_of_week = :monday
|
60
|
+
@period = 2..11
|
61
|
+
@klass.timeframe @day_of_week => @period
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should set one day settings" do
|
65
|
+
@klass.settings[@day_of_week].should == @period
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should not affect other day settings" do
|
69
|
+
(@klass.week - [:monday]).each do |dayoweek|
|
70
|
+
@klass.settings[dayoweek].should be_true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "defining few days timeframes" do
|
76
|
+
before(:each) do
|
77
|
+
@days_of_week = [:monday, :wednesday, :friday]
|
78
|
+
@period = 7..19
|
79
|
+
@klass.timeframe @days_of_week => @period
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should set one day settings" do
|
83
|
+
@days_of_week.each do |dayoweek|
|
84
|
+
@klass.settings[dayoweek].should == @period
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should not affect other day settings" do
|
89
|
+
(@klass.week - @days_of_week).each do |dayoweek|
|
90
|
+
@klass.settings[dayoweek].should be_true
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "defining few days timeframes" do
|
96
|
+
before(:each) do
|
97
|
+
@days_of_week1 = [:monday, :wednesday]
|
98
|
+
@day_of_week2 = :friday
|
99
|
+
@period1 = 7..19
|
100
|
+
@period2 = 3..16
|
101
|
+
@klass.timeframe @days_of_week1 => @period1, @day_of_week2 => @period2
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should set one day settings" do
|
105
|
+
@days_of_week1.each do |dayoweek|
|
106
|
+
@klass.settings[dayoweek].should == @period1
|
107
|
+
end
|
108
|
+
@klass.settings[@day_of_week2].should == @period2
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should not affect other day settings" do
|
112
|
+
(@klass.week - @days_of_week1 - [@day_of_week2]).each do |dayoweek|
|
113
|
+
@klass.settings[dayoweek].should be_true
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
33
119
|
context "Settings" do
|
34
120
|
it "should allowed by default" do
|
35
121
|
AllowedByDefaultTimeframeJob.allowed_at?(:monday).should be_true
|
@@ -40,13 +126,13 @@ describe Resque::Plugins::Timeframe do
|
|
40
126
|
end
|
41
127
|
|
42
128
|
it "should allow a job if exact time range specified" do
|
43
|
-
@time =
|
129
|
+
@time = @monday + (60*60 * 24 * 4) + (60*60 * 11) + (60 * 21) # 11:21 @ thursday
|
44
130
|
Time.stub!(:now).and_return(@time)
|
45
131
|
RegularWeekRestrictionJob.allowed_at?(:thursday).should be_true
|
46
132
|
end
|
47
133
|
|
48
134
|
it "should does not allow a job if exact time range specified but out of current time" do
|
49
|
-
@time =
|
135
|
+
@time = @monday + (60*60 * 24 * 5) + (60*60 * 12) + (60 * 33) # 12:33 @ friday
|
50
136
|
Time.stub!(:now).and_return(@time)
|
51
137
|
RegularWeekRestrictionJob.allowed_at?(:friday).should be_false
|
52
138
|
end
|
@@ -57,7 +143,7 @@ describe Resque::Plugins::Timeframe do
|
|
57
143
|
|
58
144
|
before(:each) do
|
59
145
|
Resque.redis.flushall
|
60
|
-
@time =
|
146
|
+
@time = @monday + (60*60 * 24 * 4) + (60*60 * 11) + (60 * 59) # 11:59 @ thursday
|
61
147
|
Time.stub!(:now).and_return(@time)
|
62
148
|
end
|
63
149
|
|
@@ -72,6 +158,27 @@ describe Resque::Plugins::Timeframe do
|
|
72
158
|
result = perform_job(RestrictedByDefaultTimeframeJob, 1)
|
73
159
|
result.should be_false
|
74
160
|
end
|
161
|
+
|
162
|
+
it "should delay job on a minute if job is out of timeframe" do
|
163
|
+
RestrictedByDefaultTimeframeJob.should_not_receive(:perform)
|
164
|
+
Resque.should_receive(:enqueue_in).with(60, RestrictedByDefaultTimeframeJob, 1)
|
165
|
+
result = perform_job(RestrictedByDefaultTimeframeJob, 1)
|
166
|
+
result.should be_false
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should delay job on custom period if job is out of timeframe" do
|
170
|
+
WorkingDaysTimeframeJob.should_not_receive(:perform)
|
171
|
+
Resque.should_receive(:enqueue_in).with(900, WorkingDaysTimeframeJob, 1)
|
172
|
+
result = perform_job(WorkingDaysTimeframeJob, 1)
|
173
|
+
result.should be_false
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should not perform and remove job if :reccurent is false" do
|
177
|
+
JunkTimeframeJob.should_not_receive(:perform)
|
178
|
+
Resque.should_not_receive(:enqueue_in)
|
179
|
+
result = perform_job(JunkTimeframeJob, 1)
|
180
|
+
result.should be_false
|
181
|
+
end
|
75
182
|
end
|
76
183
|
|
77
184
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -65,12 +65,21 @@ class RestrictedByDefaultTimeframeJob < Resque::Plugins::TimeframedJob
|
|
65
65
|
end
|
66
66
|
|
67
67
|
class WorkingDaysTimeframeJob < Resque::Plugins::TimeframedJob
|
68
|
+
timeframe :recurrent => 900
|
68
69
|
timeframe week - [:saturday, :sunday] => 0..9 # 24 hour format
|
69
70
|
|
70
71
|
@queue = :timeframed_queue
|
71
72
|
def self.perform(args); end
|
72
73
|
end
|
73
74
|
|
75
|
+
class JunkTimeframeJob < Resque::Plugins::TimeframedJob
|
76
|
+
timeframe :recurrent => false
|
77
|
+
timeframe week - [:saturday, :sunday] => 0..9
|
78
|
+
|
79
|
+
@queue = :timeframed_queue
|
80
|
+
def self.perform(args); end
|
81
|
+
end
|
82
|
+
|
74
83
|
class DisabledDayTimeframeJob < Resque::Plugins::TimeframedJob
|
75
84
|
timeframe :sunday => true
|
76
85
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resque-timeframe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dmitry Larkin
|
@@ -26,14 +26,46 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 55
|
30
30
|
segments:
|
31
31
|
- 1
|
32
|
-
-
|
32
|
+
- 8
|
33
33
|
- 0
|
34
|
-
version: 1.
|
34
|
+
version: 1.8.0
|
35
35
|
type: :runtime
|
36
36
|
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: resque-scheduler
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 55
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 8
|
49
|
+
- 0
|
50
|
+
version: 1.8.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: resque
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 55
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 8
|
65
|
+
- 0
|
66
|
+
version: 1.8.0
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
37
69
|
description: resque-timeframe is an extension to resque queue system that allow the execution at configured time.
|
38
70
|
email: dmitry.larkin@gmail.com
|
39
71
|
executables: []
|
@@ -58,7 +90,7 @@ files:
|
|
58
90
|
- LICENSE
|
59
91
|
- CHANGELOG
|
60
92
|
has_rdoc: true
|
61
|
-
homepage: http://
|
93
|
+
homepage: http://github.com/dml/resque-timeframe
|
62
94
|
licenses: []
|
63
95
|
|
64
96
|
post_install_message:
|