simple_worker 2.0.0.beta.7 → 2.0.0.beta.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -33,17 +33,18 @@ module SimpleWorker
33
33
  attr_accessor :host, :port, :token, :version, :config
34
34
 
35
35
  def initialize(host, token, options={})
36
- @host = host
37
36
  @config = options[:config]
37
+ @scheme = options[:scheme] || @config.scheme || "https"
38
+ @host = options[:host] || @config.host || host
38
39
  @port = options[:port] || @config.port || 80
39
- @token = token
40
+ @token = options[:token] || @config.token || token
40
41
  @version = options[:version]
41
42
  @logger = options[:logger]
42
43
 
43
44
  end
44
45
 
45
46
  def url(command_path)
46
- url = "http://#{host}:#{port}/#{@version}/#{command_path}"
47
+ url = "#{@scheme}://#{host}:#{port}/#{@version}/#{command_path}"
47
48
  # @logger.debug "url: " + url.to_s
48
49
  url
49
50
  end
@@ -73,10 +74,12 @@ module SimpleWorker
73
74
  def post_file(method, file, params={}, options={})
74
75
  begin
75
76
  data = add_params(method, params).to_json
77
+ url = url(method) + "?oauth=" + token
78
+ @logger.debug "post_file url = " + url
76
79
  @logger.debug "data = " + data
77
80
  @logger.debug "params = " + params.inspect
78
81
  @logger.debug "options = " + options.inspect
79
- parse_response RestClient.post(url(method) + "?oauth=" + token, {:data => data, :file => file}, :content_type => 'application/json', :accept => :json), options
82
+ parse_response RestClient.post(url, {:data => data, :file => file}, :content_type => 'application/json', :accept => :json), options
80
83
  #parse_response(RestClient.post(append_params(url(method), add_params(method, params)), {:data => data, :file => file}, :content_type => 'application/json'), options)
81
84
  rescue RestClient::Exception => ex
82
85
  process_ex(ex)
@@ -90,7 +93,7 @@ module SimpleWorker
90
93
  @logger.debug "token = "+ token.inspect
91
94
  begin
92
95
  url = url(method) + "?oauth=" + token
93
- @logger.debug 'post url=' + url
96
+ @logger.warn 'post url=' + url
94
97
  parse_response(RestClient.post(url, add_params(method, params).to_json, headers.merge!({:content_type=>'application/json', :accept => "json"})), options)
95
98
  rescue RestClient::Exception => ex
96
99
  @logger.warn("Exception in post! #{ex.message}")
@@ -12,6 +12,7 @@ module SimpleWorker
12
12
  class Config
13
13
  attr_accessor :token,
14
14
  :project_id,
15
+ :scheme,
15
16
  :host,
16
17
  :port,
17
18
  :global_attributes,
@@ -157,6 +158,7 @@ module SimpleWorker
157
158
  config_data['database'] = self.database if self.database
158
159
  config_data['mailer'] = self.mailer if self.mailer
159
160
  config_data['global_attributes'] = self.global_attributes if self.global_attributes
161
+ config_data['scheme'] = self.scheme if self.scheme
160
162
  config_data['host'] = self.host if self.host
161
163
  config_data['port'] = self.port if self.port
162
164
  config_data
@@ -98,7 +98,8 @@ module SimpleWorker
98
98
  end
99
99
 
100
100
  def set_progress(hash)
101
- SimpleWorker.service.set_progress(@job_data["task_id"], hash)
101
+ #puts 'set_progress self=' + self.inspect
102
+ SimpleWorker.service.set_progress(self.task_id, hash)
102
103
  end
103
104
 
104
105
  def something
@@ -1,6 +1,5 @@
1
1
  # This is the file that gets executed on the server.
2
2
 
3
-
4
3
  def init_database_connection(sw_config)
5
4
  if sw_config
6
5
  db_config = sw_config['database']
@@ -42,7 +41,7 @@ def constantize(camel_cased_word)
42
41
  constant
43
42
  end
44
43
 
45
- def init_runner(runner_class, job_data, user_dir)
44
+ def init_runner(runner_class, job_data, user_dir, task_id)
46
45
  # ensure initialize takes no arguments
47
46
  init_arity = runner_class.instance_method(:initialize).arity
48
47
  if init_arity == 0 || init_arity == -1
@@ -51,6 +50,7 @@ def init_runner(runner_class, job_data, user_dir)
51
50
  raise SimpleWorker::InvalidWorkerError, "Worker initialize method must accept zero arguments."
52
51
  end
53
52
  runner = runner_class.new
53
+ runner.instance_variable_set(:@task_id, task_id)
54
54
  runner.instance_variable_set(:@job_data, job_data)
55
55
  runner.instance_variable_set(:@sw_config, job_data['sw_config'])
56
56
  runner.instance_variable_set(:@user_dir, user_dir)
@@ -63,7 +63,7 @@ def init_worker_service_for_runner(job_data)
63
63
  sw_config = job_data['sw_config']
64
64
  config.token = sw_config['token']
65
65
  config.project_id = sw_config['project_id']
