simple_worker 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,21 +9,21 @@ module SimpleWorker
9
9
 
10
10
  class << self
11
11
  attr_accessor :subclass, :caller_file
12
- @merged = []
13
- @merged_workers = []
12
+ @merged = {}
13
+ @merged_workers = {}
14
14
  @merged_gems = []
15
15
  @merged_mailers = []
16
16
  @merged_folders = {}
17
- @unmerged = []
17
+ @unmerged = {}
18
18
  @unmerged_gems = []
19
19
 
20
20
  def reset!
21
- @merged = []
22
- @merged_workers = []
21
+ @merged = {}
22
+ @merged_workers = {}
23
23
  @merged_gems = []
24
24
  @merged_mailers = []
25
25
  @merged_folders = {}
26
- @unmerged = []
26
+ @unmerged = {}
27
27
  @unmerged_gems = []
28
28
  end
29
29
 
@@ -45,6 +45,7 @@ module SimpleWorker
45
45
  end
46
46
 
47
47
  def check_for_file(f)
48
+ SimpleWorker.logger.debug 'Checking for ' + f.to_s
48
49
  f = f.to_str
49
50
  unless ends_with?(f, ".rb")
50
51
  f << ".rb"
@@ -56,7 +57,7 @@ module SimpleWorker
56
57
  # try relative
57
58
  # p caller
58
59
  f2 = File.join(File.dirname(caller[3]), f)
59
- # puts 'f2=' + f2
60
+ puts 'f2=' + f2
60
61
  if File.exist? f2
61
62
  exists = true
62
63
  f = f2
@@ -70,7 +71,7 @@ module SimpleWorker
70
71
  f
71
72
  end
72
73
 
73
- # merges the specified gem.
74
+ # merges the specified gem.
74
75
  def merge_gem(gem_name, options={})
75
76
  gem_info = {:name=>gem_name, :merge=>true}
76
77
  if options.is_a?(Hash)
@@ -85,9 +86,12 @@ module SimpleWorker
85
86
  end
86
87
  gem_info[:path] = path
87
88
  @merged_gems << gem_info
88
- puts 'before require ' + (options[:require] || gem_name)
89
- require options[:require] || gem_name
90
- puts 'required yo'
89
+ #puts 'before require ' + (options[:require] || gem_name)
90
+ begin
91
+ require options[:require] || gem_name
92
+ rescue LoadError=>ex
93
+ raise "Gem #{gem_name} was found, but we could not load the file '#{options[:require] || gem_name}.rb'. You may need to use :require=>x.........."
94
+ end
91
95
  end
92
96
 
93
97
 
@@ -96,7 +100,7 @@ module SimpleWorker
96
100
  @unmerged_gems << gem_info
97
101
  end
98
102
 
99
- #merge action_mailer mailers
103
+ #merge action_mailer mailers
100
104
  def merge_mailer(mailer, params={})
101
105
  check_for_file mailer
102
106
  basename = File.basename(mailer, File.extname(mailer))
@@ -106,29 +110,45 @@ module SimpleWorker
106
110
 
107
111
  def merge_folder(path)
108
112
  files = []
109
- Dir["#{path}*.rb"].each do |f|
110
- f = check_for_file(f)
111
- files<<f
113
+ #puts "caller_file=" + caller_file
114
+ if path[0, 1] == '/'
115
+ abs_dir = path
116
+ else # relative
117
+ abs_dir = File.join(File.dirname(caller_file), path)
118
+ end
119
+ #puts 'abs_dir=' + abs_dir
120
+ raise "Folder not found for merge_folder #{path}!" unless File.directory?(abs_dir)
121
+ rbfiles = File.join(abs_dir, "*.rb")
122
+ Dir[rbfiles].each do |f|
123
+ #f2 = check_for_file(f)
124
+ #puts "f2=#{f2}"
125
+ merge(f)
126
+ #files << f
127
+ #@merged[f]
112
128
  end
113
- @merged_folders[path]=files unless files.empty?
114
- SimpleWorker.logger.info "Merged folders! #{@merged_folders.inspect}"
129
+ #@merged_folders[path] = files unless files.empty?
130
+ #SimpleWorker.logger.info "Merged folders! #{@merged_folders.inspect}"
115
131
  end
