simple_worker 0.5.8 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -111,6 +111,7 @@ This will return a hash like:
111
111
  "duration"=>nil,
112
112
  "progress"=>{"percent"=>25}}
113
113
 
114
+ There is also a convenience method `worker.wait_until_complete` that will wait until the status returned is completed or error.
114
115
 
115
116
  Logging
116
117
  -------
@@ -227,8 +228,12 @@ Merging Gems
227
228
 
228
229
  This allows you to use any gem you'd like with SimpleWorker. This uses the same syntax as bundler gem files.
229
230
 
231
+ # merge latest version of the gem
230
232
  merge_gem "some_gem"
233
+ # or specify specific version
231
234
  merge_gem "some_gem_with_version", "1.2.3"
235
+ # or if gem has poor naming scheme
236
+ merge_gem 'mongoid_i18n', :require => 'mongoid/i18n'
232
237
 
233
238
  [Check here for more info on merge_gem](http://support.simpleworker.com/kb/working-with-simpleworker/merging-gems-into-your-worker).
234
239
 
data/lib/railtie.rb CHANGED
@@ -5,18 +5,37 @@ require 'rails'
5
5
 
6
6
  module SimpleWorker
7
7
  class Railtie < Rails::Railtie
8
- # railtie_name :simple_worker deprecated
8
+ # railtie_name :simple_worker deprecated
9
9
 
10
10
  initializer "simple_worker.configure_rails_initialization" do |app|
11
11
  puts "Initializing SimpleWorker for Rails 3..."
12
12
  SimpleWorker.configure do |c2|
13
- path = File.join(Rails.root, 'app/models/*.rb')
14
- # puts 'path=' + path
15
- c2.models = Dir.glob(path)
13
+ models_path = File.join(Rails.root, 'app/models/*.rb')
14
+ c2.models = Dir.glob(models_path)
15
+ mailers_path = File.join(Rails.root, 'app/mailers/*.rb')
16
+ c2.mailers =Dir.glob(mailers_path).collect { |m| {:filename=>m, :name => File.basename(m), :path_to_templates=>File.join(Rails.root, "app/views/#{File.basename(m, File.extname(m))}")} }
16
17
  c2.extra_requires += ['active_support/core_ext', 'active_record', 'action_mailer']
17
- # puts 'config.models=' + c2.models.inspect
18
+ c2.database = Rails.configuration.database_configuration[Rails.env]
19
+ c2.gems = get_required_gems if Bundler
20
+ puts "MODELS" + c2.models.inspect
21
+ puts "MAILERS" + c2.mailers.inspect
22
+ puts "DATABASE" + c2.database.inspect
23
+ puts "GEMS" + c2.gems.inspect
18
24
  end
25
+ end
19
26
 
27
+ def get_required_gems
28
+ gems_in_gemfile = Bundler.environment.dependencies.select { |d| d.groups.include?(:default) }
29
+ gems =[]
30
+ gems_in_gemfile.each do |dep|
31
+ next if dep.name=='rails' #monkey patch
32
+ gem_info = {:name=>dep.name, :version=>dep.requirement}
33
+ gem_info.merge!({:require=>dep.autorequire.join}) if dep.autorequire
34
+ gem_info[:version] = Bundler.load.specs.find { |g| g.name==gem_info[:name] }.version.to_s
35
+ gems << gem_info
36
+ end
37
+ gems
20
38
  end
21
39
  end
40
+
22
41
  end
@@ -12,12 +12,14 @@ module SimpleWorker
12
12
  @merged = []
13
13
  @merged_workers = []
14
14
  @merged_gems = []
15
+ @merged_mailers = []
15
16
  @unmerged = []
16
17
 
17
18
  def reset!
18
19
  @merged = []
19
20
  @merged_workers = []
20
21
  @merged_gems = []
22
+ @merged_mailers = []
21
23
  @unmerged = []
22
24
  end
23
25
 
@@ -50,7 +52,7 @@ module SimpleWorker
50
52
  # try relative
51
53
  # p caller
52
54
  f2 = File.join(File.dirname(caller[3]), f)
53
- puts 'f2=' + f2
55
+ # puts 'f2=' + f2
54
56
  if File.exist? f2
55
57
  exists = true
56
58
  f = f2
@@ -66,7 +68,7 @@ module SimpleWorker
66
68
 
67
69
  # merges the specified gem.
68
70
  def merge_gem(gem_name, version=nil)
69
- gem_info = {:name=>gem_name}
71
+ gem_info = {:name=>gem_name, :merge=>true}
70
72
  if version.is_a?(Hash)
71
73
  gem_info.merge!(version)
72
74
  else
@@ -76,6 +78,14 @@ module SimpleWorker
76
78
  require gem_info[:require] || gem_name
77
79
  end
78
80
 
81
+ #merge action_mailer mailers
82
+ def merge_mailer(mailer, params={})
83
+ check_for_file mailer
84
+ basename = File.basename(mailer, File.extname(mailer))
85
+ path_to_templates = params[:path_to_templates] || File.join(Rails.root, "app/views/#{basename}")
86
+ @merged_mailers << {:name=>basename, :path_to_templates=>path_to_templates, :filename => mailer}.merge!(params)
87
+ end
88
+
79
89
  # merges the specified files.
80
90
  # todo: don't allow multiple files per merge, just one like require
81
91
  def merge(*files)
@@ -192,13 +202,33 @@ module SimpleWorker
192
202
  end
193
203
 
194
204
  def status
205
+ check_service
206
+ if task_id
207
+ task_status
208
+ elsif schedule_id
209
+ schedule_status
210
+ else
211
+ raise "Queue or schedule before check status."
212
+ end
213
+ end
214
+
215
+ def task_status
195
216
  SimpleWorker.service.status(task_id)
196
217
  end
197
218
 
219
+ def is_local?
220
+ !is_remote?
221
+ end
222
+
223
+ def is_remote?
224
+ false
225
+ end
226
+
198
227
  # will return after job has completed or errored out.
199
228
  # Returns status.
200
229
  # todo: add a :timeout option
201
230
  def wait_until_complete
231
+ check_service
202
232
  tries = 0
203
233
  status = nil
204
234
  sleep 1
@@ -266,7 +296,26 @@ module SimpleWorker
266
296
 
267
297
  private
268
298
 
299
+ def gems_to_merge(merged_gems)
300
+ list_of_gems =[]
301
+ if merged_gems && merged_gems.size > 0
302
+ installed_gems = SimpleWorker.config.get_server_gems
303
+ merged_gems.each do |gem|
304
+ gem.merge!({:merge=>(!installed_gems.find { |g| g["name"]==gem[:name] && g["version"]==gem[:version] })})
305
+ list_of_gems<< gem if (list_of_gems.select { |g| g[:name]==gem[:name] }).empty?
306
+ end
307
+ SimpleWorker.logger.debug "#{list_of_gems.inspect}"
308
+ end
309
+ list_of_gems
310
+ end
311
+
312
+ def check_service
313
+ raise "SimpleWorker configuration not set." unless SimpleWorker.service
314
+ end
315
+
269
316
  def upload_if_needed
317
+ check_service
318
+ SimpleWorker.service.check_config
270
319
 
271
320
  before_upload
272
321
 
@@ -276,6 +325,7 @@ module SimpleWorker
276
325
  merged = self.class.instance_variable_get(:@merged)
277
326
  unmerged = self.class.instance_variable_get(:@unmerged)
278
327
  merged_gems = self.class.instance_variable_get(:@merged_gems)
328
+ merged_mailers = self.class.instance_variable_get(:@merged_mailers)
279
329
  # puts 'merged1=' + merged.inspect
280
330
 
281
331
  subclass = self.class
@@ -293,8 +343,21 @@ module SimpleWorker
293
343
  merged = super_merged + merged
294
344
  # puts 'merged with superclass=' + merged.inspect
295
345
  end
296
- merged += SimpleWorker.config.models if SimpleWorker.config.models
297
- SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged, :unmerge=>unmerged, :merged_gems=>merged_gems)
346
+ if SimpleWorker.config.auto_merge
347
+ puts "Auto merge Enabled"
348
+ merged += SimpleWorker.config.models if SimpleWorker.config.models
349
+ merged_mailers += SimpleWorker.config.mailers if SimpleWorker.config.mailers
350
+ SimpleWorker.config.gems.each do |gem|
351
+ merged_gems<<gem
352
+ end
353
+ end
354
+ unless merged_gems.empty?
355
+ merged_gems = gems_to_merge(merged_gems)
356
+ merged_gems.uniq!
357
+ end
358
+ merged.uniq!
359
+ merged_mailers.uniq!
360
+ SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged, :unmerge=>unmerged, :merged_gems=>merged_gems, :merged_mailers=>merged_mailers)
298
361
  self.class.instance_variable_set(:@uploaded, true)
