simple_worker 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  require 'rest_client'
2
- require 'typhoeus'
2
+ require_relative 'uber_client'
3
3
 
4
4
  module SimpleWorker
5
5
 
@@ -56,6 +56,8 @@ module SimpleWorker
56
56
 
57
57
  @base_url = "#{@scheme}://#{@host}:#{@port}/#{@version}"
58
58
 
59
+ @uber_client = Uber::Client.new
60
+
59
61
  end
60
62
 
61
63
 
@@ -125,7 +127,7 @@ module SimpleWorker
125
127
  logger.debug 'get url=' + full_url
126
128
  req_hash = common_req_hash
127
129
  req_hash[:params] = params
128
- response = Typhoeus::Request.get(full_url, req_hash) # could let typhoeus add params, using :params=>x
130
+ response = @uber_client.get(full_url, req_hash) # could let typhoeus add params, using :params=>x
129
131
  #response = @http_sess.request(:get, url_plus_params,
130
132
  # {},
131
133
  # {})
@@ -143,7 +145,7 @@ module SimpleWorker
143
145
  logger.debug "data = " + data
144
146
  logger.debug "params = " + params.inspect
145
147
  logger.debug "options = " + options.inspect
146
- # todo: replace with typhoeus
148
+ # todo: replace with uber_client
147
149
  parse_response(RestClient.post(append_params(url, add_params(method, params)), {:data => data, :file => file}, :content_type => 'application/json'), options)
148
150
  rescue RestClient::Exception => ex
149
151
  process_ex(ex)
@@ -162,7 +164,7 @@ module SimpleWorker
162
164
  logger.debug 'body=' + json
163
165
  req_hash = common_req_hash
164
166
  req_hash[:body] = json
165
- response = Typhoeus::Request.post(url, req_hash)
167
+ response = @uber_client.post(url, req_hash)
166
168
  #response = @http_sess.post(url, json, {"Content-Type" => 'application/json'})
167
169
  check_response(response)
168
170
  logger.debug 'response: ' + response.inspect
@@ -181,7 +183,7 @@ module SimpleWorker
181
183
 
182
184
  def put(method, body, options={})
183
185
  begin
184
- # todo: replace with typhoeus
186
+ # todo: replace with uber_client
185
187
  parse_response RestClient.put(url_full(method), add_params(method, body).to_json, headers), options
186
188
  rescue RestClient::Exception => ex
187
189
  process_ex(ex)
@@ -190,7 +192,7 @@ module SimpleWorker
190
192
 
191
193
  def delete(method, params={}, options={})
192
194
  begin
193
- # todo: replace with typhoeus
195
+ # todo: replace with uber_client
194
196
  parse_response RestClient.delete(append_params(url_full(method), add_params(method, params))), options
195
197
  rescue RestClient::Exception => ex
196
198
  process_ex(ex)
@@ -46,7 +46,7 @@ module SimpleWorker
46
46
  reqs.each do |r|
47
47
  r2 = "#{gem_info[:path]}/lib/#{r}"
48
48
  begin
49
- puts 'requiring ' + r2
49
+ SimpleWorker.logger.debug 'requiring ' + r2
50
50
  require r2
51
51
  rescue LoadError=>ex
52
52
  SimpleWorker.logger.error "Error requiring gem #{r}: #{ex.message}"
@@ -70,7 +70,7 @@ module SimpleWorker
70
70
  f2 = SimpleWorker::MergeHelper.check_for_file(mailer, @caller_file)
71
71
  basename = File.basename(mailer, f2[:extname])
72
72
  path_to_templates = params[:path_to_templates] || File.join(Rails.root, "app/views/#{basename}")
73
- @merged_mailers[basename] = {:name=>basename, :path_to_templates=>path_to_templates, :filename => mailer}.merge!(params)
73
+ @merged_mailers[basename] = {:name=>basename, :path_to_templates=>path_to_templates, :filename => f2[:path]}.merge!(params)
74
74
  end
75
75
 
76
76
  def merge_folder(path)
@@ -11,6 +11,12 @@ module SimpleWorker
11
11
  def running_class
12
12
  @running_class
13
13
  end
14
+ def task_data=(td)
15
+ @task_data = td
16
+ end
17
+ def task_data
18
+ @task_data
19
+ end
14
20
  end
15
21
 
16
22
  def self.disable_queueing()
@@ -194,21 +194,19 @@ Dir.chdir(dirname)
194
194
  job_data = JSON.load(File.open(task_data_file))
195
195
  puts 'job_data=' + job_data.inspect
196
196
  sw_config = job_data['sw_config']
197
- ")
198
-
197
+ SimpleWorker.task_data = job_data
199
198
 
200
- # add some rails stuff if using Rails
201
- if defined?(Rails)
202
- f.write "module Rails
203
- def self.version
204
- '#{Rails.version}'
205
- end
206
- def self.env
207
- '#{Rails.env}'
199
+ if SimpleWorker.task_data['rails']
200
+ module ::Rails
201
+ def self.version
202
+ SimpleWorker.task_data['rails']['version']
203
+ end
204
+ def self.env
205
+ SimpleWorker.task_data['rails']['env']
206
+ end
208
207
  end
209
208
  end
210
- "
211
- end
209
+ ")
212
210
 
213
211
  if SimpleWorker.config.extra_requires
214
212
  SimpleWorker.config.extra_requires.each do |r|