116
132
 
117
- # merges the specified files.
118
- # todo: don't allow multiple files per merge, just one like require
133
+ # merges the specified file.
134
+ #
135
+ # Example: merge 'models/my_model'
119
136
  def merge(*files)
137
+ ret = nil
120
138
  files.each do |f|
121
- f = check_for_file(f)
122
- @merged << f
139
+ f2 = check_for_file(f)
140
+ ret = {:name=>f, :path=>f2}
141
+ @merged[File.basename(f2)] = ret
123
142
  end
143
+ ret
124
144
  end
125
145
 
126
- # Opposite of merge, this will omit the files you specify from being merged in. Useful in Rails apps
127
- # where a lot of things are auto-merged by default like your models.
146
+ # Opposite of merge, this will omit the files you specify from being merged in. Useful in Rails apps
147
+ # where a lot of things are auto-merged by default like your models.
128
148
  def unmerge(*files)
129
149
  files.each do |f|
130
- f = check_for_file(f)
131
- @unmerged << f
150
+ f2 = check_for_file(f)
151
+ @unmerged[File.basename(f2)] = {:name=>f, :path=>f2}
132
152
  end
133
153
  end
134
154
 
@@ -137,15 +157,18 @@ module SimpleWorker
137
157
  s[-suffix.length, suffix.length] == suffix
138
158
  end
139
159
 
140
- # Use this to merge in other workers. These are treated differently the normal merged files because
141
- # they will be uploaded separately and treated as distinctly separate workers.
142
- #
143
- # file: This is the path to the file, just like merge.
144
- # class_name: eg: 'MyWorker'.
160
+ # Use this to merge in other workers. These are treated differently the normal merged files because
161
+ # they will be uploaded separately and treated as distinctly separate workers.
162
+ #
163
+ # file: This is the path to the file, just like merge.
164
+ # class_name: eg: 'MyWorker'.
145
165
  def merge_worker(file, class_name)
146
166
  # puts 'merge_worker in ' + self.name
147
- merge(file)
148
- @merged_workers << [File.expand_path(file), class_name]
167
+ ret = merge(file)
168
+ ret[:class_name] = class_name
169
+ #[File.expand_path(file), class_name]
170
+ @merged_workers[file] = ret
171
+ ret
149
172
  end
150
173
  end
151
174
 
@@ -170,7 +193,7 @@ module SimpleWorker
170
193
  self.class.instance_variable_defined?(:@uploaded) && self.class.instance_variable_get(:@uploaded)
171
194
  end
172
195
 
173
- # Call this if you want to run locally and get some extra features from this gem like global attributes.
196
+ # Call this if you want to run locally and get some extra features from this gem like global attributes.
174
197
  def run_local
175
198
  # puts 'run_local'
176
199
  set_auto_attributes
@@ -216,11 +239,11 @@ module SimpleWorker
216
239
  queue(options)
217
240
  end
218
241
 
219
- # Call this to queue up your job to SimpleWorker cloud.
220
- # options:
221
- # :priority => 0, 1 or 2. Default is 0.
222
- # :recursive => true/false. Default is false. If you queue up a worker that is the same class as the currently
223
- # running worker, it will be rejected unless you set this explicitly so we know you meant to do it.
242
+ # Call this to queue up your job to SimpleWorker cloud.
243
+ # options:
244
+ # :priority => 0, 1 or 2. Default is 0.
245
+ # :recursive => true/false. Default is false. If you queue up a worker that is the same class as the currently
246
+ # running worker, it will be rejected unless you set this explicitly so we know you meant to do it.
224
247
  def queue(options={})
225
248
  # puts 'in queue'
226
249
  set_auto_attributes
@@ -257,9 +280,9 @@ module SimpleWorker
257
280
  false
258
281
  end
259
282
 
260
- # will return after job has completed or errored out.
261
- # Returns status.
262
- # todo: add a :timeout option
283
+ # will return after job has completed or errored out.
284
+ # Returns status.
285
+ # todo: add a :timeout option
263
286
  def wait_until_complete
264
287
  check_service
265
288
  tries = 0