299
362
  else
300
363
  SimpleWorker.logger.debug 'Already uploaded for ' + self.class.name
@@ -15,12 +15,22 @@ module SimpleWorker
15
15
  :host,
16
16
  :global_attributes,
17
17
  :models,
18
+ :mailers,
19
+ :gems,
18
20
  :database,
19
- :extra_requires
21
+ :extra_requires,
22
+ :auto_merge,
23
+ :server_gems
24
+
20
25
 
21
26
  def initialize
22
27
  @global_attributes = {}
23
- @extra_requires = []
28
+ @extra_requires = []
29
+ end
30
+
31
+ def get_server_gems
32
+ self.server_gems = SimpleWorker.service.get_server_gems unless self.server_gems
33
+ self.server_gems
24
34
  end
25
35
 
26
36
  def get_atts_to_send
@@ -21,7 +21,14 @@ module SimpleWorker
21
21
 
22
22
  def initialize(access_key, secret_key, options={})
23
23
  SimpleWorker.logger.info 'Starting SimpleWorker::Service...'
24
- self.config = options[:config] if options[:config]
24
+ if options[:config]
25
+ self.config = options[:config]
26
+ else
27
+ c = SimpleWorker::Config.new unless self.config
28
+ c.access_key = access_key
29
+ c.secret_key = secret_key
30
+ self.config = c
31
+ end
25
32
  super("http://api.simpleworker.com/api/", access_key, secret_key, options)
