cronjobber 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ begin
14
14
  gem.summary = %Q{Cronjob for Rails}
15
15
  gem.description = %Q{Enables simple cronjobs for rails}
16
16
  gem.email = "giniedp@online.de"
17
- gem.homepage = "http://github.com/giniedp/cronjob"
17
+ gem.homepage = "https://github.com/giniedp/cronjobber"
18
18
  gem.authors = ["Alexander Gräfenstein"]
19
19
  # gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
20
20
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
@@ -11,10 +11,8 @@ class Cronjobber::Task < ActiveRecord::Base
11
11
  self.name.underscore
12
12
  end
13
13
 
14
- def self.cronjob
15
- @cronjob ||= self.find_by_name(self.cronjob_name)
16
- @cronjob ||= self.create!(:name => self.cronjob_name)
17
- @cronjob
14
+ def self.current_cronjob
15
+ self.find_by_name(self.cronjob_name) || self.create!(:name => self.cronjob_name)
18
16
  end
19
17
 
20
18
  def self.cronjob_enqueue(key=nil)
@@ -22,7 +20,10 @@ class Cronjobber::Task < ActiveRecord::Base
22
20
  end
23
21
 
24
22
  def self.cronjob_perform
25
- unless cronjob.lock!
23
+ cronjob = self.current_cronjob
24
+ key = DateTime.now.to_i
25
+
26
+ unless cronjob.lock!(key)
26
27
  cronjob.status = "locked"
27
28
  return cronjob
28
29
  end
@@ -34,10 +35,10 @@ class Cronjobber::Task < ActiveRecord::Base
34
35
  end
35
36
 
36
37
  if self.cronjob_delayed
37
- self.cronjob_enqueue(cronjob.locking_key)
38
38
  cronjob.status = "enqueued"
39
39
  cronjob.last_error = nil
40
40
  cronjob.save!
41
+ self.cronjob_enqueue(key)
41
42
  else
42
43
  cronjob.send(self.cronjob_method)
43
44
  cronjob.status = "performed"
@@ -52,16 +53,20 @@ class Cronjobber::Task < ActiveRecord::Base
52
53
  end
53
54
 
54
55
  def self.cronjob_perform_delayed(key)
55
- if cronjob.locked?(key)
56
+ cronjob = self.current_cronjob
57
+
58
+ if cronjob.locked?(key.to_s)
56
59
  cronjob.status = "locked"
57
60
  else
58
61
  cronjob.send(self.cronjob_method)
59
62
  cronjob.status = "performed"
60
63
  cronjob.unlock!
61
64
  end
65
+ return cronjob
62
66
  rescue Exception => exception
63
67
  cronjob.status = "exception"
64
68
  cronjob.unlock!(exception)
69
+ return cronjob
65
70
  end
66
71
 
67
72
  def locked?(key=nil)
@@ -73,47 +78,49 @@ class Cronjobber::Task < ActiveRecord::Base
73
78
  end
74
79
 
75
80
  def lock!(key=nil)
76
- return false if self.locked?
77
- return self.update_attributes!(:locked_at => DateTime.now, :locking_key => Time.now.to_i)
81
+ !self.locked? && self.update_attributes!(:locked_at => DateTime.now, :locking_key => key)
78
82
  end
79
83
 
80
84
  def unlock! exception=nil
81
- return true unless self.locked?
82
- if exception
83
- self.last_error = [exception.message, exception.backtrace].flatten.join("\n")
84
- self.total_failures = self.total_failures.to_i + 1
85
- else
86
- self.last_error = nil
85
+ self.class.transaction do
86
+ if exception
87
+ self.last_error = [exception.message, exception.backtrace].flatten.join("\n")
88
+ self.total_failures = self.total_failures.to_i + 1
89
+ else
90
+ self.last_error = nil
91
+ end
92
+
93
+ if self.status == "performed"
94
+ self.total_runs = self.total_runs.to_i + 1
95
+ self.duration = (Time.now - self.locked_at.to_time) * 1000 if self.locked_at
96
+ self.run_at = Time.now
97
+ end
98
+
99
+ self.locked_at = nil
100
+ self.locking_key = nil
101
+ self.save!
87
102
  end