@@ -281,17 +304,17 @@ module SimpleWorker
281
304
  upload_if_needed
282
305
  end
283
306
 
284
- #
285
- # schedule: hash of scheduling options that can include:
286
- # Required:
287
- # - start_at: Time of first run - DateTime or Time object.
288
- # Optional:
289
- # - run_every: Time in seconds between runs. If ommitted, task will only run once.
290
- # - delay_type: Fixed Rate or Fixed Delay. Default is fixed_delay.
291
- # - end_at: Scheduled task will stop running after this date (optional, if ommitted, runs forever or until cancelled)
292
- # - run_times: Task will run exactly :run_times. For instance if :run_times is 5, then the task will run 5 times.
293
- # - name: Provide a name for the schedule, defaults to class name. Use this if you want more than one schedule per worker class.
294
- #
307
+ #
308
+ # schedule: hash of scheduling options that can include:
309
+ # Required:
310
+ # - start_at: Time of first run - DateTime or Time object.
311
+ # Optional:
312
+ # - run_every: Time in seconds between runs. If ommitted, task will only run once.
313
+ # - delay_type: Fixed Rate or Fixed Delay. Default is fixed_delay.
314
+ # - end_at: Scheduled task will stop running after this date (optional, if ommitted, runs forever or until cancelled)
315
+ # - run_times: Task will run exactly :run_times. For instance if :run_times is 5, then the task will run 5 times.
316
+ # - name: Provide a name for the schedule, defaults to class name. Use this if you want more than one schedule per worker class.
317
+ #
295
318
  def schedule(schedule)
296
319
  set_global_attributes
297
320
  upload_if_needed(schedule)
@@ -306,12 +329,12 @@ module SimpleWorker
306
329
  SimpleWorker.service.schedule_status(schedule_id)
307
330
  end
308
331
 
309
- # Retrieves the log for this worker from the SimpleWorker service.
332
+ # Retrieves the log for this worker from the SimpleWorker service.
310
333
  def get_log
311
334
  SimpleWorker.service.log(task_id)
312
335
  end
313
336
 
314
- # Callbacks for developer
337
+ # Callbacks for developer
315
338
  def before_upload
316
339
 
317
340
  end
@@ -358,7 +381,7 @@ module SimpleWorker
358
381
  #puts 'superclass=' + superclass.name
359
382
  break if superclass.name == SimpleWorker::Base.name
360
383
  super_merged = superclass.instance_variable_get(:@merged)
361
- #puts 'merging caller file: ' + superclass.instance_variable_get(:@caller_file).inspect
384
+ #puts 'merging caller file: ' + superclass.instance_variable_get(:@caller_file).inspect
362
385
  super_merged << superclass.instance_variable_get(:@caller_file)
363
386
  merged = super_merged + merged
364
387
  #puts 'merged with superclass=' + merged.inspect
@@ -369,11 +392,11 @@ module SimpleWorker
369
392
  def self.extract_merged_workers(worker)
370
393
  merged_workers = worker.class.instance_variable_get(:@merged_workers)
371
394
  SimpleWorker.logger.debug "Looking for merged_workers in #{worker.class.name}: #{merged_workers.inspect}"
372
- ret = []
395
+ ret = {}
373
396
  if merged_workers && merged_workers.size > 0
374
397
  merged_workers.each do |mw|
375
398
  SimpleWorker.logger.debug "merged worker found in #{worker.class.name}: #{mw.inspect}"
376
- ret << mw[0]
399
+ ret[mw[:name]] = mw
377
400
  end
378
401
  end
379
402
  ret
@@ -387,17 +410,17 @@ module SimpleWorker
387
410
 
388
411
  merged = self.class.instance_variable_get(:@merged)
389
412
 
390
- # do merged_workers first because we need to get their subclasses and what not too
413
+ # do merged_workers first because we need to get their subclasses and what not too
391
414
  merged_workers = self.class.instance_variable_get(:@merged_workers)
392
415
  if merged_workers && merged_workers.size > 0
393
416
  SimpleWorker.logger.debug 'now uploading merged workers ' + merged_workers.inspect