26
33
  self.host = self.config.host if self.config && self.config.host
27
34
  end
@@ -52,7 +59,7 @@ module SimpleWorker
52
59
  end
53
60
 
54
61
 
55
- zip_filename = build_merged_file(filename, options[:merge], options[:unmerge], options[:merged_gems])
62
+ zip_filename = build_merged_file(filename, options[:merge], options[:unmerge], options[:merged_gems], options[:merged_mailers])
56
63
 
57
64
  # sys.classes[subclass].__file__
58
65
  # puts '__FILE__=' + Base.subclass.__file__.to_s
@@ -76,12 +83,18 @@ module SimpleWorker
76
83
  end
77
84
  end
78
85
 
86
+ def get_server_gems
87
+ hash = get("gems/list")
88
+ JSON.parse(hash["gems"])
89
+ end
90
+
79
91
  def get_gem_path(gem_info)
80
92
  gem_name =(gem_info[:require] || gem_info[:name].match(/^[a-zA-Z0-9\-_]+/)[0])
81
93
  puts "Searching for #{gem_name}..."
82
94
  searcher = Gem::GemPathSearcher.new
83
- gems = searcher.find_all(gem_name)
84
- # puts 'gems found=' + gems.inspect
95
+ gems = searcher.find_all(gem_name)
96
+ # gems = searcher.init_gemspecs.select { |gem| gem.name==gem_name }
97
+ puts 'gems found=' + gems.inspect
85
98
  gems = gems.select { |g| g.version.version==gem_info[:version] } if gem_info[:version]
86
99
  if !gems.empty?
87
100
  gem = gems.first
@@ -91,7 +104,7 @@ module SimpleWorker
91
104
  end
92
105
  end
93
106
 
94
- def build_merged_file(filename, merge, unmerge, merged_gems)
107
+ def build_merged_file(filename, merge, unmerge, merged_gems, merged_mailers)
95
108
  # unless (merge && merge.size > 0) || (merged_gems && merged_gems.size > 0)
96
109
  # return filename
97
110
  # end
@@ -110,6 +123,28 @@ module SimpleWorker
110
123
  f.write "require '#{r}'\n"
111
124
  end
112
125
  end
126
+ if merged_mailers
127
+ f.write "require 'action_mailer'\n"
128
+ f.write "ActionMailer::Base.prepend_view_path('templates')\n"
129
+ end
130
+ if SimpleWorker.config.auto_merge
131
+ if SimpleWorker.config.gems
132
+ SimpleWorker.config.gems.each do |gem|
133
+ f.write "$LOAD_PATH << File.join(File.dirname(__FILE__), '/gems/#{gem[:name]}')\n" if gem[:merge]
134
+ f.write "require '#{gem[:require]||gem[:name]}'\n"
135
+ end
136
+ end
137
+ if SimpleWorker.config.models
138
+ SimpleWorker.config.models.each do |model|
139
+ f.write "require File.join(File.dirname(__FILE__),'#{File.basename(model)}')\n"
140
+ end
141
+ end
142
+ if SimpleWorker.config.mailers
143
+ SimpleWorker.config.mailers.each do |mailer|
144
+ f.write "require File.join(File.dirname(__FILE__),'#{mailer[:name]}')\n"
145
+ end
146
+ end
147
+ end
113
148
  f.write File.open(filename, 'r') { |mo| mo.read }
