when-do 1.1.4 → 1.1.6
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.
- checksums.yaml +4 -4
- data/lib/when-do.rb +10 -0
- data/lib/when-do/do.rb +6 -3
- data/spec/lib/when-do/do_spec.rb +15 -1
- data/spec/lib/when-do_spec.rb +26 -3
- data/when-do.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7344050b98b79dee12e165ab840351ca4dd7ce9
|
4
|
+
data.tar.gz: db99f14c49e9bea72e747fc8d8c92b1376e1b6e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b9a2ef9663eb74715e5d64fd195a418f039fb0d73538c9590088764d0335bb412a74e237d9ab616148fdf3beeeb90906820634d8945314b2b203e54001a712a
|
7
|
+
data.tar.gz: 6b3504daa5753f736ba81218a770579f48b9643d708dde31842f4d87d2e87ebeb18d259cd992f207836260b5257cbfd9bd1fb307e5950dd70aa2ac5036665e38
|
data/lib/when-do.rb
CHANGED
@@ -3,7 +3,12 @@ require 'json'
|
|
3
3
|
require 'yaml'
|
4
4
|
require 'logger'
|
5
5
|
|
6
|
+
require 'when-cron'
|
7
|
+
|
6
8
|
module When
|
9
|
+
class Error < StandardError; end
|
10
|
+
class InvalidCron < Error; end
|
11
|
+
|
7
12
|
DEFAULT_CONFIG = {
|
8
13
|
schedule_key: 'when:schedules',
|
9
14
|
worker_queue_key: 'when:queue:default',
|
@@ -11,11 +16,16 @@ module When
|
|
11
16
|
}
|
12
17
|
|
13
18
|
def self.schedule(name, cron, klass, *args)
|
19
|
+
raise InvalidCron, "\"#{cron}\" is invalid" unless valid_cron?(cron)
|
14
20
|
schedule = {'class' => klass.to_s, 'cron' => cron, 'args' => args}
|
15
21
|
redis.hset(schedule_key, name.to_s, schedule.to_json)
|
16
22
|
logger.info("Scheduled '#{name}' => #{schedule}.")
|
17
23
|
end
|
18
24
|
|
25
|
+
def self.valid_cron?(cron)
|
26
|
+
When::Cron.valid?(cron)
|
27
|
+
end
|
28
|
+
|
19
29
|
def self.unschedule(name)
|
20
30
|
json_sched = redis.hget(schedule_key, name.to_s)
|
21
31
|
schedule = JSON.parse(json_sched) if json_sched
|
data/lib/when-do/do.rb
CHANGED
@@ -117,9 +117,12 @@ module When
|
|
117
117
|
logger.info("Analyzing #{schedules.count} schedules.")
|
118
118
|
scheduled_jobs = schedules.inject([]) do |jobs, s|
|
119
119
|
schedule = JSON.parse(s)
|
120
|
-
cron = When::Cron.
|
121
|
-
|
122
|
-
|
120
|
+
if cron = When::Cron.valid(schedule['cron'])
|
121
|
+
if cron == started_at
|
122
|
+
jobs << { 'jid' => SecureRandom.uuid, 'class' => schedule['class'], 'args' => schedule['args'] }.to_json
|
123
|
+
end
|
124
|
+
else
|
125
|
+
logger.error { "Could not interpret cron for #{schedule.inspect}" }
|
123
126
|
end
|
124
127
|
jobs
|
125
128
|
end
|
data/spec/lib/when-do/do_spec.rb
CHANGED
@@ -74,7 +74,7 @@ describe When::Do do
|
|
74
74
|
|
75
75
|
context 'a scheduled item does not have a matching cron' do
|
76
76
|
before do
|
77
|
-
When.schedule('test schedule', '0 0
|
77
|
+
When.schedule('test schedule', '0 0 1 1 0', String)
|
78
78
|
end
|
79
79
|
|
80
80
|
it 'does not add an item to the queue' do
|
@@ -83,6 +83,20 @@ describe When::Do do
|
|
83
83
|
.from(nil)
|
84
84
|
end
|
85
85
|
end
|
86
|
+
|
87
|
+
context 'a scheduled item has an erroneous cron' do
|
88
|
+
before do
|
89
|
+
schedule = {'class' => String.to_s, 'cron' => 'this cron is totally wrong', 'args' => []}
|
90
|
+
redis.hset(When.schedule_key, 'test_schedule', schedule.to_json)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'does not add an item to the queue' do
|
94
|
+
expect(When.schedules.count).to eq 1
|
95
|
+
expect { when_do.queue_scheduled(started_at) }
|
96
|
+
.not_to change { redis.lpop(when_do.worker_queue_key) }
|
97
|
+
.from(nil)
|
98
|
+
end
|
99
|
+
end
|
86
100
|
end
|
87
101
|
|
88
102
|
describe '#queue_delayed' do
|
data/spec/lib/when-do_spec.rb
CHANGED
@@ -15,12 +15,35 @@ describe When do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
describe '#schedule' do
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
context 'scheduling a valid cron' do
|
19
|
+
it 'adds data to the schedules hash in redis' do
|
20
|
+
When.schedule('test_schedule', '* * * * *', Object, 'arg1', 'arg2')
|
21
|
+
expect(redis.hget(When.schedule_key, 'test_schedule')).to eq "{\"class\":\"Object\",\"cron\":\"* * * * *\",\"args\":[\"arg1\",\"arg2\"]}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'scheduling an invalid cron' do
|
26
|
+
it 'raises a When::InvalidCron error' do
|
27
|
+
expect {
|
28
|
+
When.schedule('test_schedule', '0 0 0 0 0', Object, 'arg1', 'arg2')
|
29
|
+
}.to raise_error When::InvalidCron
|
30
|
+
end
|
21
31
|
end
|
22
32
|
end
|
23
33
|
|
34
|
+
describe '.valid_cron?' do
|
35
|
+
context 'when cron is valid' do
|
36
|
+
it 'returns true' do
|
37
|
+
expect(When.valid_cron?('* * * * *')).to eq true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when cron is not valid' do
|
42
|
+
it 'returns false' do
|
43
|
+
expect(When.valid_cron?('* * * a* *')).to eq false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
24
47
|
describe '#unschedule' do
|
25
48
|
it 'removes data from the schedules hash in redis' do
|
26
49
|
When.schedule('test_schedule', '* * * * *', Object)
|
data/when-do.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "when-do"
|
7
|
-
spec.version = '1.1.
|
7
|
+
spec.version = '1.1.6'
|
8
8
|
spec.authors = ["TH"]
|
9
9
|
spec.email = ["tylerhartland7@gmail.com"]
|
10
10
|
spec.description = %q{Queues jobs when you want.}
|