394
- merged_workers.each do |mw|
395
- SimpleWorker.logger.debug 'Instantiating and uploading ' + mw[1]
396
- mw_instantiated = Kernel.const_get(mw[1]).new
417
+ merged_workers.each_pair do |mw, v|
418
+ SimpleWorker.logger.debug 'Instantiating and uploading ' + v.inspect
419
+ mw_instantiated = Kernel.const_get(v[:class_name]).new
397
420
  mw_instantiated.upload
398
421
 
399
422
  merged, rfile, subclass = SimpleWorker::Base.extract_superclasses_merges(mw_instantiated, merged)
400
- merged += SimpleWorker::Base.extract_merged_workers(mw_instantiated)
423
+ merged.merge!(SimpleWorker::Base.extract_merged_workers(mw_instantiated))
401
424
 
402
425
  end
403
426
  end
@@ -424,7 +447,7 @@ module SimpleWorker
424
447
  merged_gems = gems_to_merge(merged_gems)
425
448
  merged_gems.uniq!
426
449
  end
427
- merged.uniq!
450
+ #merged.uniq!
428
451
  merged_mailers.uniq!
429
452
  options_for_upload = {:merge=>merged, :unmerge=>unmerged, :merged_gems=>merged_gems, :merged_mailers=>merged_mailers, :merged_folders=>merged_folders}
430
453
  options_for_upload[:name] = options[:name] if options[:name]
@@ -442,7 +465,7 @@ module SimpleWorker
442
465
  data = {}
443
466
 
444
467
  payload = {}
445
- # todo: should put these down a layer, eg: payload[:attributes]
468
+ # todo: should put these down a layer, eg: payload[:attributes]
446
469
  self.instance_variables.each do |iv|
447
470
  payload[iv] = instance_variable_get(iv)
448
471
  end
@@ -45,5 +45,10 @@ module SimpleWorker
45
45
 
46
46
  end
47
47
 
48
+ # Can merge a gem globally here
49
+ def self.merge_gem(options)
50
+
51
+ end
52
+
48
53
  end
49
54
 
@@ -0,0 +1 @@
1
+ # This is the file that gets executed on the server.
@@ -40,7 +40,7 @@ module SimpleWorker
40
40
  if File.exists?(f)
41
41
  existing_md5 = IO.read(f)
42
42
  end
43
- # Check for code changes.
43
+ # Check for code changes.
44
44
  md5 = Digest::MD5.hexdigest(File.read(filename))
45
45
  new_code = false
46
46
  if md5 != existing_md5
@@ -60,7 +60,7 @@ module SimpleWorker
60
60
  options = {
61
61
  "class_name"=>class_name,
62
62
  "name"=>name,
63
- "file_name"=> File.basename(filename)
63
+ "file_name"=> "runner.rb" # File.basename(filename)
64
64
  }
65
65
  #puts 'options for upload=' + options.inspect
66
66
  SimpleWorker.logger.info "Uploading now..."
@@ -84,7 +84,7 @@ module SimpleWorker
84
84
  gem_name =(gem_info[:name].match(/^[a-zA-Z0-9\-_]+/)[0])
85
85
  #puts "Searching for #{gem_name}..."
86
86
  gems= Gem::Specification.respond_to?(:each) ? Gem::Specification.find_all_by_name(gem_name) : Gem::GemPathSearcher.new.find_all(gem_name)
87
- # gems = searcher.init_gemspecs.select { |gem| gem.name==gem_name }
87
+ # gems = searcher.init_gemspecs.select { |gem| gem.name==gem_name }
88
88
  logger.debug 'gems found=' + gems.inspect
89
89
  gems = gems.select { |g| g.version.version==gem_info[:version] } if gem_info[:version]
90
90
  if !gems.empty?
@@ -100,17 +100,24 @@ module SimpleWorker
100
100
  # unless (merge && merge.size > 0) || (rged_gems && merged_gems.size > 0)
101
101
  # return filename
102
102
  # end
103
- merge = merge.nil? ? [] : merge.dup
103
+ merge = merge.nil? ? {} : merge.dup
104
+ SimpleWorker.logger.debug "merge"
104
105
  if unmerge
105
- unmerge.each do |x|
106
+ unmerge.each_pair do |x, y|
106
107
  deleted = merge.delete x