88
-
89
- self.update_attributes!({
90
- :total_runs => self.total_runs.to_i + 1,
91
- :locked_at => nil,
92
- :locking_key => nil,
93
- :run_at => Time.now,
94
- :duration => (Time.now - self.locked_at.to_time) * 1000
95
- })
96
103
  end
97
104
 
98
105
  def self.cronjob_timepoint t1, t2
99
106
  result = []
100
- t1.to_date.upto(t2.to_date) do |date|
107
+ (t1 - 1.day).to_date.upto((t2 + 1.day).to_date) do |date|
101
108
  self.cronjob_timesteps.each do |time|
102
109
  result << Time.parse([date.year, date.month, date.day].join("-") + " " + time.to_s)
103
110
  end
104
111
  end
105
- result.sort.uniq.select { |time| (time > t1 && time < t2) }.first
112
+ result.sort.uniq.select { |time| (time > t1 && time <= t2) }.first
106
113
  end
107
114
 
108
- def should_run?(run_at=nil)
109
- run_at ||= Time.now
110
- t1, t2 = (self.run_at || run_at), run_at
115
+ def should_run?(at_time=nil)
116
+ t1 = self.run_at || Time.now
117
+ t2 = at_time || Time.now
111
118
 
112
119
  if self.class.cronjob_frequency.to_i == 0
113
120
  if self.class.cronjob_timesteps.empty?
114
121
  true
115
122
  else
116
- self.class.cronjob_timepoint(t1, t2 + 1.day).present?
123
+ self.class.cronjob_timepoint(t1, t2).present?
117
124
  end
118
125
  else
119
126
  if self.class.cronjob_timesteps.empty?
data/cronjobber.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "cronjobber"
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexander Gr\303\244fenstein"]
12
- s.date = "2012-01-11"
12
+ s.date = "2012-01-16"
13
13
  s.description = "Enables simple cronjobs for rails"
14
14
  s.email = "giniedp@online.de"
15
15
  s.extra_rdoc_files = [
@@ -65,7 +65,7 @@ Gem::Specification.new do |s|
65
65
  "spec/dummy/script/rails",
66
66
  "spec/spec_helper.rb"
67
67
  ]
68
- s.homepage = "http://github.com/giniedp/cronjob"
68
+ s.homepage = "https://github.com/giniedp/cronjobber"
69
69
  s.require_paths = ["lib"]
70
70
  s.rubygems_version = "1.8.15"
71
71
  s.summary = "Cronjob for Rails"
@@ -68,7 +68,7 @@ describe Cronjobber::Task do
68
68
 
69
69
  class BackgroundJob < Cronjobber::Task
70
70
  run_task :in_background => true
71
- def self.cronjob_enqueue
71
+ def self.cronjob_enqueue(key=nil)
72
72
  # mock with empty method for testing
73
73
  end
74
74
  end
@@ -100,13 +100,13 @@ describe Cronjobber::Task do
100
100
  end
101
101
 
102
102
  it "should be locked for invalid key" do
103
- @job.lock!.should be true
104
- @job.locked?("123456").should be true
103
+ @job.lock!("123456").should be true
104
+ @job.locked?("654321").should be true
105
105
  end
106
106
 
107
107
  it "should not be locked for valid key" do
108
- @job.lock!.should be true
109
- @job.locked?(@job.locking_key).should be false
108
+ @job.lock!("123456").should be true
109
+ @job.locked?("123456").should be false
110
110
  end
111
111
 
112
112
  it "should perform the job" do
@@ -136,7 +136,8 @@ describe Cronjobber::Task do
136
136
  it "should perform background job with valid key" do
137
137
  job = BackgroundJob.cronjob_perform
138
138
  job.locked?.should be true
