zfben_rails_jobs 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -0
- data/lib/tasks.rb +5 -6
- data/lib/zfben_rails_jobs.rb +48 -41
- data/zfben_rails_jobs.gemspec +1 -1
- metadata +5 -5
data/README.rdoc
CHANGED
data/lib/tasks.rb
CHANGED
@@ -6,11 +6,6 @@ namespace :jobs do
|
|
6
6
|
else
|
7
7
|
jobs_root = File.realpath('.') << '/tmp/jobs'
|
8
8
|
end
|
9
|
-
|
10
|
-
desc 'Init Jobs Folders'
|
11
|
-
task :init do
|
12
|
-
FileUtils.mkdir(jobs_root) unless File.exists? jobs_root
|
13
|
-
end
|
14
9
|
|
15
10
|
desc 'Start Jobs'
|
16
11
|
task :start do
|
@@ -18,7 +13,11 @@ namespace :jobs do
|
|
18
13
|
end
|
19
14
|
|
20
15
|
desc 'Run Jobs'
|
21
|
-
task :run
|
16
|
+
task :run do
|
17
|
+
if defined? Rails
|
18
|
+
Rake::Task[:environment].execute
|
19
|
+
end
|
20
|
+
FileUtils.mkdir(jobs_root) unless File.exists? jobs_root
|
22
21
|
File.open(jobs_root + '/.lock', 'w'){ |f| f.write Process.pid.to_s }
|
23
22
|
print "Starting jobs at process##{Process.pid}\n"
|
24
23
|
loop do
|
data/lib/zfben_rails_jobs.rb
CHANGED
@@ -4,11 +4,11 @@ require 'yaml'
|
|
4
4
|
|
5
5
|
module ZfbenRailsJobs
|
6
6
|
if defined? Rails
|
7
|
-
class Railtie < Rails::Railtie
|
7
|
+
class Railtie < ::Rails::Railtie
|
8
8
|
railtie_name :zfben_rails_jobs
|
9
|
-
path = File.realpath(File.dirname(__FILE__))
|
9
|
+
path = ::File.realpath(::File.dirname(__FILE__))
|
10
10
|
rake_tasks do
|
11
|
-
require File.join(path, 'tasks.rb')
|
11
|
+
require ::File.join(path, 'tasks.rb')
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -21,7 +21,7 @@ end
|
|
21
21
|
class Jobs
|
22
22
|
def import id
|
23
23
|
if File.exists?(@data[:dir] + id)
|
24
|
-
@data = merge YAML::
|
24
|
+
@data = merge ::YAML::load_file(@data[:dir] + id)
|
25
25
|
true
|
26
26
|
else
|
27
27
|
false
|
@@ -43,7 +43,7 @@ class Jobs
|
|
43
43
|
def save
|
44
44
|
unless locked?
|
45
45
|
@data[:locked] = true
|
46
|
-
File.open(path, 'w'){ |f| f.write @data.to_yaml }
|
46
|
+
::File.open(path, 'w'){ |f| f.write @data.to_yaml }
|
47
47
|
true
|
48
48
|
else
|
49
49
|
false
|
@@ -51,8 +51,8 @@ class Jobs
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def destroy
|
54
|
-
if File.exists? path
|
55
|
-
File.delete path
|
54
|
+
if ::File.exists? path
|
55
|
+
::File.delete path
|
56
56
|
end
|
57
57
|
@data = merge
|
58
58
|
end
|
@@ -78,6 +78,7 @@ class Jobs
|
|
78
78
|
failed = 0
|
79
79
|
pending = 0
|
80
80
|
@data[:list].each{ |job|
|
81
|
+
next if job.nil?
|
81
82
|
if job.has_key? :status
|
82
83
|
case job[:status]
|
83
84
|
when :successed
|
@@ -105,7 +106,7 @@ class Jobs
|
|
105
106
|
|
106
107
|
def run
|
107
108
|
unless @data[:at].nil?
|
108
|
-
if Time.now < @data[:at]
|
109
|
+
if ::Time.now < @data[:at]
|
109
110
|
return false
|
110
111
|
end
|
111
112
|
end
|
@@ -114,33 +115,49 @@ class Jobs
|
|
114
115
|
|
115
116
|
@data[:list].map!{ |job|
|
116
117
|
if !job.has_key?(:status) || job[:status] != :successed
|
117
|
-
|
118
|
-
job[:
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
job[:
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
118
|
+
if job[:class] != :eval
|
119
|
+
unless Object.const_defined?(job[:class])
|
120
|
+
job[:status] = :failed
|
121
|
+
job[:status_msg] = 'Class missing'
|
122
|
+
next
|
123
|
+
end
|
124
|
+
|
125
|
+
obj = Object.const_get(job[:class])
|
126
|
+
|
127
|
+
unless obj.respond_to?(job[:method])
|
128
|
+
job[:status] = :failed
|
129
|
+
job[:status_msg] = 'Method missing'
|
130
|
+
next
|
131
|
+
end
|
132
|
+
|
133
|
+
begin
|
134
|
+
obj.send job[:method], *job[:args]
|
135
|
+
rescue => e
|
136
|
+
job[:status] = :failed
|
137
|
+
job[:status_msg] = e
|
138
|
+
next
|
139
|
+
end
|
140
|
+
else
|
141
|
+
begin
|
142
|
+
Kernel.eval job[:method].to_s
|
143
|
+
rescue => e
|
144
|
+
job[:status] = :failed
|
145
|
+
job[:status_msg] = e
|
146
|
+
next
|
147
|
+
end
|
137
148
|
end
|
138
149
|
job[:status] = :successed
|
139
150
|
end
|
140
151
|
job
|
141
152
|
}
|
142
153
|
|
143
|
-
|
154
|
+
if ::File.exists? path
|
155
|
+
if finished?
|
156
|
+
::File.delete path
|
157
|
+
else
|
158
|
+
::File.open(path, 'w'){ |f| f.write data.to_yaml }
|
159
|
+
end
|
160
|
+
end
|
144
161
|
true
|
145
162
|
end
|
146
163
|
|
@@ -149,7 +166,7 @@ class Jobs
|
|
149
166
|
|
150
167
|
def initialize opts = {}
|
151
168
|
@data = merge opts
|
152
|
-
FileUtils.mkdir(@data[:dir]) unless File.exists?(@data[:dir])
|
169
|
+
::FileUtils.mkdir(@data[:dir]) unless ::File.exists?(@data[:dir])
|
153
170
|
if @data.has_key?(:class)
|
154
171
|
add class: @data[:class], method: @data[:method], args: @data[:args]
|
155
172
|
@data.delete :class
|
@@ -164,17 +181,7 @@ class Jobs
|
|
164
181
|
list: [],
|
165
182
|
at: nil,
|
166
183
|
locked: false,
|
167
|
-
dir: (defined?(::Rails) ? File.realpath(::Rails.root) : File.realpath('.')) << '/tmp/jobs/'
|
184
|
+
dir: (defined?(::Rails) ? ::File.realpath(::Rails.root) : ::File.realpath('.')) << '/tmp/jobs/'
|
168
185
|
}.merge(opts)
|
169
186
|
end
|
170
|
-
|
171
|
-
def update
|
172
|
-
if File.exists? path
|
173
|
-
if finished?
|
174
|
-
File.delete path
|
175
|
-
else
|
176
|
-
File.open(path, 'w'){ |f| f.write data.to_yaml }
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
187
|
end
|
data/zfben_rails_jobs.gemspec
CHANGED
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
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-11-30 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &18767100 !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: *
|
24
|
+
version_requirements: *18767100
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: uuid
|
27
|
-
requirement: &
|
27
|
+
requirement: &18780060 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *18780060
|
36
36
|
description: ''
|
37
37
|
email:
|
38
38
|
- ben@zfben.com
|