114
149
  end
115
150
  merge << tmp_file
@@ -121,10 +156,10 @@ module SimpleWorker
121
156
  # puts 'merged_file_array=' + merge.inspect
122
157
  #File.open(fname2, "w") do |f|
123
158
  File.delete(fname2) if File.exist?(fname2)
124
-
125
159
  Zip::ZipFile.open(fname2, 'w') do |f|
126
160
  if merged_gems
127
161
  merged_gems.each do |gem|
162
+ next unless gem[:merge]
128
163
  # puts 'gem=' + gem.inspect
129
164
  path = get_gem_path(gem)
130
165
  if path
@@ -145,6 +180,20 @@ module SimpleWorker
145
180
  # puts "merging #{m} into #{filename}"
146
181
  f.add(File.basename(m), m)
147
182
  end
183
+ puts "merge models - done"
184
+ if merged_mailers
185
+ puts " MERGED MAILERS" + merged_mailers.inspect
186
+ merged_mailers.each do |mailer|
187
+ puts "Collecting mailer #{mailer[:name]}"
188
+ f.add(File.basename(mailer[:filename]), mailer[:filename])
189
+ path = mailer[:path_to_templates]
190
+ Dir["#{path}/**/**"].each do |file|
191
+ zdest = "templates/#{mailer[:name]}/#{file.sub(path+'/', '')}"
192
+ f.add(zdest, file)
193
+ end
194
+ end
195
+ end
196
+ puts "merging templates - done"
148
197
  end
149
198
  fname2
150
199
  end
@@ -223,7 +272,7 @@ module SimpleWorker
223
272
  def cancel_schedule(scheduled_task_id)
224
273
  raise "Must include a schedule id." if scheduled_task_id.blank?
225
274
  hash_to_send = {}
226
- hash_to_send["scheduled_task_id"] = scheduled_task_id
275
+ hash_to_send["schedule_id"] = scheduled_task_id
227
276
  ret = post("scheduler/cancel", hash_to_send)
228
277
  ret
229
278
  end
data/lib/simple_worker.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'appoxy_api'
2
- require 'active_support/core_ext'
2
+ #require 'active_support/core_ext'
3
3
  require_relative 'simple_worker/service'
4
4
  require_relative 'simple_worker/base'
5
5
  require_relative 'simple_worker/config'
@@ -14,7 +14,7 @@ module SimpleWorker
14
14
 
15
15
  def configure()
16
16
  yield(config)
17
- SimpleWorker.service = Service.new(config.access_key, config.secret_key, :config=>config)
17
+ SimpleWorker.service ||= Service.new(config.access_key, config.secret_key, :config=>config)
18
18
  end
19
19
 
20
20
  def config
@@ -23,8 +23,6 @@ module SimpleWorker
23
23
 
24
24
  end
25
25
 
26
-
27
-
28
26
  end
29
27
 
30
28
  if defined?(Rails)
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_worker
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 5
8
- - 8
9
- version: 0.5.8
4
+ prerelease:
5
+ version: 0.6.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Travis Reeder
@@ -14,8 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-03-30 00:00:00 -07:00
18
- default_executable:
13
+ date: 2011-05-26 00:00:00 Z
19
14
  dependencies:
20
15
  - !ruby/object:Gem::Dependency
21
16
  name: appoxy_api
@@ -25,10 +20,6 @@ dependencies:
25
20
  requirements:
26
21
  - - ">="
27
22
  - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- - 0
31
- - 11
32
23
  version: 0.0.11
33
24
  type: :runtime
34
25
  version_requirements: *id001
@@ -40,8 +31,6 @@ dependencies:
40
31
  requirements:
41
32
  - - ">="
42
33
  - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
34
  version: "0"
46
35
  type: :runtime
47
36
  version_requirements: *id002
@@ -63,26 +52,6 @@ files:
63
52
  - lib/simple_worker/used_in_worker.rb
64
53
  - rails/init.rb
65
54
  - README.markdown
