simple_worker 0.4.1 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rails2_init.rb +8 -8
- data/lib/railtie.rb +22 -22
- data/lib/simple_worker/base.rb +11 -4
- data/lib/simple_worker/service.rb +248 -235
- data/lib/simple_worker.rb +3 -1
- data/test/cool_model.rb +7 -7
- data/test/cool_worker.rb +14 -14
- data/test/db_worker.rb +18 -19
- data/test/models/model_1.rb +1 -1
- data/test/models/model_2.rb +22 -22
- data/test/requiring_worker.rb +4 -4
- data/test/test_no_config.rb +25 -25
- data/test/test_simple_worker.rb +2 -2
- data/test/test_worker.rb +29 -29
- data/test/test_worker_2.rb +2 -5
- data/test/trace_object.rb +6 -6
- data/test/zip_vs_gzip.rb +26 -0
- metadata +48 -30
data/lib/rails2_init.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#puts "Initializing list of Rails models..."
|
2
|
-
SimpleWorker.configure do |config|
|
3
|
-
path = File.join(Rails.root, 'app/models/*.rb')
|
4
|
-
# puts 'path=' + path
|
5
|
-
config.models = Dir.glob(path)
|
6
|
-
config.extra_requires += ['active_support/core_ext', 'active_record', 'action_mailer']
|
7
|
-
# puts 'config.models=' + config.models.inspect
|
8
|
-
end
|
1
|
+
#puts "Initializing list of Rails models..."
|
2
|
+
SimpleWorker.configure do |config|
|
3
|
+
path = File.join(Rails.root, 'app/models/*.rb')
|
4
|
+
# puts 'path=' + path
|
5
|
+
config.models = Dir.glob(path)
|
6
|
+
config.extra_requires += ['active_support/core_ext', 'active_record', 'action_mailer']
|
7
|
+
# puts 'config.models=' + config.models.inspect
|
8
|
+
end
|
data/lib/railtie.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
# see http://api.rubyonrails.org/classes/Rails/Railtie.html
|
2
|
-
|
3
|
-
require 'simple_worker'
|
4
|
-
require 'rails'
|
5
|
-
|
6
|
-
module SimpleWorker
|
7
|
-
class Railtie < Rails::Railtie
|
8
|
-
# railtie_name :simple_worker deprecated
|
9
|
-
|
10
|
-
initializer "simple_worker.configure_rails_initialization" do |app|
|
11
|
-
puts "Initializing SimpleWorker for Rails 3..."
|
12
|
-
SimpleWorker.configure do |c2|
|
13
|
-
path = File.join(Rails.root, 'app/models/*.rb')
|
14
|
-
# puts 'path=' + path
|
15
|
-
c2.models = Dir.glob(path)
|
16
|
-
c2.extra_requires += ['active_support/core_ext', 'active_record', 'action_mailer']
|
17
|
-
# puts 'config.models=' + c2.models.inspect
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
1
|
+
# see http://api.rubyonrails.org/classes/Rails/Railtie.html
|
2
|
+
|
3
|
+
require 'simple_worker'
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
module SimpleWorker
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
# railtie_name :simple_worker deprecated
|
9
|
+
|
10
|
+
initializer "simple_worker.configure_rails_initialization" do |app|
|
11
|
+
puts "Initializing SimpleWorker for Rails 3..."
|
12
|
+
SimpleWorker.configure do |c2|
|
13
|
+
path = File.join(Rails.root, 'app/models/*.rb')
|
14
|
+
# puts 'path=' + path
|
15
|
+
c2.models = Dir.glob(path)
|
16
|
+
c2.extra_requires += ['active_support/core_ext', 'active_record', 'action_mailer']
|
17
|
+
# puts 'config.models=' + c2.models.inspect
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/simple_worker/base.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# This is an abstract module that developers creating works can mixin/include to use the SimpleWorker special functions.
|
2
|
-
|
3
1
|
require 'digest/md5'
|
4
2
|
require 'base64'
|
5
3
|
|
@@ -13,11 +11,13 @@ module SimpleWorker
|
|
13
11
|
attr_accessor :subclass, :caller_file
|
14
12
|
@merged = []
|
15
13
|
@merged_workers = []
|
14
|
+
@merged_gems = []
|
16
15
|
@unmerged = []
|
17
16
|
|
18
17
|
def reset!
|
19
18
|
@merged = []
|
20
19
|
@merged_workers = []
|
20
|
+
@merged_gems = []
|
21
21
|
@unmerged = []
|
22
22
|
end
|
23
23
|
|
@@ -64,6 +64,12 @@ module SimpleWorker
|
|
64
64
|
f
|
65
65
|
end
|
66
66
|
|
67
|
+
# merges the specified gem.
|
68
|
+
def merge_gem(gem_name, version=nil)
|
69
|
+
@merged_gems << {:name=>gem_name, :version=>version}
|
70
|
+
require gem_name
|
71
|
+
end
|
72
|
+
|
67
73
|
# merges the specified files.
|
68
74
|
# todo: don't allow multiple files per merge, just one like require
|
69
75
|
def merge(*files)
|
@@ -235,10 +241,11 @@ module SimpleWorker
|
|
235
241
|
before_upload
|
236
242
|
|
237
243
|
# puts 'upload_if_needed ' + self.class.name
|
238
|
-
# Todo, watch for this file changing or something so we can reupload
|
244
|
+
# Todo, watch for this file changing or something so we can reupload (if in dev env)
|
239
245
|
unless uploaded?
|
240
246
|
merged = self.class.instance_variable_get(:@merged)
|
241
247
|
unmerged = self.class.instance_variable_get(:@unmerged)
|
248
|
+
merged_gems = self.class.instance_variable_get(:@merged_gems)
|
242
249
|
# puts 'merged1=' + merged.inspect
|
243
250
|
|
244
251
|
subclass = self.class
|
@@ -257,7 +264,7 @@ module SimpleWorker
|
|
257
264
|
# puts 'merged with superclass=' + merged.inspect
|
258
265
|
end
|
259
266
|
merged += SimpleWorker.config.models if SimpleWorker.config.models
|
260
|
-
SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged, :unmerge=>unmerged)
|
267
|
+
SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged, :unmerge=>unmerged, :merged_gems=>merged_gems)
|
261
268
|
self.class.instance_variable_set(:@uploaded, true)
|
262
269
|
else
|
263
270
|
SimpleWorker.logger.debug 'Already uploaded for ' + self.class.name
|
@@ -1,236 +1,249 @@
|
|
1
|
-
require 'base64'
|
2
|
-
require 'logger'
|
3
|
-
require 'appoxy_api'
|
4
|
-
|
5
|
-
module SimpleWorker
|
6
|
-
|
7
|
-
@@logger
|
8
|
-
@@logger.level = Logger::INFO
|
9
|
-
|
10
|
-
def self.logger
|
11
|
-
@@logger
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
#
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
#
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
#
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
#
|
66
|
-
#
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
if
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
#
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
File.open(fname2, "w") do |f|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
hash_to_send =
|
166
|
-
hash_to_send["
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
#
|
187
|
-
#
|
188
|
-
#
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
#
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
hash_to_send
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
ret
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
ret
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
ret
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
1
|
+
require 'base64'
|
2
|
+
require 'logger'
|
3
|
+
require 'appoxy_api'
|
4
|
+
require 'zip'
|
5
|
+
module SimpleWorker
|
6
|
+
|
7
|
+
@@logger = Logger.new(STDOUT)
|
8
|
+
@@logger.level = Logger::INFO
|
9
|
+
|
10
|
+
def self.logger
|
11
|
+
@@logger
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.api_version
|
15
|
+
3
|
16
|
+
end
|
17
|
+
|
18
|
+
class Service < Appoxy::Api::Client
|
19
|
+
|
20
|
+
attr_accessor :config
|
21
|
+
|
22
|
+
def initialize(access_key, secret_key, options={})
|
23
|
+
SimpleWorker.logger.info 'Starting SimpleWorker::Service...'
|
24
|
+
self.config = options[:config] if options[:config]
|
25
|
+
super("http://api.simpleworker.com/api/", access_key, secret_key, options)
|
26
|
+
self.host = self.config.host if self.config && self.config.host
|
27
|
+
end
|
28
|
+
|
29
|
+
# Options:
|
30
|
+
# - :callback_url
|
31
|
+
# - :merge => array of files to merge in with this file
|
32
|
+
def upload(filename, class_name, options={})
|
33
|
+
# puts "Uploading #{class_name}"
|
34
|
+
# check whether it should upload again
|
35
|
+
tmp = Dir.tmpdir()
|
36
|
+
md5file = "simple_worker_#{class_name.gsub("::", ".")}_#{access_key[0, 8]}.md5"
|
37
|
+
existing_md5 = nil
|
38
|
+
f = File.join(tmp, md5file)
|
39
|
+
if File.exists?(f)
|
40
|
+
existing_md5 = IO.read(f)
|
41
|
+
end
|
42
|
+
# Check for code changes.
|
43
|
+
md5 = Digest::MD5.hexdigest(File.read(filename))
|
44
|
+
new_code = false
|
45
|
+
if md5 != existing_md5
|
46
|
+
SimpleWorker.logger.info "Uploading #{class_name}, code modified."
|
47
|
+
File.open(f, 'w') { |f| f.write(md5) }
|
48
|
+
new_code = true
|
49
|
+
else
|
50
|
+
# puts "#{class_name}: same code, not uploading"
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
filename = build_merged_file(filename, options[:merge], options[:unmerge], options[:merged_gems])
|
55
|
+
|
56
|
+
# sys.classes[subclass].__file__
|
57
|
+
# puts '__FILE__=' + Base.subclass.__file__.to_s
|
58
|
+
# puts "new md5=" + md5
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
if new_code
|
63
|
+
# mystring = nil
|
64
|
+
# file = File.open(filename, "r") do |f|
|
65
|
+
# mystring = f.read
|
66
|
+
# end
|
67
|
+
# mystring = Base64.encode64(mystring)
|
68
|
+
# puts 'code=' + mystring
|
69
|
+
options = {"class_name"=>class_name, }
|
70
|
+
ret = post_file("code/put", File.new(filename), options)
|
71
|
+
ret
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def get_gem_path(full_gem_name, version=nil)
|
76
|
+
gem_name =(full_gem_name.match(/^[a-zA-Z0-9\-_]+/)[0])
|
77
|
+
puts "Searching #{gem_name}"
|
78
|
+
searcher = Gem::GemPathSearcher.new
|
79
|
+
gems = searcher.find_all(gem_name)
|
80
|
+
gems = gems.select { |g| g.version.version==version } if version
|
81
|
+
if !gems.empty?
|
82
|
+
gem = gems.first
|
83
|
+
gem.full_gem_path + "/lib"
|
84
|
+
else
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def build_merged_file(filename, merge, unmerge, merged_gems)
|
90
|
+
# unless (merge && merge.size > 0) || (merged_gems && merged_gems.size > 0)
|
91
|
+
# return filename
|
92
|
+
# end
|
93
|
+
merge = merge.dup
|
94
|
+
if unmerge
|
95
|
+
unmerge.each do |x|
|
96
|
+
deleted = merge.delete x
|
97
|
+
# puts "Unmerging #{x}. Success? #{deleted}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
merge.uniq!
|
101
|
+
tmp_file = File.join(Dir.tmpdir(), File.basename(filename))
|
102
|
+
File.open(tmp_file, "w") do |f|
|
103
|
+
if SimpleWorker.config.extra_requires
|
104
|
+
SimpleWorker.config.extra_requires.each do |r|
|
105
|
+
f.write "require '#{r}'\n"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
f.write File.open(filename, 'r') { |mo| mo.read }
|
109
|
+
end
|
110
|
+
merge << tmp_file
|
111
|
+
#puts "merge before uniq! " + merge.inspect
|
112
|
+
# puts "merge after uniq! " + merge.inspect
|
113
|
+
|
114
|
+
fname2 = tmp_file + ".zip"
|
115
|
+
# puts 'fname2=' + fname2
|
116
|
+
# puts 'merged_file_array=' + merge.inspect
|
117
|
+
#File.open(fname2, "w") do |f|
|
118
|
+
File.delete(fname2) if File.exist?(fname2)
|
119
|
+
|
120
|
+
Zip::ZipFile.open(fname2, 'w') do |f|
|
121
|
+
if merged_gems
|
122
|
+
merged_gems.each do |gem|
|
123
|
+
path = get_gem_path(gem[:name], gem[:version])
|
124
|
+
if path
|
125
|
+
puts "Collecting gem #{path}"
|
126
|
+
Dir["#{path}/**/**"].each do |file|
|
127
|
+
f.add('gems/'+"#{gem["name"]}/"+ file.sub(path+'/', ''), file)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
merge.each do |m|
|
133
|
+
# puts "merging #{m} into #{filename}"
|
134
|
+
f.add(File.basename(m), m)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
fname2
|
138
|
+
end
|
139
|
+
|
140
|
+
def add_sw_params(hash_to_send)
|
141
|
+
# todo: remove secret key?? Can use worker service from within a worker without it now
|
142
|
+
hash_to_send["sw_access_key"] = self.access_key
|
143
|
+
hash_to_send["sw_secret_key"] = self.secret_key
|
144
|
+
hash_to_send["api_version"] = SimpleWorker.api_version
|
145
|
+
end
|
146
|
+
|
147
|
+
def check_config
|
148
|
+
if self.config.nil? || self.config.access_key.nil?
|
149
|
+
raise "Invalid SimpleWorker configuration, no access key specified."
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# class_name: The class name of a previously upload class, eg: MySuperWorker
|
154
|
+
# data: Arbitrary hash of your own data that your task will need to run.
|
155
|
+
def queue(class_name, data={}, options={})
|
156
|
+
puts "Queuing #{class_name}..."
|
157
|
+
check_config
|
158
|
+
if !data.is_a?(Array)
|
159
|
+
data = [data]
|
160
|
+
end
|
161
|
+
# p data
|
162
|
+
hash_to_send = {}
|
163
|
+
hash_to_send["payload"] = data
|
164
|
+
hash_to_send["class_name"] = class_name
|
165
|
+
hash_to_send["priority"] = options[:priority] if options[:priority]
|
166
|
+
hash_to_send["options"] = options
|
167
|
+
add_sw_params(hash_to_send)
|
168
|
+
if defined?(RAILS_ENV)
|
169
|
+
# todo: move this to global_attributes in railtie
|
170
|
+
hash_to_send["rails_env"] = RAILS_ENV
|
171
|
+
end
|
172
|
+
return queue_raw(class_name, hash_to_send)
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
def queue_raw(class_name, data={})
|
177
|
+
params = nil
|
178
|
+
hash_to_send = data
|
179
|
+
hash_to_send["class_name"] = class_name
|
180
|
+
ret = post("queue/add", hash_to_send)
|
181
|
+
ret
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
#
|
187
|
+
# schedule: hash of scheduling options that can include:
|
188
|
+
# Required:
|
189
|
+
# - start_at: Time of first run - DateTime or Time object.
|
190
|
+
# Optional:
|
191
|
+
# - run_every: Time in seconds between runs. If ommitted, task will only run once.
|
192
|
+
# - delay_type: Fixed Rate or Fixed Delay. Default is fixed_delay.
|
193
|
+
# - end_at: Scheduled task will stop running after this date (optional, if ommitted, runs forever or until cancelled)
|
194
|
+
# - run_times: Task will run exactly :run_times. For instance if :run_times is 5, then the task will run 5 times.
|
195
|
+
#
|
196
|
+
def schedule(class_name, data, schedule)
|
197
|
+
puts "Scheduling #{class_name}..."
|
198
|
+
raise "Schedule must be a hash." if !schedule.is_a? Hash
|
199
|
+
# if !data.is_a?(Array)
|
200
|
+
# data = [data]
|
201
|
+
# end
|
202
|
+
hash_to_send = {}
|
203
|
+
hash_to_send["payload"] = data
|
204
|
+
hash_to_send["class_name"] = class_name
|
205
|
+
hash_to_send["schedule"] = schedule
|
206
|
+
add_sw_params(hash_to_send)
|
207
|
+
# puts ' about to send ' + hash_to_send.inspect
|
208
|
+
ret = post("scheduler/schedule", hash_to_send)
|
209
|
+
ret
|
210
|
+
end
|
211
|
+
|
212
|
+
def cancel_schedule(scheduled_task_id)
|
213
|
+
raise "Must include a schedule id." if scheduled_task_id.blank?
|
214
|
+
hash_to_send = {}
|
215
|
+
hash_to_send["scheduled_task_id"] = scheduled_task_id
|
216
|
+
ret = post("scheduler/cancel", hash_to_send)
|
217
|
+
ret
|
218
|
+
end
|
219
|
+
|
220
|
+
def get_schedules()
|
221
|
+
hash_to_send = {}
|
222
|
+
ret = get("scheduler/list", hash_to_send)
|
223
|
+
ret
|
224
|
+
end
|
225
|
+
|
226
|
+
def status(task_id)
|
227
|
+
data = {"task_id"=>task_id}
|
228
|
+
ret = get("task/status", data)
|
229
|
+
ret
|
230
|
+
end
|
231
|
+
|
232
|
+
def schedule_status(schedule_id)
|
233
|
+
data = {"schedule_id"=>schedule_id}
|
234
|
+
ret = get("scheduler/status", data)
|
235
|
+
ret
|
236
|
+
end
|
237
|
+
|
238
|
+
def log(task_id)
|
239
|
+
data = {"task_id"=>task_id}
|
240
|
+
ret = get("task/log", data, {:parse=>false})
|
241
|
+
# puts ' ret=' + ret.inspect
|
242
|
+
# ret["log"] = Base64.decode64(ret["log"])
|
243
|
+
ret
|
244
|
+
end
|
245
|
+
|
246
|
+
|
247
|
+
end
|
248
|
+
|
236
249
|
end
|
data/lib/simple_worker.rb
CHANGED
@@ -13,11 +13,13 @@ module SimpleWorker
|
|
13
13
|
:service
|
14
14
|
|
15
15
|
def configure()
|
16
|
-
SimpleWorker.config ||= Config.new
|
17
16
|
yield(config)
|
18
17
|
SimpleWorker.service = Service.new(config.access_key, config.secret_key, :config=>config)
|
19
18
|
end
|
20
19
|
|
20
|
+
def config
|
21
|
+
@config ||= Config.new
|
22
|
+
end
|
21
23
|
|
22
24
|
end
|
23
25
|
|
data/test/cool_model.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'simple_record'
|
2
|
-
|
3
|
-
class CoolModel < SimpleRecord::Base
|
4
|
-
|
5
|
-
has_strings :name
|
6
|
-
|
7
|
-
end
|
1
|
+
require 'simple_record'
|
2
|
+
|
3
|
+
class CoolModel < SimpleRecord::Base
|
4
|
+
|
5
|
+
has_strings :name
|
6
|
+
|
7
|
+
end
|
data/test/cool_worker.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
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
|
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
CHANGED
@@ -1,19 +1,18 @@
|
|
1
|
-
require 'mysql2'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
to
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
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
|
data/test/models/model_1.rb
CHANGED
data/test/models/model_2.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
begin
|
2
|
-
require File.join(File.dirname(__FILE__), '../../lib/simple_worker')
|
3
|
-
rescue Exception => ex
|
4
|
-
puts '
|
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
|
+
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
|
data/test/requiring_worker.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require_relative 'models/model_1.rb'
|
4
|
-
|
1
|
+
|
2
|
+
|
3
|
+
require_relative 'models/model_1.rb'
|
4
|
+
|
data/test/test_no_config.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
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
|
+
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
|
+
|
data/test/test_simple_worker.rb
CHANGED
@@ -28,7 +28,7 @@ class SimpleWorkerTests < TestBase
|
|
28
28
|
puts 'queuing ' + tw.inspect
|
29
29
|
|
30
30
|
response_hash_single = nil
|
31
|
-
|
31
|
+
5.times do |i|
|
32
32
|
begin
|
33
33
|
response_hash_single = tw.queue
|
34
34
|
rescue => ex
|
@@ -38,7 +38,7 @@ class SimpleWorkerTests < TestBase
|
|
38
38
|
|
39
39
|
puts 'response_hash=' + response_hash_single.inspect
|
40
40
|
puts 'task_id=' + tw.task_id
|
41
|
-
|
41
|
+
10.times do |i|
|
42
42
|
puts "status #{i}: " + tw.status.inspect
|
43
43
|
break if tw.status["status"] == "complete"
|
44
44
|
sleep 2
|
data/test/test_worker.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
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
|
+
#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
|
+
|
data/test/test_worker_2.rb
CHANGED
@@ -7,10 +7,7 @@ rescue Exception => ex
|
|
7
7
|
end
|
8
8
|
=end
|
9
9
|
|
10
|
-
# Bump for new checksum.sdf adsfjlaksdjf
|
11
|
-
|
12
|
-
require 'json'
|
13
|
-
|
10
|
+
# Bump for new checksum.sdf adsfjlaksdjf dasfasdfadsf
|
14
11
|
class TestWorker2 < SimpleWorker::Base
|
15
12
|
|
16
13
|
merge File.join(File.dirname(__FILE__), 'models', 'model_1.rb')
|
@@ -40,7 +37,7 @@ class TestWorker2 < SimpleWorker::Base
|
|
40
37
|
|
41
38
|
second_workers = []
|
42
39
|
now = Time.now
|
43
|
-
|
40
|
+
10.times do |i|
|
44
41
|
second_worker = SecondWorker.new
|
45
42
|
second_worker.start_time = now
|
46
43
|
second_worker.num = i
|
data/test/trace_object.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
class TraceObject < ActiveRecord::Base
|
2
|
-
belongs_to :tracking_device
|
3
|
-
has_many :location_updates
|
4
|
-
|
5
|
-
|
6
|
-
end
|
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
ADDED
@@ -0,0 +1,26 @@
|
|
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)}"
|
metadata
CHANGED
@@ -1,35 +1,44 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_worker
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 5
|
8
|
+
- 1
|
9
|
+
version: 0.5.1
|
6
10
|
platform: ruby
|
7
|
-
authors:
|
11
|
+
authors:
|
8
12
|
- Travis Reeder
|
9
13
|
autorequire:
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
|
-
|
16
|
+
|
17
|
+
date: 2011-03-26 00:00:00 -07:00
|
13
18
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
16
21
|
name: appoxy_api
|
17
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
24
|
none: false
|
19
|
-
requirements:
|
20
|
-
- -
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
23
31
|
type: :runtime
|
24
|
-
|
25
|
-
version_requirements: *22838376
|
32
|
+
version_requirements: *id001
|
26
33
|
description: The official SimpleWorker gem for http://www.simpleworker.com
|
27
34
|
email: travis@appoxy.com
|
28
35
|
executables: []
|
36
|
+
|
29
37
|
extensions: []
|
30
|
-
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
31
40
|
- README.markdown
|
32
|
-
files:
|
41
|
+
files:
|
33
42
|
- lib/rails2_init.rb
|
34
43
|
- lib/railtie.rb
|
35
44
|
- lib/simple_worker.rb
|
@@ -55,32 +64,40 @@ files:
|
|
55
64
|
- test/test_worker_2.rb
|
56
65
|
- test/test_worker_3.rb
|
57
66
|
- test/trace_object.rb
|
67
|
+
- test/zip_vs_gzip.rb
|
58
68
|
has_rdoc: true
|
59
69
|
homepage: http://github.com/appoxy/simple_worker
|
60
70
|
licenses: []
|
71
|
+
|
61
72
|
post_install_message:
|
62
73
|
rdoc_options: []
|
63
|
-
|
74
|
+
|
75
|
+
require_paths:
|
64
76
|
- lib
|
65
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
78
|
none: false
|
67
|
-
requirements:
|
68
|
-
- -
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
|
71
|
-
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
86
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
77
93
|
requirements: []
|
94
|
+
|
78
95
|
rubyforge_project:
|
79
|
-
rubygems_version: 1.
|
96
|
+
rubygems_version: 1.3.7
|
80
97
|
signing_key:
|
81
98
|
specification_version: 3
|
82
99
|
summary: The official SimpleWorker gem for http://www.simpleworker.com
|
83
|
-
test_files:
|
100
|
+
test_files:
|
84
101
|
- test/cool_model.rb
|
85
102
|
- test/cool_worker.rb
|
86
103
|
- test/db_worker.rb
|
@@ -97,3 +114,4 @@ test_files:
|
|
97
114
|
- test/test_worker_2.rb
|
98
115
|
- test/test_worker_3.rb
|
99
116
|
- test/trace_object.rb
|
117
|
+
- test/zip_vs_gzip.rb
|