jobit 0.0.3 → 0.0.4
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.md +6 -0
- data/README.md +14 -6
- data/lib/jobit/job.rb +7 -3
- data/lib/jobit/jobby.rb +14 -13
- data/lib/jobit/jobs/commands.rb +0 -1
- data/lib/jobit/jobs/statuses.rb +8 -0
- data/lib/jobit/storage.rb +2 -1
- data/lib/jobit/version.rb +1 -1
- data/lib/tasks/tasks.rb +0 -1
- data/spec/jobit_spec.rb +68 -18
- metadata +7 -7
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -20,11 +20,11 @@ Create jobs file app/model/jobit_items.rb and put your tasks there:
|
|
20
20
|
```ruby
|
21
21
|
method JobitItems
|
22
22
|
|
23
|
-
def
|
23
|
+
def job_without_output_task(arg)
|
24
24
|
sleep 60
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
27
|
+
def job_with_output_task(arg1, arg2)
|
28
28
|
add_message("job #{name} started\n")
|
29
29
|
set_progress(10)
|
30
30
|
sleep 60
|
@@ -37,6 +37,14 @@ method JobitItems
|
|
37
37
|
end
|
38
38
|
```
|
39
39
|
|
40
|
+
Jobs should be defined as "job_name" + "_task" suffix:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
def new_job_task
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
|
40
48
|
###Running worker to process the jobs queue
|
41
49
|
|
42
50
|
```ruby
|
@@ -48,10 +56,10 @@ rake jobit:work
|
|
48
56
|
|
49
57
|
```ruby
|
50
58
|
#job will be added to queue and processed. After successful processing it will be destroyed
|
51
|
-
new_job = Jobit::Job.add("job_name", :
|
59
|
+
new_job = Jobit::Job.add("job_name", :job_without_output, 'val1')
|
52
60
|
|
53
|
-
#job will be added to queue and processed. After processing
|
54
|
-
new_job = Jobit::Job.add("job_name", :
|
61
|
+
#job will be added to queue and processed. After processing wont be destroyed so you can see outputs from it
|
62
|
+
new_job = Jobit::Job.add("job_name", :job_with_output, 'val1', 'val2'){{ :keep => true }}
|
55
63
|
```
|
56
64
|
|
57
65
|
###Monitoring job progress and responses
|
@@ -61,7 +69,7 @@ new_job = Jobit::Job.add("job_name", :some_long_running_task_with_response, 'val
|
|
61
69
|
job = Jobit::Job.find_by_name('job_name')
|
62
70
|
|
63
71
|
job.status #current job status
|
64
|
-
job.message #job responses
|
72
|
+
job.message #job responses if job sent any
|
65
73
|
job.progress #job progress
|
66
74
|
job.error #job error if status is "failed"
|
67
75
|
```
|
data/lib/jobit/job.rb
CHANGED
@@ -64,12 +64,12 @@ module Jobit
|
|
64
64
|
# Jobit::Job.add(name, object, *args) {{options}}
|
65
65
|
# options:
|
66
66
|
# :priority => the job priority (Integer)
|
67
|
-
# :
|
67
|
+
# :run_at => run job at some time ex: :run_at => Time.now + 4.hours
|
68
68
|
# :schedule => run job at some time. ex: :schedule_at => "16:00"
|
69
69
|
# :repeat => how many times to repeat the job (Integer)
|
70
70
|
# :repeat_delay => delay in seconds before next repeat
|
71
71
|
def self.add(name, object, *args, &block)
|
72
|
-
unless JobitItems.method_defined?(object)
|
72
|
+
unless JobitItems.method_defined?("#{object}_task")
|
73
73
|
raise ArgumentError, "Can't add job #{object}. It's not defined in jobs."
|
74
74
|
end
|
75
75
|
|
@@ -88,10 +88,14 @@ module Jobit
|
|
88
88
|
num.times do
|
89
89
|
jobs = self.where({:status => 'new'})
|
90
90
|
break unless jobs.size > 0
|
91
|
-
|
91
|
+
job = jobs.first
|
92
|
+
next unless job.time_to_run?
|
93
|
+
res = jobs.first.run_job(self.worker_name)
|
94
|
+
next if res.nil?
|
92
95
|
complete += res[0]
|
93
96
|
failed += res[1]
|
94
97
|
reissued += res[2]
|
98
|
+
job = nil
|
95
99
|
break if $exit
|
96
100
|
end
|
97
101
|
[complete, failed, reissued]
|
data/lib/jobit/jobby.rb
CHANGED
@@ -39,15 +39,20 @@ module Jobit
|
|
39
39
|
|
40
40
|
def process_job(args)
|
41
41
|
begin
|
42
|
-
logger.info "* [JOB]
|
42
|
+
logger.info "* [JOB:#{name}] acquiring lock"
|
43
43
|
runtime = Benchmark.realtime do
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
44
|
+
self.status = 'running'
|
45
|
+
self.started_at = Time.now.to_f
|
46
|
+
update
|
47
|
+
|
48
|
+
self.send("#{object}_task",*args)
|
49
|
+
|
50
|
+
self.status = 'complete'
|
51
|
+
self.progress = 100
|
52
|
+
self.stopped_at = Time.now.to_f
|
53
|
+
update
|
49
54
|
end
|
50
|
-
logger.info "* [JOB
|
55
|
+
logger.info "* [JOB:#{name}] completed after %.4f" % runtime
|
51
56
|
rescue Exception => e
|
52
57
|
log_exception(e)
|
53
58
|
msg = "#{e.message}\n\n#{e.backtrace.join("\n")}"
|
@@ -68,8 +73,6 @@ module Jobit
|
|
68
73
|
increase_tries
|
69
74
|
sleep delay if delay > 0
|
70
75
|
end
|
71
|
-
#p self.class.send(:method_defined?, :perform)
|
72
|
-
#p self.class.send(:undef_method, :perform)
|
73
76
|
cleanup
|
74
77
|
end
|
75
78
|
|
@@ -91,7 +94,7 @@ module Jobit
|
|
91
94
|
self.message = ''
|
92
95
|
self.progress = 0
|
93
96
|
self.priority = 0
|
94
|
-
self.run_at =
|
97
|
+
self.run_at = id
|
95
98
|
self.repeat = 0
|
96
99
|
self.repeat_delay = 0
|
97
100
|
self.created_at = id
|
@@ -117,7 +120,7 @@ module Jobit
|
|
117
120
|
if failed?
|
118
121
|
if tries < MAX_ATTEMPTS
|
119
122
|
self.tries += 1
|
120
|
-
self.run_at = Time.now + (tries ** 4) + 5
|
123
|
+
self.run_at = Time.now.to_f + (tries ** 4) + 5
|
121
124
|
self.status = 'new'
|
122
125
|
unlock!
|
123
126
|
update
|
@@ -129,8 +132,6 @@ module Jobit
|
|
129
132
|
end
|
130
133
|
elsif complete?
|
131
134
|
if keep?
|
132
|
-
set_progress(100)
|
133
|
-
set_stop_time
|
134
135
|
unlock!
|
135
136
|
else
|
136
137
|
destroy
|
data/lib/jobit/jobs/commands.rb
CHANGED
data/lib/jobit/jobs/statuses.rb
CHANGED
data/lib/jobit/storage.rb
CHANGED
@@ -4,7 +4,8 @@ module Jobit
|
|
4
4
|
|
5
5
|
def self.all_files
|
6
6
|
Dir.mkdir(Jobit::Job.jobs_path) unless Dir.exist?(Jobit::Job.jobs_path)
|
7
|
-
Dir.glob("#{Jobit::Job.jobs_path}/*").find_all { |x| File.file? x }
|
7
|
+
arr = Dir.glob("#{Jobit::Job.jobs_path}/*").find_all { |x| File.file? x }
|
8
|
+
arr.sort
|
8
9
|
end
|
9
10
|
|
10
11
|
def self.destroy_all
|
data/lib/jobit/version.rb
CHANGED
data/lib/tasks/tasks.rb
CHANGED
data/spec/jobit_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
3
3
|
|
4
4
|
module JobitItems
|
5
5
|
|
6
|
-
def
|
6
|
+
def good_job_task(opt1, opt2)
|
7
7
|
set_progress(10)
|
8
8
|
unless opt1 == 'val1'
|
9
9
|
raise(NoMethodError, 'wrong arguments 0')
|
@@ -14,7 +14,7 @@ module JobitItems
|
|
14
14
|
set_progress(100)
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def bad_job_task(text)
|
18
18
|
add_message "start"
|
19
19
|
set_progress(10)
|
20
20
|
add_message "end"
|
@@ -23,7 +23,20 @@ module JobitItems
|
|
23
23
|
end
|
24
24
|
|
25
25
|
describe Jobit do
|
26
|
-
|
26
|
+
|
27
|
+
after(:all) do
|
28
|
+
begin
|
29
|
+
files = Jobit::Storage.all_files
|
30
|
+
for file in files
|
31
|
+
File.delete(file)
|
32
|
+
end
|
33
|
+
Dir.delete(Jobit::Job.jobs_path)
|
34
|
+
rescue
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "Jobit::Job basic stuff" do
|
27
40
|
|
28
41
|
before(:each) do
|
29
42
|
@job_name = 'job-1'
|
@@ -37,14 +50,6 @@ describe Jobit do
|
|
37
50
|
@new_job.destroy
|
38
51
|
end
|
39
52
|
|
40
|
-
after(:all) do
|
41
|
-
begin
|
42
|
-
Dir.delete(Jobit::JOBS_PATH)
|
43
|
-
rescue
|
44
|
-
nil
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
53
|
it "creates new job" do
|
49
54
|
@new_job.name.should eq(@job_name)
|
50
55
|
end
|
@@ -130,11 +135,11 @@ describe Jobit do
|
|
130
135
|
|
131
136
|
it 'adds message' do
|
132
137
|
@new_job.message.should eq('')
|
133
|
-
@new_job.add_message('hello',true)
|
138
|
+
@new_job.add_message('hello', true)
|
134
139
|
@new_job.message.should eq('hello')
|
135
140
|
job = Jobit::Job.find_by_name(@job_name)
|
136
141
|
job.message.should eq('hello')
|
137
|
-
@new_job.add_message(' there',true)
|
142
|
+
@new_job.add_message(' there', true)
|
138
143
|
@new_job.message.should eq('hello there')
|
139
144
|
job = Jobit::Job.find_by_name(@job_name)
|
140
145
|
job.message.should eq('hello there')
|
@@ -146,6 +151,10 @@ describe Jobit do
|
|
146
151
|
job.should eq(nil)
|
147
152
|
end
|
148
153
|
|
154
|
+
end
|
155
|
+
|
156
|
+
describe "Jobit::Job work_off and different runs" do
|
157
|
+
|
149
158
|
it 'running good job 5 times' do
|
150
159
|
job = Jobit::Job.add('good_job', :good_job, 'val1', 'val2') { {
|
151
160
|
:priority => 0,
|
@@ -204,7 +213,6 @@ describe Jobit do
|
|
204
213
|
end
|
205
214
|
|
206
215
|
it 'work_off test' do
|
207
|
-
@new_job.destroy
|
208
216
|
for i in (0..5)
|
209
217
|
Jobit::Job.add("work_off_#{i}", :good_job, 'val1', 'val2') { {
|
210
218
|
:priority => rand(6),
|
@@ -215,14 +223,26 @@ describe Jobit do
|
|
215
223
|
:priority => 2,
|
216
224
|
:keep => true
|
217
225
|
} }
|
226
|
+
jobs = []
|
227
|
+
for i in (0..5)
|
228
|
+
jobs[i] = Jobit::Job.find_by_name("work_off_#{i}")
|
229
|
+
jobs[i].tries.should eq(0), "#{jobs[i]}"
|
230
|
+
jobs[i].status.should eq('new'), "#{jobs[i]}"
|
231
|
+
jobs[i].run_at.should_not eq(nil)
|
232
|
+
jobs[i].started_at.should eq(nil)
|
233
|
+
jobs[i].run_at.should be < Time.now.to_f
|
234
|
+
end
|
218
235
|
|
219
236
|
Jobit::Job.work_off
|
237
|
+
|
220
238
|
jobs = []
|
221
239
|
for i in (0..5)
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
240
|
+
jobs[i] = Jobit::Job.find_by_name("work_off_#{i}")
|
241
|
+
jobs[i].run_at.should be < Time.now.to_f
|
242
|
+
jobs[i].tries.should eq(1), "#{jobs[i]}"
|
243
|
+
jobs[i].status.should eq('complete'), "#{jobs[i]}"
|
244
|
+
jobs[i].destroy
|
245
|
+
jobs[i].id.should eq(nil)
|
226
246
|
end
|
227
247
|
|
228
248
|
job_failed = Jobit::Job.find_by_name('work_off_bad')
|
@@ -232,5 +252,35 @@ describe Jobit do
|
|
232
252
|
|
233
253
|
end
|
234
254
|
|
255
|
+
it 'new job should have run_at after creation and started_at after running' do
|
256
|
+
job = Jobit::Job.add('good_job_keep_11', :good_job, 'val1', 'val2') { {
|
257
|
+
:keep => true
|
258
|
+
} }
|
259
|
+
job.run_at.should_not eq(nil)
|
260
|
+
job.run_job
|
261
|
+
job.started_at.should_not eq(nil)
|
262
|
+
job = Jobit::Job.find_by_name('good_job_keep_11')
|
263
|
+
job.tries.should eq(1)
|
264
|
+
job.run_at.should_not eq(nil)
|
265
|
+
job.started_at.should_not eq(nil)
|
266
|
+
job.destroy
|
267
|
+
end
|
268
|
+
|
269
|
+
it 'Job should not be processed if run_at set to later time' do
|
270
|
+
run_at_time = Time.now.to_f + 10000
|
271
|
+
job = Jobit::Job.add('test_run_at', :good_job, 'val1', 'val2') { {
|
272
|
+
:run_at => run_at_time
|
273
|
+
} }
|
274
|
+
job.run_at.should eq(run_at_time)
|
275
|
+
|
276
|
+
Jobit::Job.work_off
|
277
|
+
|
278
|
+
job = Jobit::Job.find_by_name('test_run_at')
|
279
|
+
job.tries.should eq(0)
|
280
|
+
job.run_at.should eq(run_at_time)
|
281
|
+
job.started_at.should eq(nil)
|
282
|
+
job.destroy
|
283
|
+
end
|
235
284
|
end
|
285
|
+
|
236
286
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jobit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-12-04 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: daemons
|
16
|
-
requirement: &
|
16
|
+
requirement: &2158419860 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2158419860
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &2158419440 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2158419440
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &2158419020 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2158419020
|
47
47
|
description: Process background jobs in queue.
|
48
48
|
email:
|
49
49
|
- v.t.g.m.b.x@gmail.com
|