66
- - test/awesome_job.rb
67
- - test/cool_model.rb
68
- - test/cool_worker.rb
69
- - test/db_worker.rb
70
- - test/models/model_1.rb
71
- - test/models/model_2.rb
72
- - test/requiring_worker.rb
73
- - test/scheduled_worker.rb
74
- - test/second_worker.rb
75
- - test/test_base.rb
76
- - test/test_gems.rb
77
- - test/test_inheritance.rb
78
- - test/test_no_config.rb
79
- - test/test_simple_worker.rb
80
- - test/test_worker.rb
81
- - test/test_worker_2.rb
82
- - test/test_worker_3.rb
83
- - test/trace_object.rb
84
- - test/zip_vs_gzip.rb
85
- has_rdoc: true
86
55
  homepage: http://github.com/appoxy/simple_worker
87
56
  licenses: []
88
57
 
@@ -96,41 +65,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
65
  requirements:
97
66
  - - ">="
98
67
  - !ruby/object:Gem::Version
99
- segments:
100
- - 0
101
68
  version: "0"
102
69
  required_rubygems_version: !ruby/object:Gem::Requirement
103
70
  none: false
104
71
  requirements:
105
72
  - - ">="
106
73
  - !ruby/object:Gem::Version
107
- segments:
108
- - 0
109
74
  version: "0"
110
75
  requirements: []
111
76
 
112
77
  rubyforge_project:
113
- rubygems_version: 1.3.7
78
+ rubygems_version: 1.7.2
114
79
  signing_key:
115
80
  specification_version: 3
116
81
  summary: The official SimpleWorker gem for http://www.simpleworker.com
