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 +7 -5
- data/lib/rails2_init.rb +3 -3
- data/lib/simple_worker/base.rb +38 -21
- data/lib/simple_worker/service.rb +16 -7
- data/lib/simple_worker/used_in_worker.rb +1 -2
- data/test/test_base.rb +1 -1
- data/test/test_simple_worker.rb +2 -2
- data/test/test_worker.rb +6 -6
- data/test/test_worker_2.rb +2 -0
- data/test/test_worker_3.rb +2 -0
- metadata +5 -5
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
|
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
|
-
|
213
|
+
Merging other Workers
|
214
214
|
---------------------
|
215
215
|
|
216
|
-
|
217
|
-
|
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
|
data/lib/simple_worker/base.rb
CHANGED
@@ -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
|
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
|
-
#
|
124
|
+
# puts 'run_local'
|
120
125
|
set_auto_attributes
|
121
|
-
|
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
|
-
#
|
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
|
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
|
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
|
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
|
213
|
-
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
|
217
|
-
rfile
|
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
|
-
|
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
|
-
|
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
|
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
|
-
|
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
|
-
|
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]
|
data/test/test_base.rb
CHANGED
data/test/test_simple_worker.rb
CHANGED
@@ -22,7 +22,8 @@ class SimpleWorkerTests < TestBase
|
|
22
22
|
# queue up a task
|
23
23
|
puts 'queuing ' + tw.inspect
|
24
24
|
|
25
|
-
|
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
|
|
data/test/test_worker_2.rb
CHANGED
@@ -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
|
data/test/test_worker_3.rb
CHANGED
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.
|
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-
|
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: &
|
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: *
|
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.
|
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
|