@@ -469,12 +467,12 @@ end
469
467
  ret
470
468
  end
471
469
 
472
- def cancel_schedule(scheduled_task_id)
470
+ def cancel_schedule(scheduled_task_id, options={})
473
471
  raise "Must include a schedule id." if scheduled_task_id.blank?
474
472
  hash_to_send = {}
475
473
  hash_to_send["schedule_id"] = scheduled_task_id
476
- ret = post("scheduler/cancel", hash_to_send)
477
- ret
474
+ uri = "#{project_url_prefix(get_project_id(options))}schedules/#{scheduled_task_id}"
475
+ delete(uri, hash_to_send)
478
476
  end
479
477
 
480
478
  def get_projects()
@@ -0,0 +1,88 @@
1
+ # This is a simple wrapper that can use different http clients depending on what's installed.
2
+ # The purpose of this is so that users who can't install binaries easily (like windoze users) can have fallbacks that work.
3
+
4
+
5
+ module Uber
6
+ def self.gem=(g)
7
+ @gem = g
8
+ end
9
+
10
+ def self.gem
11
+ @gem
12
+ end
13
+
14
+ begin
15
+ require 'typhoeus'
16
+ Uber.gem = :typhoeus
17
+ rescue LoadError => ex
18
+ puts "Could not load typhoeus. #{ex.class.name}: #{ex.message}. Falling back to rest-client. Please install 'typhoeus' gem for best performance."
19
+ require 'rest_client'
20
+ Uber.gem = :rest_client
21
+ end
22
+
23
+ class RestClientResponseWrapper
24
+ def initialize(response)
25
+ @response = response
26
+ end
27
+
28
+ def code
29
+ @response.code
30
+ end
31
+
32
+ def body
33
+ @response.body
34
+ end
35
+
36
+ end
37
+
38
+ class ClientError < StandardError
39
+
40
+ end
41
+
42
+ class RestClientExceptionWrapper < ClientError
43
+ def initialize(ex)
44
+ super(ex.message)
45
+ @ex = ex
46
+ end
47
+ end
48
+
49
+ class Client
50
+
51
+ def initialize
52
+
53
+ end
54
+
55
+ def get(url, req_hash={})
56
+ if Uber.gem == :typhoeus
57
+ response = Typhoeus::Request.get(url, req_hash)
58
+ else
59
+ begin
60
+ headers = req_hash[:headers] || {}
61
+ r2 = RestClient.get(url, req_hash.merge(headers))
62
+ response = RestClientResponseWrapper.new(r2)
63
+ # todo: make generic exception
64
+ rescue RestClient::Exception => ex
65
+ raise RestClientExceptionWrapper.new(ex)
66
+ end
67
+ end
68
+ response
69
+ end
70
+
71
+ def post(url, req_hash={})
72
+ if Uber.gem == :typhoeus
73
+ response = Typhoeus::Request.post(url, req_hash)
74
+ else
75
+ begin
76
+ headers = req_hash[:headers] || {}
77
+ r2 = RestClient.post(url, req_hash[:body], headers)
78
+ response = RestClientResponseWrapper.new(r2)
79
+ # todo: make generic exception
80
+ rescue RestClient::Exception => ex
81
+ raise RestClientExceptionWrapper(ex)
82
+ end
83
+ end
84
+ response
85
+ end
86
+
87
+ end
88
+ end
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: 2.0.2
4
+ version: 2.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-07 00:00:00.000000000 Z
12
+ date: 2011-11-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: zip
16
- requirement: &25817460 !ruby/object:Gem::Requirement
16
+ requirement: &14034160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *25817460
24
+ version_requirements: *14034160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rest-client
27
- requirement: &25816940 !ruby/object:Gem::Requirement
27
+ requirement: &14033500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *25816940
35
+ version_requirements: *14033500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: typhoeus
38
- requirement: &25816440 !ruby/object:Gem::Requirement
38
+ requirement: &14103600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *25816440
46
+ version_requirements: *14103600
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: zip
49
- requirement: &25815920 !ruby/object:Gem::Requirement
49
+ requirement: &14103060 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *25815920
57
+ version_requirements: *14103060
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rest-client
60
- requirement: &25815380 !ruby/object:Gem::Requirement
60
+ requirement: &14102560 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *25815380
68
+ version_requirements: *14102560
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: typhoeus
71
- requirement: &25814840 !ruby/object:Gem::Requirement
71
+ requirement: &14102080 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *25814840
79
+ version_requirements: *14102080
80
80
  description: The official SimpleWorker gem for http://www.simpleworker.com
81
81
  email: travis@appoxy.com
82
82
  executables: []
@@ -94,6 +94,7 @@ files:
94
94
  - lib/simple_worker/server/overrides.rb
95
95
  - lib/simple_worker/server/runner.rb
96
96
  - lib/simple_worker/service.rb
97
+ - lib/simple_worker/uber_client.rb
97
98
  - lib/simple_worker/used_in_worker.rb
98
99
  - lib/simple_worker/utils.rb
99
100
  - rails/init.rb
@@ -118,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
119
  version: '0'
119
120
  requirements: []
120
121
  rubyforge_project:
121
- rubygems_version: 1.8.8
122
+ rubygems_version: 1.8.11
122
123
  signing_key:
123
124
  specification_version: 3
124
125
  summary: The official SimpleWorker gem for http://www.simpleworker.com