simple_worker 0.3.21 → 0.3.22

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -165,9 +165,9 @@ Rails 3.X:
165
165
  gem 'simple_worker'
166
166
 
167
167
  Now you can use your workers like they're part of your app! We recommend putting your worker classes in
168
- /app/workers path.
168
+ /app/workers path.
169
169
 
170
- Configuring an Database Connection
170
+ Configuring a Database Connection
171
171
  ---------------------------------
172
172
 
173
173
  Although you could easily do this in your worker, this makes it a bit more convenient and more importantly
@@ -210,11 +210,13 @@ The opposite can be done as well with "unmerge" and can be useful when using Rai
210
210
  merged.
211
211
 
212
212
 
213
- Bringing in other Workers
213
+ Merging other Workers
214
214
  ---------------------
215
215
 
216
- merge_worker
217
- TODO
216
+ Merging other workers is a bit different than merging other code like above because they will be
217
+ uploaded separately and treated as distinctly separate workers.
218
+
219
+ merge_worker "./other_worker.rb", "OtherWorker"
218
220
 
219
221
 
220
222
  Configuration Options
data/lib/rails2_init.rb CHANGED
@@ -1,8 +1,8 @@
1
- puts "Initializing list of Rails models..."
1
+ #puts "Initializing list of Rails models..."
2
2
  SimpleWorker.configure do |config|
3
3
  path = File.join(Rails.root, 'app/models/*.rb')
4
- puts 'path=' + path
4
+ # puts 'path=' + path
5
5
  config.models = Dir.glob(path)
6
6
  config.extra_requires += ['active_support/core_ext', 'active_record', 'action_mailer']
7
- puts 'config.models=' + config.models.inspect
7
+ # puts 'config.models=' + config.models.inspect
8
8
  end
@@ -10,14 +10,14 @@ module SimpleWorker
10
10
 
11
11
  class << self
12
12
  attr_accessor :subclass, :caller_file
13
- @merged = []
13
+ @merged = []
14
14
  @merged_workers = []
15
- @unmerged = []
15
+ @unmerged = []
16
16
 
17
17
  def reset!
18
- @merged = []
18
+ @merged = []
19
19
  @merged_workers = []
20
- @unmerged = []
20
+ @unmerged = []
21
21
  end
22
22
 
23
23
  def inherited(subclass)
@@ -52,7 +52,7 @@ module SimpleWorker
52
52
  puts 'f2=' + f2
53
53
  if File.exist? f2
54
54
  exists = true
55
- f = f2
55
+ f = f2
56
56
  end
57
57
  end
58
58
  unless exists
@@ -86,6 +86,11 @@ module SimpleWorker
86
86
  s[-suffix.length, suffix.length] == suffix
87
87
  end
88
88
 
89
+ # Use this to merge in other workers. These are treated differently the normal merged files because
90
+ # they will be uploaded separately and treated as distinctly separate workers.
91
+ #
92
+ # file: This is the path to the file, just like merge.
93
+ # class_name: eg: 'MyWorker'.
89
94
  def merge_worker(file, class_name)
90
95
  # puts 'merge_worker in ' + self.name
91
96
  merge(file)
@@ -116,9 +121,17 @@ module SimpleWorker
116
121
 
117
122
  # Call this if you want to run locally and get some extra features from this gem like global attributes.
118
123
  def run_local
119
- # puts 'run_local'
124
+ # puts 'run_local'
120
125
  set_auto_attributes
121
- run
126
+ begin
127
+ run
128
+ rescue => ex
129
+ if self.respond_to?(:rescue_all)
130
+ rescue_all(ex)
131
+ else
132
+ raise ex
133
+ end
134
+ end
122
135
  end
123
136
 
124
137
  def set_auto_attributes
@@ -138,16 +151,20 @@ module SimpleWorker
138
151
  end
139
152
  end
140
153
 
141
- # Will send in all instance_variables.
154
+ # Call this to queue up your job to SimpleWorker cloud.
155
+ # options:
156
+ # :priority => 0,1 or 2. Default is 0.
157
+ # :recursive => true/false. Default is false. If you queue up a worker that is the same class as the currently
158
+ # running worker, it will be rejected unless you set this explicitly so we know you meant to do it.
142
159
  def queue(options={})
143
160
  # puts 'in queue'
144
161
  set_auto_attributes
145
162
  upload_if_needed
146
163
 
147
- response = SimpleWorker.service.queue(self.class.name, sw_get_data, options)
164
+ response = SimpleWorker.service.queue(self.class.name, sw_get_data, options)
148
165
  # puts 'queue response=' + response.inspect
149
- @task_set_id = response["task_set_id"]
150
- @task_id = response["tasks"][0]["task_id"]
166
+ # @task_set_id = response["task_set_id"]
167
+ @task_id = response["task_id"]
151
168
  response
152
169
  end
153
170
 
