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 CHANGED
@@ -16,3 +16,7 @@
16
16
  Jobs.new class: ClassName, method: MethodName, args: MethodArguments
17
17
 
18
18
  Jobs.new at: ScheduleTime
19
+
20
+ == Special
21
+
22
+ Jobs.new class: :eval, method: 'system "ls"'
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 => [:init, :environment] do
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
@@ -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::load(File.read(@data[:dir] + id))
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
- unless Object.const_defined?(job[:class])
118
- job[:status] = :failed
119
- job[:status_msg] = 'Class missing'
120
- next
121
- end
122
-
123
- obj = Object.const_get(job[:class])
124
-
125
- unless obj.respond_to?(job[:method])
126
- job[:status] = :failed
127
- job[:status_msg] = 'Method missing'
128
- next
129
- end
130
-
131
- begin
132
- obj.send job[:method], *job[:args]
133
- rescue => e
134
- job[:status] = :failed
135
- job[:status_msg] = e
136
- next
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
- update
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
@@ -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.6'
6
+ s.version = '0.0.7'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Ben"]
9
9
  s.email = ["ben@zfben.com"]
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.6
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: &13991360 !ruby/object:Gem::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: *13991360
24
+ version_requirements: *18767100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: uuid
27
- requirement: &13990680 !ruby/object:Gem::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: *13990680
35
+ version_requirements: *18780060
36
36
  description: ''
37
37
  email:
38
38
  - ben@zfben.com