117
- test_files:
118
- - test/awesome_job.rb
119
- - test/cool_model.rb
120
- - test/cool_worker.rb
121
- - test/db_worker.rb
122
- - test/models/model_1.rb
123
- - test/models/model_2.rb
124
- - test/requiring_worker.rb
125
- - test/scheduled_worker.rb
126
- - test/second_worker.rb
127
- - test/test_base.rb
128
- - test/test_gems.rb
129
- - test/test_inheritance.rb
130
- - test/test_no_config.rb
131
- - test/test_simple_worker.rb
132
- - test/test_worker.rb
133
- - test/test_worker_2.rb
134
- - test/test_worker_3.rb
135
- - test/trace_object.rb
136
- - test/zip_vs_gzip.rb
82
+ test_files: []
83
+
data/test/awesome_job.rb DELETED
@@ -1,25 +0,0 @@
1
-
2
- class AwesomeJob < SimpleWorker::Base
3
- merge_gem 'dropbox' # , '1.2.3'
4
- # bumpdsfsdfdsfasdf
5
- merge_gem 'mongoid_i18n', :require => 'mongoid/i18n'
6
-
7
- def run
8
- begin
9
- s = Dropbox::Session.new('...', '...')
10
- rescue => ex
11
- log "Dropbox doesn't like it when you don't have keys"
12
- end
13
- # s.mode = :dropbox
14
- # s.authorizing_user = 'email@gmail.com'
15
- # s.authorizing_password = '...'
16
- # s.authorize!
17
- #
18
- # tmp_file = Tempfile.new('myfile.txt')
19
- # tmp_file.write("blahblah")
20
- # tmp_file.close
21
- #
22
- # s.upload tmp_file.path, 'Test'
23
- # tmp_file.unlink
24
- end
25
- end
data/test/cool_model.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'simple_record'
2
-
3
- class CoolModel < SimpleRecord::Base
4
-
5
- has_strings :name
6
-
7
- end
data/test/cool_worker.rb DELETED
@@ -1,14 +0,0 @@
1
-
2
- class CoolWorker < SimpleWorker::Base
3
- attr_accessor :array_of_models
4
- merge 'cool_model'
5
-
6
-
7
- def run
8
- 10.times do |i|
9
- puts "HEY THERE PUTS #{i}"
10
- log "HEY THERE LOG #{i}"
11
- sleep 1
12
- end
13
- end
14
- end
data/test/db_worker.rb DELETED
@@ -1,18 +0,0 @@
1
- require 'mysql2'
2
-
3
- class DbWorker < SimpleWorker::Base
4
- attr_accessor :array_of_models
5
- merge 'trace_object'
6
-
7
- def run
8
- to = TraceObject.find(:first)
9
- log to.inspect
10
- @object = to
11
- end
12
-
13
- def ob
14
- @object
15
- end
16
-
17
-
18
- end
@@ -1,22 +0,0 @@
1
- begin
2
- require File.join(File.dirname(__FILE__), '../../lib/simple_worker')
3
- rescue Exception => ex
4
- puts 'ERROR!!! ' + ex.message
5
- # require 'simple_worker'
6
- end
7
-
8
-
9
- class Model1
10
- attr_accessor :heidi, :ho
11
-
12
- include SimpleWorker::UsedInWorker
13
-
14
- def say_hello
15
- log "Hi there sir"
16
- end
17
-
18
- # testk laksdfj klasj df
19
- def test
20
- log 'test'
21
- end
22
- end
@@ -1,22 +0,0 @@
1
- begin
2
- require File.join(File.dirname(__FILE__), '../../lib/simple_worker')
3
- rescue Exception => ex
4
- puts 'ERROR!!! ' + ex.message
5
- # require 'simple_worker'
6
- end
7
-
8
-
9
- class Model2
10
- attr_accessor :heidi, :ho
11
-
12
- include SimpleWorker::UsedInWorker
13
-
14
- def say_hello
15
- log "Hi there sir"
16
- end
17
-
18
- # testk laksdfj klasj df
19
- def test
20
- log 'test'
21
- end
22
- end
@@ -1,4 +0,0 @@
1
-
2
-
3
- require_relative 'models/model_1.rb'
4
-
@@ -1,15 +0,0 @@
1
- begin
2
- require File.join(File.dirname(__FILE__), '../lib/simple_worker')
3
- rescue Exception
4
- require 'simple_worker'
5
- end
6
-
7
-
8
- class ScheduledWorker < SimpleWorker::Base
9
-
10
- def scheduled_run(data=nil)
11
- log "This is scheduled yes it is"
12
- log data.inspect
13
- end
14
-
15
- end
@@ -1,20 +0,0 @@
1
- begin
2
- require File.join(File.dirname(__FILE__), '../lib/simple_worker')
3
- rescue Exception => ex
4
- puts ex.message
5
- require 'simple_worker'
6
- end
7
-
8
- class SecondWorker < SimpleWorker::Base
9
- attr_accessor :start_time, :num
10
-
11
- # change
12
-
13
- def run
14
- log self.to_s
15
- end
16
-
17
- def to_s
18
- "I am Second Worker #{num}. I was started at #{start_time}"
19
- end
20
- end
data/test/test_base.rb DELETED
@@ -1,57 +0,0 @@
1
- require 'test/unit'
2
- require 'yaml'
3
- begin
4
- require File.join(File.dirname(__FILE__), '../lib/simple_worker')
5
- rescue Exception => ex
6
- puts "Could NOT load current simple_worker: " + ex.message
7
- require 'simple_worker'
8
- end
9
- require_relative "test_worker"
10
- require_relative "test_worker_2"
11
- require_relative "test_worker_3"
12
-
13
- class TestBase < Test::Unit::TestCase
14
-
15
- def setup
16
- @config = YAML::load(File.open(File.expand_path("~/.test_configs/simple_worker.yml")))
17
- #puts @config.inspect
18
- @access_key = @config['simple_worker']['access_key']
19
- @secret_key = @config['simple_worker']['secret_key']
20
-
21
- # new style
22
- SimpleWorker.configure do |config|
23
- config.access_key = @access_key
24
- config.secret_key = @secret_key
25
- config.host = @config['simple_worker']['host']
26
- config.global_attributes["db_user"] = "sa"
27
- config.global_attributes["db_pass"] = "pass"
28
- config.database = @config['database']
29
-
30
- end
31
- end
32
-
33
- def wait_for_task(params={})
34
- tries = 0
35
- status = nil
36
- sleep 1
37
- while tries < 60
38
- status = status_for(params)
39
- puts 'status = ' + status.inspect
40
- if status["status"] == "complete" || status["status"] == "error"
41
- break
42
- end
43
- sleep 2
44
- end
45
- status
46
- end
47
-
48
- def status_for(ob)
49
- if ob.is_a?(Hash)
50
- ob[:schedule_id] ? WORKER.schedule_status(ob[:schedule_id]) : WORKER.status(ob[:task_id])
51
- else
52
- ob.status
53
- end
54
- end
55
-
56
-
57
- end
data/test/test_gems.rb DELETED
@@ -1,18 +0,0 @@
1
- require_relative 'test_base'
2
- require_relative 'awesome_job'
3
-
4
-
5
- class TestGems < TestBase
6
-
7
- def test_dropbox_gem
8
-
9
- worker = AwesomeJob.new
10
- worker.queue
11
-
12
- wait_for_task(worker)
13
-
14
- puts 'log=' + worker.get_log
15
-
16
- end
17
-
18
- end
@@ -1,19 +0,0 @@
1
- require_relative 'test_base'
2
-
3
- class TestInheritance < TestBase
4
-
5
- def test_who_am_i
6
-
7
- worker = TestWorker2.new
8
- puts "1: " + worker.who_am_i?
9
- puts "2: " + worker.who_am_i2?
10
-
11
-
12
- end
13
-
14
- def test_multi_subs
15
- t3 = TestWorker3.new
16
- t3.queue
17
- end
18
-
19
- end
@@ -1,25 +0,0 @@
1
- require 'test/unit'
2
- begin
3
- require File.join(File.dirname(__FILE__), '../lib/simple_worker')
4
- rescue Exception => ex
5
- puts ex.message
6
- require 'simple_worker'
7
- end
8
- require_relative "test_worker"
9
- require_relative "test_worker_2"
10
-
11
-
12
- class SimpleWorkerTests < Test::Unit::TestCase
13
-
14
- def test_no_conf
15
- # Add something to queue, get task ID back
16
- tw = TestWorker.new
17
- tw.s3_key = "active style runner"
18
- tw.times = 3
19
-
20
- tw.run_local
21
-
22
- end
23
-
24
- end
25
-
@@ -1,106 +0,0 @@
1
- require 'active_record'
2
- require_relative 'test_base'
3
- require_relative 'cool_worker'
4
- require_relative 'cool_model'
5
- require_relative 'trace_object'
6
- require_relative 'db_worker'
7
-
8
- class SimpleWorkerTests < TestBase
9
-
10
-
11
- def test_new_worker_style
12
- # Add something to queue, get task ID back
13
- tw = TestWorker2.new
14
- tw.s3_key = "active style runner"
15
- tw.times = 3
16
- tw.x = true
17
-
18
- # schedule up a task
19
- # start_at = 10.seconds.since
20
- # response_hash_single = tw.schedule(:start_at=>start_at, :run_every=>30, :run_times=>3)
21
- # puts 'response_hash=' + response_hash_single.inspect
22
- #
23
- # 10.times do |i|
24
- # puts "status #{i}: " + tw.schedule_status.inspect
25
- # end
26
-
27
- # queue up a task
28
- puts 'queuing ' + tw.inspect
29
-
30
- response_hash_single = nil
31
- 5.times do |i|
32
- begin
33
- response_hash_single = tw.queue
34
- rescue => ex
35
- puts ex.message
36
- end
37
- end
38
-
39
- puts 'response_hash=' + response_hash_single.inspect
40
- puts 'task_id=' + tw.task_id
41
- 10.times do |i|
42
- puts "status #{i}: " + tw.status.inspect
43
- break if tw.status["status"] == "complete"
44
- sleep 2
45
- end
46
-
47
- assert tw.status["status"] == "complete"
48
-
49
- end
50
-
51
- def test_global_attributes
52
- worker = TestWorker3.new
53
- worker.run_local
54
-
55
- puts 'worker=' + worker.inspect
56
-
57
- assert_equal "sa", worker.db_user
58
- assert_equal "pass", worker.db_pass
59
- assert_equal 123, worker.x
60
-
61
- end
62
-
63
-
64
- def test_data_passing
65
- cool = CoolWorker.new
66
- cool.array_of_models = [CoolModel.new(:name=>"name1"), CoolModel.new(:name=>"name2")]
67
- cool.queue
68
- status = wait_for_task(cool)
69
- assert status["status"] == "complete"
70
- log = SimpleWorker.service.log(cool.task_id)
71
- puts 'log=' + log.inspect
72
- assert log.length > 10
73
-
74
- end
75
-
76
- def test_exceptions
77
- worker = TestWorker.new
78
- worker.queue
79
- status = wait_for_task(worker)
80
- assert status["status"] == "error"
81
- assert status["msg"].present?
82
- end
83
-
84
- def test_active_record
85
- dbw = DbWorker.new
86
- dbw.run_local
87
- assert !dbw.ob.nil?
88
- assert !dbw.ob.id.nil?
89
-
90
- dbw.queue
91
- # would be interesting if the object could update itself on complete. Like it would retrieve new values from
92
- # finished job when calling status or something.
93
-
94
- status = wait_for_task(dbw)
95
- assert status["status"] == "complete"
96
-
97
-
98
- end
99
-
100
-
101
- def test_require_relative_merge
102
-
103
-
104
- end
105
- end
106
-
data/test/test_worker.rb DELETED
@@ -1,29 +0,0 @@
1
- #begin
2
- # require File.join(File.dirname(__FILE__), '../lib/simple_worker')
3
- #rescue Exception => ex
4
- # puts ex.message
5
- # require 'simple_worker'
6
- #end
7
- # Bump for new checksum.
8
- class TestWorker < SimpleWorker::Base
9
-
10
- attr_accessor :s3_key, :times
11
-
12
- def run
13
- log 'running the test worker for moi '.upcase
14
- log 's3_key instance_variable = ' + self.s3_key.to_s
15
-
16
- @times.times do |i|
17
- log 'running at ' + i.to_s
18
- sleep 1
19
- set_progress(:percent=> (i / @times * 100))
20
- end
21
- end
22
-
23
- def set_complete(params=nil)
24
- log 'SET COMPLETE YAY!' + params[:task_set_id]
25
- end
26
-
27
-
28
- end
29
-
@@ -1,60 +0,0 @@
1
- =begin
2
- begin
3
- require File.join(File.dirname(__FILE__), '../lib/simple_worker')
4
- rescue Exception => ex
5
- puts ex.message
6
- require 'simple_worker'
7
- end
8
- =end
9
-
10
- # Bump for new checksum.sdf adsfjlaksdjf dasfasdfadsf
11
- class TestWorker2 < SimpleWorker::Base
12
-
13
- merge File.join(File.dirname(__FILE__), 'models', 'model_1.rb')
14
- unmerge 'models/model_2.rb'
15
- merge_worker File.join(File.dirname(__FILE__), 'second_worker.rb'), 'SecondWorker'
16
-
17
- attr_accessor :s3_key, :times, :x
18
-
19
-
20
- #TEST dsd
21
- def who_am_i2?
22
- return self.class.name
23
- end
24
-
25
- def run
26
- log 'running the runner for leroy '.upcase + ' with data: '
27
-
28
- log 's3_key instance_variable = ' + self.s3_key
29
- times.times do |i|
30
- log 'running at ' + i.to_s
31
- sleep 1
32
- set_progress(:percent=> (i / times * 100))
33
- end
34
- m1 = Model1.new
35
- log "I made a new model1"
36
- m1.say_hello
37
-
38
- second_workers = []
39
- now = Time.now
40
- 10.times do |i|
41
- second_worker = SecondWorker.new
42
- second_worker.start_time = now
43
- second_worker.num = i
44
- second_worker.queue
45
- second_workers << second_worker
46
- end
47
-
48
- 10.times do |i|
49
- second_workers.each do |sw|
50
- puts sw.to_s
51
- puts sw.status["status"].to_s
52
- end
53
- end
54
- end
55
-
56
- def set_complete(params=nil)
57
- log 'SET COMPLETE YAY!' + params[:task_set_id]
58
- end
59
-
60
- end
@@ -1,22 +0,0 @@
1
- =begin
2
- begin
3
- require File.join(File.dirname(__FILE__), '../lib/simple_worker')
4
- rescue Exception => ex
5
- puts ex.message
6
- require 'simple_worker'
7
- end
8
- =end
9
- require_relative 'test_worker_2'
10
-
11
- class TestWorker3 < TestWorker2
12
-
13
-
14
- attr_accessor :x, :db_user, :db_pass
15
-
16
- def run()
17
- puts 'TestWorker3.run'
18
- @x = 123
19
- end
20
-
21
- end
22
-
data/test/trace_object.rb DELETED
@@ -1,6 +0,0 @@
1
- class TraceObject < ActiveRecord::Base
2
- belongs_to :tracking_device
3
- has_many :location_updates
4
-
5
-
6
- end
data/test/zip_vs_gzip.rb DELETED
@@ -1,26 +0,0 @@
1
- require 'zip'
2
- require 'zlib'
3
-
4
- dir = Dir.new("./")
5
- started_at = Time.now
6
- fname2 = "zipped.zip"
7
- Zip::ZipFile.open(fname2, 'w') do |f|
8
- dir.each do |fname|
9
- puts "merging #{fname} into #{fname2}"
10
- f.add(File.basename(fname), fname)
11
- end
12
- end
13
- ended_at = Time.now
14
- puts "zip duration=#{(ended_at.to_f - started_at.to_f)}"
15
-
16
- # todo: need to tar these first.
17
- started_at = Time.now
18
- fname2 = "gzipped.gzip"
19
- Zlib::GzipWriter.open(fname2) do |gz|
20
- dir.each do |fname|
21
- puts "merging #{fname} into #{fname2}"
22
- gz.write(File.basename(fname), fname)
23
- end
24
- end
25
- ended_at = Time.now
26
- puts "gzip duration=#{(ended_at.to_f - started_at.to_f)}"