@@ -173,7 +190,7 @@ module SimpleWorker
173
190
  set_global_attributes
174
191
  upload_if_needed
175
192
 
176
- response = SimpleWorker.service.schedule(self.class.name, sw_get_data, schedule)
193
+ response = SimpleWorker.service.schedule(self.class.name, sw_get_data, schedule)
177
194
  # puts 'schedule response=' + response.inspect
178
195
  @schedule_id = response["schedule_id"]
179
196
  response
@@ -206,15 +223,15 @@ module SimpleWorker
206
223
 
207
224
  before_upload
208
225
 
209
- puts 'upload_if_needed ' + self.class.name
226
+ # puts 'upload_if_needed ' + self.class.name
210
227
  # Todo, watch for this file changing or something so we can reupload
211
228
  unless uploaded?
212
- merged = self.class.instance_variable_get(:@merged)
213
- unmerged = self.class.instance_variable_get(:@unmerged)
229
+ merged = self.class.instance_variable_get(:@merged)
230
+ unmerged = self.class.instance_variable_get(:@unmerged)
214
231
  # puts 'merged1=' + merged.inspect
215
232
 
216
- subclass = self.class
217
- rfile = subclass.instance_variable_get(:@caller_file) # Base.caller_file # File.expand_path(Base.subclass)
233
+ subclass = self.class
234
+ rfile = subclass.instance_variable_get(:@caller_file) # Base.caller_file # File.expand_path(Base.subclass)
218
235
  # puts 'subclass file=' + rfile.inspect
219
236
  # puts 'subclass.name=' + subclass.name
220
237
  superclass = subclass
@@ -232,14 +249,14 @@ module SimpleWorker
232
249
  SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged, :unmerge=>unmerged)
233
250
  self.class.instance_variable_set(:@uploaded, true)
234
251
  else
235
- puts 'already uploaded for ' + self.class.name
252
+ SimpleWorker.logger.debug 'Already uploaded for ' + self.class.name
236
253
  end
237
254
  merged_workers = self.class.instance_variable_get(:@merged_workers)
238
255
  if merged_workers.size > 0
239
- puts 'now uploading merged workers ' + merged_workers.inspect
256
+ # puts 'now uploading merged workers ' + merged_workers.inspect
240
257
  merged_workers.each do |mw|
241
258
  # to support merges in the secondary worker, we should instantiate it here, then call "upload"
242
- puts 'instantiating and uploading ' + mw[1]
259
+ SimpleWorker.logger.debug 'Instantiating and uploading ' + mw[1]
243
260
  Kernel.const_get(mw[1]).new.upload
244
261
  # SimpleWorker.service.upload(mw[0], mw[1])
245
262
  end
@@ -254,7 +271,7 @@ module SimpleWorker
254
271
  data[iv] = instance_variable_get(iv)
255
272
  end
256
273
 
257
- config_data = SimpleWorker.config.get_atts_to_send
274
+ config_data = SimpleWorker.config.get_atts_to_send
258
275
  data[:sw_config] = config_data
259
276
  return data
260
277
  end
@@ -1,13 +1,21 @@
1
1
  require 'base64'
2
+ require 'logger'
2
3
 
3
4
  module SimpleWorker
4
5
 
6
+ @@logger = Logger.new(STDOUT)
7
+ @@logger.level = Logger::INFO
8
+
9
+ def self.logger
10
+ @@logger
11
+ end
12
+
5
13
  class Service < Appoxy::Api::Client
6
14
 
7
15
  attr_accessor :config
8
16
 
9
17
  def initialize(access_key, secret_key, options={})
10
- puts 'Starting SimpleWorker::Service...'
18
+ SimpleWorker.logger.info 'Starting SimpleWorker::Service...'
11
19
  self.config = options[:config] if options[:config]
12
20
  super("http://api.simpleworker.com/api/", access_key, secret_key, options)
13
21
  self.host = self.config.host if self.config && self.config.host
@@ -17,7 +25,7 @@ module SimpleWorker
17
25
  # - :callback_url
18
26
  # - :merge => array of files to merge in with this file
19
27
  def upload(filename, class_name, options={})
20
- puts "Uploading #{class_name}"
28
+ # puts "Uploading #{class_name}"
21
29
  # check whether it should upload again
22
30
  tmp = Dir.tmpdir()
23
31
  md5file = "simple_worker_#{class_name.gsub("::", ".")}_#{access_key[0,8]}.md5"
@@ -36,11 +44,11 @@ module SimpleWorker
36
44
 
37
45
  new_code = false
38
46
  if md5 != existing_md5
39
- puts "#{class_name}: new code, uploading"
47
+ SimpleWorker.logger.info "Uploading #{class_name}, code modified."
40
48
  File.open(f, 'w') { |f| f.write(md5) }
41
49
  new_code = true
42
50
  else
