simple_worker 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,19 +28,24 @@ module SimpleWorker
28
28
  def upload(filename, class_name, options={})
29
29
  # puts "Uploading #{class_name}"
30
30
  # check whether it should upload again
31
- tmp = Dir.tmpdir()
32
- md5file = "simple_worker_#{class_name.gsub("::", ".")}_#{access_key[0,8]}.md5"
31
+ tmp = Dir.tmpdir()
32
+ md5file = "simple_worker_#{class_name.gsub("::", ".")}_#{access_key[0, 8]}.md5"
33
33
  existing_md5 = nil
34
- f = File.join(tmp, md5file)
34
+ f = File.join(tmp, md5file)
35
35
  if File.exists?(f)
36
36
  existing_md5 = IO.read(f)
37
37
  end
38
38
 
39
+ # todo: no point in writing the merged file, just use a string, we're just reading it back in anyways
39
40
  filename = build_merged_file(filename, options[:merge], options[:unmerge]) if options[:merge]
40
41
 
41
42
  # sys.classes[subclass].__file__
42
43
  # puts '__FILE__=' + Base.subclass.__file__.to_s
43
- md5 = Digest::MD5.hexdigest(File.read(filename))
44
+ contents = File.read(filename)
45
+ # puts 'START CONTENTS:'
46
+ # puts contents
47
+ # puts 'END CONTENTS'
48
+ md5 = Digest::MD5.hexdigest(contents)
44
49
  # puts "new md5=" + md5
45
50
 
46
51
  new_code = false
@@ -54,23 +59,23 @@ module SimpleWorker
54
59
 
55
60
  if new_code
56
61
  mystring = nil
57
- file = File.open(filename, "r") do |f|
62
+ file = File.open(filename, "r") do |f|
58
63
  mystring = f.read
59
64
  end
60
65
  # mystring = Base64.encode64(mystring)
61
66
  # puts 'code=' + mystring
62
- options = {"code"=>mystring, "class_name"=>class_name}
63
- ret = post("code/put", options)
67
+ options = {"code"=>mystring, "class_name"=>class_name}
68
+ ret = post("code/put", options)
64
69
  ret
65
70
  end
66
71
  end
67
72
 
68
73
  def build_merged_file(filename, merge, unmerge)
69
74
  merge = merge.dup
70
- merge << filename
75
+ # merge << filename
71
76
  #puts "merge before uniq! " + merge.inspect
72
77
  merge.uniq!
73
- # puts "merge after uniq! " + merge.inspect
78
+ # puts "merge after uniq! " + merge.inspect
74
79
 
75
80
  if unmerge
76
81
  unmerge.each do |x|
@@ -82,17 +87,47 @@ module SimpleWorker
82
87
  fname2 = File.join(Dir.tmpdir(), File.basename(filename))
83
88
  # puts 'fname2=' + fname2
84
89
  # puts 'merged_file_array=' + merge.inspect
90
+
91
+ # Let's put the requires from the worker at the top
92
+ worker_file_contents_array = File.readlines(filename)
93
+ requires_lines = ""
94
+ in_comment = false
95
+ worker_file_contents_array.each do |line|
96
+ line = line.strip
97
+ # puts 'checking for requires in ' + line
98
+ next if line.length == 0
99
+ next if line[0] == "#"
100
+ # puts 'line[0, 6]=' + line[0, 6]
101
+ if in_comment && line[0,4] == "=end"
102
+ in_comment = false
103
+ next
104
+ elsif in_comment
105
+ next
106
+ elsif line[0, 6] == "=begin"
107
+ in_comment = true
108
+ next
109
+ end
110
+ if line.index("require") == 0
111
+ requires_lines << line + "\n"
112
+ else
113
+ break
114
+ end
115
+ end
116
+
85
117
  File.open(fname2, "w") do |f|
86
- if SimpleWorker.config.extra_requires
87
- SimpleWorker.config.extra_requires.each do |r|
88
- f.write "require '#{r}'\n"
89
- end
90
- end
118
+ if SimpleWorker.config.extra_requires
119
+ SimpleWorker.config.extra_requires.each do |r|
120
+ f.write "require '#{r}'\n"
121
+ end
122
+ end
123
+ # puts 'Adding requires lines ' + requires_lines.inspect
124
+ f.write requires_lines
91
125
  merge.each do |m|
92
126
  # puts "merging #{m} into #{filename}"
93
127
  f.write File.open(m, 'r') { |mo| mo.read }
94
128
  f.write "\n\n"
95
129
  end
130
+ f.write worker_file_contents_array.join("\n")
96
131
  end
97
132
  fname2
98
133
  end
@@ -111,8 +146,8 @@ module SimpleWorker
111
146
  data = [data]
112
147
  end
113
148
  # p data
114
- hash_to_send = {}
115
- hash_to_send["payload"] = data
149
+ hash_to_send = {}
150
+ hash_to_send["payload"] = data
116
151
  hash_to_send["class_name"] = class_name
117
152
  hash_to_send["priority"] = options[:priority] if options[:priority]
118
153
  hash_to_send["options"] = options
@@ -126,10 +161,10 @@ module SimpleWorker
126
161
  end
127
162
 
128
163
  def queue_raw(class_name, data={})
129
- params = nil
130
- hash_to_send = data
164
+ params = nil
165
+ hash_to_send = data
131
166
  hash_to_send["class_name"] = class_name
