zfben_rails_jobs 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -12,10 +12,7 @@
12
12
  bundle install
13
13
 
14
14
  == How to use
15
-
16
- class Sth
17
- def todo args
18
- end
19
- end
20
15
 
21
- Jobs.new.add(Sth, :todo, args)
16
+ Jobs.new class: ClassName, method: MethodName, args: MethodArguments
17
+
18
+ Jobs.new at: ScheduleTime
data/lib/tasks.rb CHANGED
@@ -1,5 +1,11 @@
1
+ require 'fileutils'
2
+
1
3
  namespace :jobs do
2
- jobs_root = Rails.root.join('tmp/jobs').to_s
4
+ if defined? Rails
5
+ jobs_root = Rails.root.join('tmp/jobs').to_s
6
+ else
7
+ jobs_root = File.realpath('.') << '/tmp/jobs'
8
+ end
3
9
 
4
10
  desc 'Init Jobs Folders'
5
11
  task :init do
@@ -16,7 +22,7 @@ namespace :jobs do
16
22
  File.open(jobs_root + '/.lock', 'w'){ |f| f.write Process.pid.to_s }
17
23
  print "Starting jobs at process##{Process.pid}\n"
18
24
  loop do
19
- list = Dir[jobs_root + '*']
25
+ list = Dir.glob(jobs_root + '/[a-z0-9-]*')
20
26
  if list.length > 0
21
27
  print "#{list.length} jobs found, running..\n"
22
28
  list.each do |id|
@@ -24,10 +30,8 @@ namespace :jobs do
24
30
  jobs = Jobs.new
25
31
  jobs.import File.basename(id)
26
32
  print "Jobs detail: #{jobs.data}\n"
27
- jobs.run
33
+ print "Jobs result: #{jobs.run}\n"
28
34
  end
29
- else
30
- print "No job found, skipping..\n"
31
35
  end
32
36
  sleep 10
33
37
  end
@@ -15,20 +15,26 @@ module ZfbenRailsJobs
15
15
  end
16
16
  end
17
17
 
18
+ if !defined?(Rails) && defined?(Rake)
19
+ require File.join(File.dirname(__FILE__), 'tasks.rb')
20
+ end
21
+
18
22
  class Jobs
19
23
  def import id
20
- if File.exists?(@path + id)
21
- yaml = YAML::load(File.read(@path + id))
22
- @id = yaml[:id]
23
- @list = yaml[:list]
24
+ if File.exists?(@data[:dir] + id)
25
+ @data = merge YAML::load(File.read(@data[:dir] + id))
26
+ true
24
27
  else
25
28
  false
26
29
  end
27
30
  end
28
31
 
29
- def add cls, method, args = nil
32
+ def add opts = {}
30
33
  unless locked?
31
- @list.push [cls.to_s, method, args]
34
+ [:class, :method].each{ |key|
35
+ opts[key] = opts[key].to_s.to_sym
36
+ }
37
+ @data[:list].push opts
32
38
  true
33
39
  else
34
40
  false
@@ -37,40 +43,56 @@ class Jobs
37
43
 
38
44
  def save
39
45
  unless locked?
40
- @locked = true
41
- File.open(@path + @id, 'w'){ |f| f.write data.to_yaml }
46
+ @data[:locked] = true
47
+ File.open(path, 'w'){ |f| f.write @data.to_yaml }
42
48
  true
43
49
  else
44
50
  false
45
51
  end
46
52
  end
47
53
 
54
+ def destroy
55
+ if File.exists? path
56
+ File.delete path
57
+ end
58
+ @data = merge
59
+ end
60
+
48
61
  def id
49
- @id
62
+ @data[:id]
50
63
  end
51
64
 
52
65
  def list
53
- @list
66
+ @data[:list]
54
67
  end
55
68
 
56
69
  def data
57
- { id: @id, list: @list, at: @at }
70
+ @data
71
+ end
72
+
73
+ def path
74
+ @data[:dir] + @data[:id]
58
75
  end
59
76
 
60
77
  def result
61
78
  successed = 0
62
79
  failed = 0
63
80
  pending = 0