43
- puts "#{class_name}: same code, not uploading"
51
+ # puts "#{class_name}: same code, not uploading"
44
52
  end
45
53
 
46
54
  if new_code
@@ -97,15 +105,16 @@ module SimpleWorker
97
105
  # class_name: The class name of a previously upload class, eg: MySuperWorker
98
106
  # data: Arbitrary hash of your own data that your task will need to run.
99
107
  def queue(class_name, data={}, options={})
100
- puts "Queuing #{class_name}"
108
+ puts "Queuing #{class_name}..."
101
109
  if !data.is_a?(Array)
102
110
  data = [data]
103
111
  end
104
- p data
112
+ # p data
105
113
  hash_to_send = {}
106
114
  hash_to_send["payload"] = data
107
115
  hash_to_send["class_name"] = class_name
108
116
  hash_to_send["priority"] = options[:priority] if options[:priority]
117
+ hash_to_send["options"] = options
109
118
  add_sw_params(hash_to_send)
110
119
  if defined?(RAILS_ENV)
111
120
  # todo: move this to global_attributes in railtie
@@ -136,7 +145,7 @@ module SimpleWorker
136
145
  # - run_times: Task will run exactly :run_times. For instance if :run_times is 5, then the task will run 5 times.
137
146
  #
138
147
  def schedule(class_name, data, schedule)
139
- puts "Scheduling #{class_name}"
148
+ puts "Scheduling #{class_name}..."
140
149
  raise "Schedule must be a hash." if !schedule.is_a? Hash
141
150
  # if !data.is_a?(Array)
142
151
  # data = [data]
@@ -5,8 +5,7 @@ module SimpleWorker
5
5
  module UsedInWorker
6
6
 
7
7
  def log(str)
8
- puts 'gem usedinworker log=' + str.to_s
9
- # puts str.to_s
8
+ puts str.to_s
10
9
  end
11
10
  end
12
11
  end
data/test/test_base.rb CHANGED
@@ -3,7 +3,7 @@ require 'yaml'
3
3
  begin
4
4
  require File.join(File.dirname(__FILE__), '../lib/simple_worker')
5
5
  rescue Exception => ex
6
- puts ex.message
6
+ puts "Could NOT load current simple_worker: " + ex.message
7
7
  require 'simple_worker'
8
8
  end
9
9
  require_relative "test_worker"
@@ -22,7 +22,8 @@ class SimpleWorkerTests < TestBase
22
22
  # queue up a task
23
23
  puts 'queuing ' + tw.inspect
24
24
 
25
- 50.times do |i|
25
+ response_hash_single = nil
26
+ 5.times do |i|
26
27
  begin
27
28
  response_hash_single = tw.queue
28
29
  rescue => ex
@@ -31,7 +32,6 @@ class SimpleWorkerTests < TestBase
31
32
  end
32
33
 
33
34
  puts 'response_hash=' + response_hash_single.inspect
34
- puts 'task_set_id=' + tw.task_set_id
35
35
  puts 'task_id=' + tw.task_id
36
36
  10.times do |i|
37
37
  puts "status #{i}: " + tw.status.inspect
data/test/test_worker.rb CHANGED
@@ -1,9 +1,9 @@
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
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
7
  # Bump for new checksum.
8
8
  class TestWorker < SimpleWorker::Base
9
9
 
@@ -1,9 +1,11 @@
1
+ =begin
1
2
  begin
2
3
  require File.join(File.dirname(__FILE__), '../lib/simple_worker')
3
4
  rescue Exception => ex
4
5
  puts ex.message
5
6
  require 'simple_worker'
6
7
  end
8
+ =end
7
9
 
8
10
  # Bump for new checksum.sdf adsfjlaksdjf dasfasdfadsf
9
11
  class TestWorker2 < SimpleWorker::Base
@@ -1,9 +1,11 @@
1
+ =begin
1
2
  begin
2
3
  require File.join(File.dirname(__FILE__), '../lib/simple_worker')
3
4
  rescue Exception => ex
4
5
  puts ex.message
5
6
  require 'simple_worker'
6
7
  end
8
+ =end
7
9
  require_relative 'test_worker_2'
8
10
 
9
11
  class TestWorker3 < TestWorker2
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.21
4
+ version: 0.3.22
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-02-13 00:00:00.000000000 -08:00
12
+ date: 2011-02-19 00:00:00.000000000 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: appoxy_api
17
- requirement: &26617572 !ruby/object:Gem::Requirement
17
+ requirement: &27002148 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *26617572
25
+ version_requirements: *27002148
26
26
  description: The official SimpleWorker gem for http://www.simpleworker.com
27
27
  email: travis@appoxy.com
28
28
  executables: []
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 1.5.1
75
+ rubygems_version: 1.5.2
76
76
  signing_key:
77
77
  specification_version: 3
78
78
  summary: The official SimpleWorker gem for http://www.simpleworker.com