107
- # puts "Unmerging #{x}. Success? #{deleted}"
108
+ SimpleWorker.logger.debug "Unmerging #{x}. Success? #{deleted}"
108
109
  end
109
110
  end
110
- merge.uniq!
111
- tmp_file = File.join(Dir.tmpdir(), File.basename(filename))
111
+ #tmp_file = File.join(Dir.tmpdir(), File.basename(filename))
112
+ tmp_file = File.join(Dir.tmpdir(), 'runner.rb')
112
113
  File.open(tmp_file, "w") do |f|
113
114
  # add some rails stuff if using Rails
115
+
116
+ merge.each_pair do |k,v|
117
+ SimpleWorker.logger.debug "merging #{k} into #{filename}"
118
+ f.write("require_relative '#{k}'\n") # add(File.basename(m), m)
119
+ end
120
+
114
121
  if defined?(Rails)
115
122
  f.write "module Rails
116
123
  def self.version
@@ -121,7 +128,6 @@ module SimpleWorker
121
128
  end
122
129
  end
123
130
  "
124
-
125
131
  end
126
132
  if SimpleWorker.config.extra_requires
127
133
  SimpleWorker.config.extra_requires.each do |r|
@@ -164,9 +170,13 @@ end
164
170
  end
165
171
  end
166
172
  end
167
- f.write File.open(filename, 'r') { |mo| mo.read }
173
+ #f.write File.open(filename, 'r') { |mo| mo.read }
174
+ f.write("require_relative '#{File.basename(filename)}'")
168
175
  end
169
- merge << tmp_file
176
+ #puts 'funner.rb=' + tmp_file
177
+ merge['runner.rb'] = {:path=>tmp_file}
178
+ #puts 'filename=' + filename
179
+ merge[File.basename(filename)] = {:path=>filename}
170
180
  #puts "merge before uniq! " + merge.inspect
171
181
  # puts "merge after uniq! " + merge.inspect
172
182
 
@@ -185,11 +195,11 @@ end
185
195
  SimpleWorker.logger.debug "Collecting gem #{path}"
186
196
  Dir["#{path}/*", "#{path}/lib/**/**"].each do |file|
187
197
  # todo: could check if directory and it not lib, skip it
188
- puts 'file=' + file.inspect
198
+ SimpleWorker.logger.debug 'file for gem=' + file.inspect
189
199
  # puts 'gem2=' + gem.inspect
190
200
  zdest = "gems/#{gem[:name]}/#{file.sub(path+'/', '')}"
191
201
  # puts 'gem file=' + file.to_s
192
- puts 'zdest=' + zdest
202
+ SimpleWorker.logger.debug 'zip dest=' + zdest
193
203
  f.add(zdest, file)
194
204
  end
195
205
  else
@@ -209,16 +219,16 @@ end
209
219
  if files and files.size>0
210
220
  files.each do |file|
211
221
  zdest = "#{Digest::MD5.hexdigest(folder)}/#{file.sub(':', '_').sub('/', '_')}"
212
- puts 'put file to=' + zdest
222
+ SimpleWorker.logger.debug 'put file to=' + zdest
213
223
  f.add(zdest, file)
214
224
  end
215
225
  end
216
226
  end
217
227
  end
218
228
 
219
- merge.each do |m|
229
+ merge.each_pair do |k,v|
220
230
  # puts "merging #{m} into #{filename}"
221
- f.add(File.basename(m), m)
231
+ f.add(File.basename(v[:path]), v[:path])
222
232
  end
223
233
  if merged_mailers && merged_mailers.size > 0
224
234
  # puts " MERGED MAILERS" + merged_mailers.inspect
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple_worker
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.7.2
5
+ version: 0.7.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Travis Reeder
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-14 00:00:00 Z
13
+ date: 2011-08-01 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: zip
@@ -49,6 +49,7 @@ files:
49
49
  - lib/simple_worker/api.rb
50
50
  - lib/simple_worker/base.rb
51
51
  - lib/simple_worker/config.rb
52
+ - lib/simple_worker/runner.rb
52
53
  - lib/simple_worker/service.rb
53
54
  - lib/simple_worker/used_in_worker.rb
54
55
  - rails/init.rb