139
- BackgroundJob.cronjob_perform_delayed(job.locking_key)
139
+ job.locking_key.blank?.should be false
140
+ job = BackgroundJob.cronjob_perform_delayed(job.locking_key)
140
141
  job.locked?.should be false
141
142
  job.status.should == "performed"
142
143
  job.last_error.should be_nil
@@ -145,7 +146,7 @@ describe Cronjobber::Task do
145
146
  it "should not perform background job with invalidvalid key" do
146
147
  job = BackgroundJob.cronjob_perform
147
148
  job.locked?.should be true
148
- BackgroundJob.cronjob_perform_delayed("some invalid key")
149
+ job = BackgroundJob.cronjob_perform_delayed("some invalid key")
149
150
  job.locked?.should be true
150
151
  job.status.should == "locked"
151
152
  end
@@ -156,20 +157,22 @@ describe Cronjobber::Task do
156
157
  class TestJob < Cronjobber::Task
157
158
  run_task :every => 0.minutes, :at => []
158
159
  end
159
- @test_job = TestJob.new({ :run_at => Time.parse("2011-1-1 12:00") })
160
- @test_job.save!
160
+ TestJob.destroy_all
161
+ TestJob.create!({ :name => TestJob.cronjob_name, :run_at => Time.parse("2011-1-1 12:00") })
161
162
  end
162
163
 
163
164
  it "should be runable before run_at time" do
164
- @test_job.should_run?(Time.parse("2011-1-1 11:00")).should be true
165
+ TestJob.current_cronjob.should_run?(Time.parse("2010-12-31 11:59")).should be true
166
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 11:59")).should be true
165
167
  end
166
168
 
167
169
  it "should be runable on run_at time" do
168
- @test_job.should_run?(Time.parse("2011-1-1 12:00")).should be true
170
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:00")).should be true
169
171
  end
170
172
 
171
173
  it "should be runable after run_at time" do
172
- @test_job.should_run?(Time.parse("2011-1-1 13:00")).should be true
174
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:01")).should be true
175
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-2 12:01")).should be true
173
176
  end
174
177
  end
175
178
 
@@ -178,50 +181,55 @@ describe Cronjobber::Task do
178
181
  class TestJob < Cronjobber::Task
179
182
  run_task :every => 10.minutes, :at => []
180
183
  end
181
- @test_job = TestJob.new({ :run_at => Time.parse("2011-1-1 12:00") })
182
- @test_job.save!
184
+ TestJob.destroy_all
185
+ TestJob.create!({ :name => TestJob.cronjob_name, :run_at => Time.parse("2011-1-1 12:00") })
183
186
  end
184
187
 
185
188
  it "should not be runable before run_at time" do
186
- @test_job.should_run?(Time.parse("2011-1-1 11:00")).should be false
189
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 11:00")).should be false
187
190
  end
188
191
 
189
192
  it "should not be runable on run_at time" do
190
- @test_job.should_run?(Time.parse("2011-1-1 12:00")).should be false
193
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:00")).should be false
191
194
  end
192
195
 
193
196
  it "should not be runable after run_at time before frequency elapses" do
194
- @test_job.should_run?(Time.parse("2011-1-1 12:09")).should be false
197
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:09")).should be false
195
198
  end
196
199
 
197
200
  it "should be runable after run_at time after frequency elapsed" do
198
- @test_job.should_run?(Time.parse("2011-1-1 12:10")).should be true
201
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:10")).should be true
202
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:11")).should be true
199
203
  end
200
204
  end
201
205
 
202
206
  describe "without frequency and with time" do
203
207
  before :each do
204
208
  class TestJob < Cronjobber::Task
205
- run_task :every => 0.minutes, :at => ["12:00"]
209
+ run_task :at => ["12:00"]
206
210
  end
207
- @test_job = TestJob.new({ :run_at => Time.parse("2011-1-1 12:00") })
208
- @test_job.save!
211
+ TestJob.destroy_all
212
+ TestJob.create!({ :name => TestJob.cronjob_name, :run_at => Time.parse("2011-1-1 12:00") })
209
213
  end
210
214
 