66
- #puts 'Setting host to ' + host.inspect
66
+ config.scheme = sw_config['scheme'] if sw_config['scheme']
67
67
  config.host = sw_config['host'] if sw_config['host']
68
68
  config.port = sw_config['port'] if sw_config['port']
69
69
  db_config = sw_config['database']
@@ -78,22 +78,3 @@ def init_worker_service_for_runner(job_data)
78
78
  end
79
79
  end
80
80
 
81
- # SimpleWorker.logger.level == Logger::DEBUG
82
- run_data = JSON.load(File.open(run_data_file))
83
- # Load in job data
84
- job_data = JSON.load(File.open(job_data_file))
85
- job_data.merge!(run_data)
86
- SimpleWorker.logger.debug 'job_data=' + job_data.inspect
87
-
88
- sw_config = job_data['sw_config']
89
- init_database_connection(sw_config)
90
- init_mailer(sw_config)
91
- SimpleWorker.disable_queueing()
92
- runner_class = get_class_to_run(job_data['class_name'])
93
- SimpleWorker.running_class = runner_class
94
- runner = init_runner(runner_class, job_data, dirname)
95
- init_worker_service_for_runner(job_data)
96
- SimpleWorker.enable_queueing()
97
-
98
- # Let's run it!
99
- runner_return_data = runner.run
@@ -125,38 +125,72 @@ module SimpleWorker
125
125
  #tmp_file = File.join(Dir.tmpdir(), File.basename(filename))
126
126
  tmp_file = File.join(Dir.tmpdir(), 'runner.rb')
127
127
  File.open(tmp_file, "w") do |f|
128
+ File.open(File.join(File.dirname(__FILE__), "server", 'runner.rb'), 'r') do |fr|
129
+ while line = fr.gets
130
+ f.write line
131
+ end
132
+ end
128
133
  f.write("begin\n")#error handling block start