132
- ret = post("queue/add", hash_to_send)
167
+ ret = post("queue/add", hash_to_send)
133
168
  ret
134
169
 
135
170
  end
@@ -151,10 +186,10 @@ module SimpleWorker
151
186
  # if !data.is_a?(Array)
152
187
  # data = [data]
153
188
  # end
154
- hash_to_send = {}
155
- hash_to_send["payload"] = data
189
+ hash_to_send = {}
190
+ hash_to_send["payload"] = data
156
191
  hash_to_send["class_name"] = class_name
157
- hash_to_send["schedule"] = schedule
192
+ hash_to_send["schedule"] = schedule
158
193
  add_sw_params(hash_to_send)
159
194
  # puts 'about to send ' + hash_to_send.inspect
160
195
  ret = post("scheduler/schedule", hash_to_send)
@@ -163,33 +198,33 @@ module SimpleWorker
163
198
 
164
199
  def cancel_schedule(scheduled_task_id)
165
200
  raise "Must include a schedule id." if scheduled_task_id.blank?
166
- hash_to_send = {}
201
+ hash_to_send = {}
167
202
  hash_to_send["scheduled_task_id"] = scheduled_task_id
168
- ret = post("scheduler/cancel", hash_to_send)
203
+ ret = post("scheduler/cancel", hash_to_send)
169
204
  ret
170
205
  end
171
206
 
172
207
  def get_schedules()
173
208
  hash_to_send = {}
174
- ret = get("scheduler/list", hash_to_send)
209
+ ret = get("scheduler/list", hash_to_send)
175
210
  ret
176
211
  end
177
212
 
178
213
  def status(task_id)
179
214
  data = {"task_id"=>task_id}
180
- ret = get("task/status", data)
215
+ ret = get("task/status", data)
181
216
  ret
182
217
  end
183
218
 
184
219
  def schedule_status(schedule_id)
185
220
  data = {"schedule_id"=>schedule_id}
186
- ret = get("scheduler/status", data)
221
+ ret = get("scheduler/status", data)
187
222
  ret
188
223
  end
189
224
 
190
225
  def log(task_id)
191
226
  data = {"task_id"=>task_id}
192
- ret = get("task/log", data, {:parse=>false})
227
+ ret = get("task/log", data, {:parse=>false})
193
228
  # puts 'ret=' + ret.inspect
194
229
  # ret["log"] = Base64.decode64(ret["log"])
195
230
  ret
data/test/db_worker.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'mysql2'
2
2
 
3
+ # bump asdf
3
4
  class DbWorker < SimpleWorker::Base
4
5
  attr_accessor :array_of_models
5
6
  merge 'trace_object'
@@ -1,7 +1,7 @@
1
1
  begin
2
2
  require File.join(File.dirname(__FILE__), '../../lib/simple_worker')
3
3
  rescue Exception => ex
4
- puts 'ERROR!!! ' + ex.message
4
+ puts 'Could not load relative simple_worker: ' + ex.message
5
5
  # require 'simple_worker'
6
6
  end
7
7
 
@@ -1,7 +1,7 @@
1
1
  begin
2
2
  require File.join(File.dirname(__FILE__), '../../lib/simple_worker')
3
3
  rescue Exception => ex
4
- puts 'ERROR!!! ' + ex.message
4
+ puts 'Could not load relative simple_worker: ' + ex.message
5
5
  # require 'simple_worker'
6
6
  end
7
7
 
@@ -28,7 +28,7 @@ class SimpleWorkerTests < TestBase
28
28
  puts 'queuing ' + tw.inspect
29
29
 
30
30
  response_hash_single = nil
31
- 5.times do |i|
31
+ 1.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
- 10.times do |i|
41
+ 20.times do |i|
42
42
  puts "status #{i}: " + tw.status.inspect
43
43
  break if tw.status["status"] == "complete"
44
44
  sleep 2
@@ -7,7 +7,10 @@ rescue Exception => ex
7
7
  end
8
8
  =end
9
9
 
10
- # Bump for new checksum.sdf adsfjlaksdjf dasfasdfadsf
10
+ # Bump for new checksum.sdf adsfjlaksdjf d
11
+
12
+ require 'json'
13
+
11
14
  class TestWorker2 < SimpleWorker::Base
12
15
 
13
16
  merge File.join(File.dirname(__FILE__), 'models', 'model_1.rb')
@@ -37,7 +40,7 @@ class TestWorker2 < SimpleWorker::Base
37
40
 
38
41
  second_workers = []
39
42
  now = Time.now
40
- 10.times do |i|
43
+ 1.times do |i|
41
44
  second_worker = SecondWorker.new
42
45
  second_worker.start_time = now
43
46
  second_worker.num = i
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.4.0
4
+ version: 0.4.1
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-24 00:00:00.000000000 -08:00
12
+ date: 2011-03-07 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: &27678636 !ruby/object:Gem::Requirement
17
+ requirement: &22838376 !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: *27678636
25
+ version_requirements: *22838376
26
26
  description: The official SimpleWorker gem for http://www.simpleworker.com
27
27
  email: travis@appoxy.com
28
28
  executables: []
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  version: '0'
77
77
  requirements: []
78
78
  rubyforge_project:
79
- rubygems_version: 1.5.2
79
+ rubygems_version: 1.6.0
80
80
  signing_key:
81
81
  specification_version: 3
82
82
  summary: The official SimpleWorker gem for http://www.simpleworker.com