64
- @list.map{ |l|
65
- if l.length < 4
66
- pending = pending + 1
67
- elsif l[3]
68
- successed = successed + 1
81
+ @data[:list].each{ |job|
82
+ if job.has_key? :status
83
+ case job[:status]
84
+ when :successed
85
+ successed = successed + 1
86
+ when :failed
87
+ failed = failed + 1
88
+ else
89
+ pending = pending + 1
90
+ end
69
91
  else
70
- failed = failed + 1
92
+ pending = pending + 1
71
93
  end
72
94
  }
73
- { successed: successed, failed: failed, pending: pending }
95
+ { successed: successed, failed: failed, pending: pending, total: @data[:list].length }
74
96
  end
75
97
 
76
98
  def finished?
@@ -79,41 +101,45 @@ class Jobs
79
101
  end
80
102
 
81
103
  def locked?
82
- @locked
104
+ @data[:locked]
83
105
  end
84
106
 
85
107
  def run
86
- unless @at.nil?
87
- if Time.now < @at
108
+ unless @data[:at].nil?
109
+ if Time.now < @data[:at]
88
110
  return false
89
111
  end
90
112
  end
91
113
 
92
- @locked = true
114
+ @data[:locked] = true
93
115
 
94
- @list.each_index do |i|
95
- if !@list[i][3]
96
- unless Object.const_defined?(@list[i][0])
97
- @list[i].push false, 'Class missing'
116
+ @data[:list].map!{ |job|
117
+ if !job.has_key?(:status) || job[:status] != :successed
118
+ unless Object.const_defined?(job[:class])
119
+ job[:status] = :failed
120
+ job[:status_msg] = 'Class missing'
98
121
  next
99
122
  end
100
123
 
101
- obj = Object.const_get(@list[i][0])
124
+ obj = Object.const_get(job[:class])
102
125
 
103
- unless obj.respond_to?(@list[i][1])
104
- @list[i].push false, 'Method missing'
126
+ unless obj.respond_to?(job[:method])
127
+ job[:status] = :failed
128
+ job[:status_msg] = 'Method missing'
105
129
  next
106
130
  end
107
131
 
108
132
  begin
109
- obj.send @list[i][1], *@list[i][2]
133
+ obj.send job[:method], *job[:args]
110
134
  rescue => e
111
- @list[i].push false, e
135
+ job[:status] = :failed
136
+ job[:status_msg] = e
112
137
  next
113
138
  end
114
- @list[i].push true, 'Finished'
139
+ job[:status] = :successed
115
140
  end
116
- end
141
+ job
142
+ }
117
143
 
118
144
  update
119
145
  true
@@ -121,21 +147,34 @@ class Jobs
121
147
 
122
148
  private
123
149
 
124
- def initialize at = nil
125
- @id = UUID.new.generate
126
- @list = []
127
- @path = File.realpath(Rails.root) + '/tmp/jobs/'
128
- FileUtils.mkdir(@path) unless File.exists?(@path)
129
- @locked = false
130
- @at = at
150
+
151
+ def initialize opts = {}
152
+ @data = merge opts
153
+ FileUtils.mkdir(@data[:dir]) unless File.exists?(@data[:dir])
154
+ if @data.has_key?(:class)
155
+ add class: @data[:class], method: @data[:method], args: @data[:args]
156
+ @data.delete :class
157
+ @data.delete :method
158
+ @data.delete :args
159
+ end
160
+ end
161
+
162
+ def merge opts = {}
163
+ {
164
+ id: UUID.new.generate,
165
+ list: [],
166
+ at: nil,
167
+ locked: false,
168
+ dir: (defined?(::Rails) ? File.realpath(::Rails.root) : File.realpath('.')) << '/tmp/jobs/'
169
+ }.merge(opts)
131
170
  end
132
171
 
133
172
  def update
134
- if File.exists? @path + @id
173
+ if File.exists? path
135
174
  if finished?
136
- File.delete @path + @id
175
+ File.delete path
137
176
  else
138
- File.open(@path + @id, 'w'){ |f| f.write data.to_yaml }
177
+ File.open(path, 'w'){ |f| f.write data.to_yaml }
139
178
  end
140
179
  end
141
180
  end
data/test/jobs_test.rb CHANGED
@@ -15,31 +15,33 @@ class JobsTest < Test::Unit::TestCase
15
15
 
16
16
  def test_jobs_add
17
17
  jobs = Jobs.new
18
- assert jobs.add Example, :job_0
19
- assert_equal jobs.list, [['Example', :job_0, nil]]
20
- assert jobs.add Example, :job, 5
21
- assert_equal jobs.list, [['Example', :job_0, nil], ['Example', :job, 5]]
18
+ assert jobs.add class: Example, method: :job_0, args: nil
19
+ assert_equal jobs.list, Jobs.new(class: Example, method: :job_0).list
20
+ assert jobs.add class: Example, method: :job, args: 5
21
+ assert_equal jobs.list, [{class: :Example, method: :job_0, args: nil}, {class: :Example, method: :job, args: 5}]
22
22
  end
23
23
 
24
24
  def test_jobs_save
25
25
  jobs = Jobs.new
26
- jobs.add Example, :job_0
26
+ jobs.add class: Example, method: :job_0
27
27
  assert jobs.save
28
+ jobs.destroy
28
29
  end
29
30
 
30
31
  def test_jobs_import
31
32
  jobs = Jobs.new
32
- jobs.add Example, :job_0
33
+ jobs.add class: Example, method: :job_0
33
34
  jobs.save
34
35
  jobs2 = Jobs.new
35
36
  assert jobs2.import(jobs.id)
36
37
  assert_equal jobs.data, jobs2.data
38
+ jobs.destroy
37
39
  end
38
40
 
39
41
  def test_jobs_run
40
42
  jobs = Jobs.new
41
- jobs.add Example, :job_0
42
- jobs.add Example, :job, 1
43
+ jobs.add class: Example, method: :job_0
44
+ jobs.add class: Example, method: :job, args: 1
43
45
  jobs.save
44
46
  assert jobs.run
45
47
  assert_equal jobs.result[:successed], 2
@@ -47,12 +49,13 @@ class JobsTest < Test::Unit::TestCase
47
49
  jobs2.import jobs.id
48
50
  assert jobs2.run
49
51
  assert_equal jobs.result[:successed], 2
52
+ jobs.destroy
50
53
  end
51
54
 
52
55
  def test_jobs_run_at
53
56
  at = 1.seconds.from_now
54
- jobs = Jobs.new(at)
55
- jobs.add Example, :job_0
57
+ jobs = Jobs.new at: at
58
+ jobs.add class: Example, method: :job_0
56
59
  assert !jobs.run
57
60
  sleep 2
58
61
  assert jobs.run
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "zfben_rails_jobs"
6
- s.version = '0.0.4'
6
+ s.version = '0.0.5'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Ben"]
9
9
  s.email = ["ben@zfben.com"]
@@ -28,6 +28,4 @@ Gem::Specification.new do |s|
28
28
 
29
29
  s.add_dependency 'rails', '>=3.1.0'
30
30
  s.add_dependency 'uuid'
31
- s.add_dependency 'whenever'
32
- s.add_dependency 'rainbow'
33
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zfben_rails_jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-29 00:00:00.000000000 Z
12
+ date: 2011-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &12197140 !ruby/object:Gem::Requirement
16
+ requirement: &12519240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *12197140
24
+ version_requirements: *12519240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: uuid
27
- requirement: &12222060 !ruby/object:Gem::Requirement
27
+ requirement: &12518680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,29 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *12222060
36
- - !ruby/object:Gem::Dependency
37
- name: whenever
38
- requirement: &12220240 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
44
- type: :runtime
45
- prerelease: false
46
- version_requirements: *12220240
47
- - !ruby/object:Gem::Dependency
48
- name: rainbow
49
- requirement: &12218100 !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :runtime
56
- prerelease: false
57
- version_requirements: *12218100
35
+ version_requirements: *12518680
58
36
  description: ''
59
37
  email:
60
38
  - ben@zfben.com