129
134
  f.write("
130
135
  # Find environment (-e)
131
136
  dirname = ''
132
137
  i = 0
133
- job_data_file = run_data_file = nil
138
+ task_data_file = nil
139
+ task_id = nil
134
140
  #puts \"args for single file=\" + ARGV.inspect
135
141
  ARGV.each do |arg|
136
142
  if arg == \"-d\"
137
143
  # the user's writable directory
138
144
  dirname = ARGV[i+1]
139
145
  end
140
- if arg == \"-j\"
141
- # path to job data
142
- job_data_file = ARGV[i+1]
146
+ if arg == \"-id\"
147
+ # task_id
148
+ task_id = ARGV[i+1]
143
149
  end
144
- if arg == \"-p\"
145
- # path to run data
146
- run_data_file = ARGV[i+1]
150
+ if arg == \"-payload\"
151
+ # path to job data
152
+ task_data_file = ARGV[i+1]
147
153
  end
148
154
  i+=1
149
155
  end
156
+ require 'json'
157
+ ")
158
+ # require merged gems
159
+ merged_gems.each_pair do |k, gem|
160
+ SimpleWorker.logger.debug "Bundling gem #{gem[:name]}..."
161
+ if gem[:merge]
162
+ f.write "$LOAD_PATH << File.join(File.dirname(__FILE__), '/gems/#{gem[:name]}/lib')\n"
163
+ end
164
+ # unless gem[:no_require]
165
+ SimpleWorker.logger.debug 'writing requires: ' + gem[:require].inspect
166
+ if gem[:require].nil?
167
+ gem[:require] = []
168
+ elsif gem[:require].is_a?(String)
169
+ gem[:require] = [gem[:require]]
170
+ end
171
+ SimpleWorker.logger.debug "gem[:require]: " + gem[:require].inspect
172
+ gem[:require].each do |r|
173
+ SimpleWorker.logger.debug 'adding require to file ' + r.to_s
174
+ f.write "require '#{r}'\n"
175
+ end
176
+ # end
177
+ end
150
178
 
179
+ # load job data
180
+ f.write("
151
181
  # Change to user directory
152
182
  #puts 'dirname=' + dirname.inspect
153
183
  Dir.chdir(dirname)
184
+ # Load in job data
185
+ job_data = JSON.load(File.open(task_data_file))
186
+ puts 'job_data=' + job_data.inspect
187
+ sw_config = job_data['sw_config']
188
+ init_database_connection(sw_config)
189
+ init_mailer(sw_config)
154
190
  ")
155
191
 
156
-
157
192
  f.write("require 'simple_worker'\n")
158
193
 
159
-
160
194
  # add some rails stuff if using Rails
161
195
  if defined?(Rails)
162
196
  f.write "module Rails
@@ -180,25 +214,6 @@ end
180
214
  f.write "ActionMailer::Base.prepend_view_path('templates')\n"
181
215
  end
182
216
  #if SimpleWorker.config.auto_merge
183
- merged_gems.each_pair do |k, gem|
184
- SimpleWorker.logger.debug "Bundling gem #{gem[:name]}..."
185
- if gem[:merge]
186
- f.write "$LOAD_PATH << File.join(File.dirname(__FILE__), '/gems/#{gem[:name]}/lib')\n"
187
- end
188
- # unless gem[:no_require]
189
- SimpleWorker.logger.debug 'writing requires: ' + gem[:require].inspect
190
- if gem[:require].nil?
191
- gem[:require] = []
192
- elsif gem[:require].is_a?(String)
193
- gem[:require] = [gem[:require]]
194
- end
195
- SimpleWorker.logger.debug "gem[:require]: " + gem[:require].inspect
196
- gem[:require].each do |r|
197
- SimpleWorker.logger.debug 'adding require to file ' + r.to_s
198
- f.write "require '#{r}'\n"
199
- end
200
- # end
201
- end
202
217
 
203
218
  if SimpleWorker.config.extra_requires
204
219
  SimpleWorker.config.extra_requires.each do |r|
@@ -224,11 +239,20 @@ end
224
239
  #f.write File.open(filename, 'r') { |mo| mo.read }
225
240
  f.write("require_relative '#{File.basename(filename)}'")
226
241
 
227
- File.open(File.join(File.dirname(__FILE__), "server", 'runner.rb'), 'r') do |fr|
228
- while line = fr.gets
229
- f.write line
230
- end
231
- end
242
+
243
+
244
+ f.write("
245
+ SimpleWorker.disable_queueing()
246
+ runner_class = get_class_to_run(job_data['class_name'])
247
+ SimpleWorker.running_class = runner_class
248
+ runner = init_runner(runner_class, job_data, dirname, task_id)
249
+ init_worker_service_for_runner(job_data)
250
+ SimpleWorker.enable_queueing()
251
+
252
+ # Let's run it!
253
+ runner_return_data = runner.run
254
+ ")
255
+
232
256
  #error handling block - end
233
257
  f.write("\nrescue Exception => ex
234
258
  $stderr.puts '_error_from_sw_'
@@ -343,7 +367,7 @@ end
343
367
  }
344
368
  #puts 'options for upload=' + options.inspect
345
369
  SimpleWorker.logger.info "Uploading now..."
346
- ret = post_file("#{project_url_prefix(get_project_id(options))}workers", File.new(package_file), options)
370
+ ret = post_file("#{project_url_prefix(get_project_id(options))}codes", File.new(package_file), options)
347
371
  SimpleWorker.logger.info "Done uploading."
348
372
  return ret
349
373
  end
@@ -420,7 +444,7 @@ end
420
444
  hash_to_send = data
421
445
  hash_to_send["class_name"] = class_name unless hash_to_send["class_name"]
422
446
  hash_to_send["name"] = class_name unless hash_to_send["name"]
423
- uri = project_url_prefix(get_project_id(options)) + "jobs"
447
+ uri = project_url_prefix(get_project_id(options)) + "tasks"
424
448
  SimpleWorker.logger.debug 'queue_raw , uri = ' + uri
425
449
  ret = post(uri, hash_to_send)
426
450
  ret
@@ -480,9 +504,9 @@ end
480
504
  ret
481
505
  end
482
506
 
483
- def get_workers(options={})
507
+ def get_codes(options={})
484
508
  hash_to_send = {}
485
- uri = "projects/" + get_project_id(options) + "/workers/"
509
+ uri = "projects/" + get_project_id(options) + "/codes/"
486
510
  ret = get(uri, hash_to_send)
487
511
  ret
488
512
  end
@@ -496,7 +520,7 @@ end
496
520
 
497
521
  def get_jobs(options={})
498
522
  hash_to_send = {}
499
- uri = "projects/" + get_project_id(options) + "/jobs/"
523
+ uri = "projects/" + get_project_id(options) + "/tasks/"
500
524
  ret = get(uri, hash_to_send)
501
525
  ret
502
526
  end
@@ -507,14 +531,14 @@ end
507
531
 
508
532
  def log(task_id, options={})
509
533
  data = {}
510
- ret = get("#{project_url_prefix(get_project_id(options))}jobs/#{task_id}/log", data, :parse=>false)
534
+ ret = get("#{project_url_prefix(get_project_id(options))}tasks/#{task_id}/log", data, :parse=>false)
511
535
  ret
512
536
  end
513
537
 
514
538
 
515
539
  def status(task_id, options={})
516
540
  data = {"task_id"=>task_id}
517
- ret = get("#{project_url_prefix(get_project_id(options))}jobs/#{task_id}", data)
541
+ ret = get("#{project_url_prefix(get_project_id(options))}tasks/#{task_id}", data)
518
542
  ret
519
543
  end
520
544
 
@@ -527,7 +551,7 @@ end
527
551
  # data is a hash, should include 'percent' and 'msg'
528
552
  def set_progress(task_id, options={})
529
553
  #data={"data"=>data, "task_id"=>task_id}
530
- post("#{project_url_prefix(get_project_id(options))}jobs/#{task_id}/progress", options)
554
+ post("#{project_url_prefix(get_project_id(options))}tasks/#{task_id}/progress", options)
531
555
  end
532
556
 
533
557
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple_worker
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 2.0.0.beta.7
5
+ version: 2.0.0.beta.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Travis Reeder
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-01 00:00:00 Z
13
+ date: 2011-10-15 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: zip