211
- it "should not be runable before run_at time" do
212
- @test_job.should_run?(Time.parse("2010-1-1 11:00")).should be false
213
- @test_job.should_run?(Time.parse("2011-1-1 11:00")).should be false
214
- @test_job.should_run?(Time.parse("2011-1-1 11:59")).should be false
215
+ it "should not be runable before next run at time" do
216
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 11:59")).should be false
217
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:00")).should be false
218
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:01")).should be false
219
+
220
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-2 11:59")).should be false
215
221
  end
216
222
 
217
- it "should not be runable on run_at time" do
218
- @test_job.should_run?(Time.parse("2011-1-1 12:00")).should be false
223
+ it "should not be runable on run_at time on same day" do
224
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:00")).should be false
219
225
  end
220
226
 
221
- it "should be runable after run_at time" do
222
- @test_job.should_run?(Time.parse("2011-1-1 12:01")).should be true
223
- @test_job.should_run?(Time.parse("2011-1-1 13:00")).should be true
224
- @test_job.should_run?(Time.parse("2012-1-1 12:01")).should be true
227
+ it "should be runable on next run_at time" do
228
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-2 12:00")).should be true
229
+ end
230
+
231
+ it "should be runable after next run_at time" do
232
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-2 12:01")).should be true
225
233
  end
226
234
  end
227
235
 
@@ -230,26 +238,26 @@ describe Cronjobber::Task do
230
238
  class TestJob < Cronjobber::Task
231
239
  run_task :every => 30.minutes, :at => ["12:00"]
232
240
  end
233
- @test_job = TestJob.new({ :run_at => Time.parse("2011-1-1 12:00") })
234
- @test_job.save!
241
+ TestJob.destroy_all
242
+ TestJob.create!({ :name => TestJob.cronjob_name, :run_at => Time.parse("2011-1-1 12:00") })
235
243
  end
236
244
 
237
245
  it "should not be runable before run_at time" do
238
- @test_job.should_run?(Time.parse("2010-1-1 11:00")).should be false
239
- @test_job.should_run?(Time.parse("2011-1-1 11:00")).should be false
240
- @test_job.should_run?(Time.parse("2011-1-1 11:59")).should be false
246
+ TestJob.current_cronjob.should_run?(Time.parse("2010-1-1 11:00")).should be false
247
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 11:00")).should be false
248
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 11:59")).should be false
241
249
  end
242
250
 
243
251
  it "should not be runable after run_at within frequency time" do
244
- @test_job.should_run?(Time.parse("2011-1-1 12:00")).should be false
245
- @test_job.should_run?(Time.parse("2011-1-1 12:01")).should be false
246
- @test_job.should_run?(Time.parse("2011-1-1 12:29")).should be false
252
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:00")).should be false
253
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:01")).should be false
254
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:29")).should be false
247
255
  end
248
256
 
249
257
  it "should be runable after run_at time after frequency time" do
250
- @test_job.should_run?(Time.parse("2011-1-1 12:30")).should be true
251
- @test_job.should_run?(Time.parse("2011-1-1 13:31")).should be true
252
- @test_job.should_run?(Time.parse("2012-1-1 12:00")).should be true
258
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 12:30")).should be true
259
+ TestJob.current_cronjob.should_run?(Time.parse("2011-1-1 13:31")).should be true
260
+ TestJob.current_cronjob.should_run?(Time.parse("2012-1-1 12:00")).should be true
253
261
  end
254
262
  end
255
263
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cronjobber
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Alexander Gr\xC3\xA4fenstein"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-11 00:00:00 Z
18
+ date: 2012-01-16 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :development
@@ -104,7 +104,7 @@ files:
104
104
  - spec/dummy/public/stylesheets/.gitkeep
105
105
  - spec/dummy/script/rails
106
106
  - spec/spec_helper.rb
107
- homepage: http://github.com/giniedp/cronjob
107
+ homepage: https://github.com/giniedp/cronjobber
108
108
  licenses: []
109
109
 
110
110
  post_install_message: