simple_worker 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/railtie.rb +20 -13
- data/lib/simple_worker.rb +11 -3
- data/lib/simple_worker/api.rb +153 -0
- data/lib/simple_worker/service.rb +6 -144
- metadata +22 -7
data/lib/railtie.rb
CHANGED
@@ -10,29 +10,36 @@ module SimpleWorker
|
|
10
10
|
initializer "simple_worker.configure_rails_initialization" do |app|
|
11
11
|
puts "Initializing SimpleWorker for Rails 3..."
|
12
12
|
SimpleWorker.configure do |c2|
|
13
|
-
models_path
|
14
|
-
c2.models
|
15
|
-
mailers_path
|
16
|
-
c2.mailers
|
13
|
+
models_path = File.join(Rails.root, 'app/models/*.rb')
|
14
|
+
c2.models = Dir.glob(models_path)
|
15
|
+
mailers_path = File.join(Rails.root, 'app/mailers/*.rb')
|
16
|
+
c2.mailers =Dir.glob(mailers_path).collect { |m| {:filename=>m, :name => File.basename(m), :path_to_templates=>File.join(Rails.root, "app/views/#{File.basename(m, File.extname(m))}")} }
|
17
17
|
c2.extra_requires += ['active_support/core_ext', 'active_record', 'action_mailer']
|
18
|
-
c2.database
|
19
|
-
c2.gems = get_required_gems if Bundler
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
c2.database = Rails.configuration.database_configuration[Rails.env]
|
19
|
+
c2.gems = get_required_gems if defined?(Bundler)
|
20
|
+
SimpleWorker.logger.debug "MODELS " + c2.models.inspect
|
21
|
+
SimpleWorker.logger.debug "MAILERS " + c2.mailers.inspect
|
22
|
+
SimpleWorker.logger.debug "DATABASE " + c2.database.inspect
|
23
|
+
SimpleWorker.logger.debug "GEMS " + c2.gems.inspect
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
def get_required_gems
|
28
28
|
gems_in_gemfile = Bundler.environment.dependencies.select { |d| d.groups.include?(:default) }
|
29
|
-
gems
|
29
|
+
gems =[]
|
30
|
+
specs = Bundler.load.specs
|
31
|
+
puts 'specs=' + specs.inspect
|
30
32
|
gems_in_gemfile.each do |dep|
|
31
33
|
next if dep.name=='rails' #monkey patch
|
32
34
|
gem_info = {:name=>dep.name, :version=>dep.requirement}
|
33
35
|
gem_info.merge!({:require=>dep.autorequire.join}) if dep.autorequire
|
34
|
-
|
35
|
-
|
36
|
+
spec = specs.find { |g| g.name==gem_info[:name] }
|
37
|
+
if spec
|
38
|
+
gem_info[:version] = spec.version.to_s
|
39
|
+
gems << gem_info
|
40
|
+
else
|
41
|
+
SimpleWorker.logger.warn "Could not find gem spec for #{gem_info[:name]}"
|
42
|
+
end
|
36
43
|
end
|
37
44
|
gems
|
38
45
|
end
|
data/lib/simple_worker.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'appoxy_api'
|
2
|
-
#require 'active_support/core_ext'
|
3
1
|
require_relative 'simple_worker/service'
|
4
2
|
require_relative 'simple_worker/base'
|
5
3
|
require_relative 'simple_worker/config'
|
@@ -7,6 +5,9 @@ require_relative 'simple_worker/used_in_worker'
|
|
7
5
|
|
8
6
|
|
9
7
|
module SimpleWorker
|
8
|
+
@@logger = Logger.new(STDOUT)
|
9
|
+
@@logger.level = Logger::INFO
|
10
|
+
|
10
11
|
|
11
12
|
class << self
|
12
13
|
attr_accessor :config,
|
@@ -20,7 +21,14 @@ module SimpleWorker
|
|
20
21
|
def config
|
21
22
|
@config ||= Config.new
|
22
23
|
end
|
23
|
-
|
24
|
+
|
25
|
+
def logger
|
26
|
+
@@logger
|
27
|
+
end
|
28
|
+
|
29
|
+
def api_version
|
30
|
+
3
|
31
|
+
end
|
24
32
|
end
|
25
33
|
|
26
34
|
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
module SimpleWorker
|
2
|
+
module Api
|
3
|
+
|
4
|
+
module Signatures
|
5
|
+
|
6
|
+
|
7
|
+
def self.generate_timestamp(gmtime)
|
8
|
+
return gmtime.strftime("%Y-%m-%dT%H:%M:%SZ")
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def self.generate_signature(operation, timestamp, secret_key)
|
13
|
+
my_sha_hmac = Digest::HMAC.digest(operation + timestamp, secret_key, Digest::SHA1)
|
14
|
+
my_b64_hmac_digest = Base64.encode64(my_sha_hmac).strip
|
15
|
+
return my_b64_hmac_digest
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def self.hash_to_s(hash)
|
20
|
+
str = ""
|
21
|
+
hash.sort.each { |a| str+= "#{a[0]}#{a[1]}" }
|
22
|
+
#removing all characters that could differ after parsing with rails
|
23
|
+
return str.delete "\"\/:{}[]\' T"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Subclass must define:
|
28
|
+
# host: endpoint url for service
|
29
|
+
class Client
|
30
|
+
|
31
|
+
@@logger = Logger.new(STDOUT)
|
32
|
+
@@logger.level = Logger::INFO
|
33
|
+
|
34
|
+
def self.logger
|
35
|
+
@@logger
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_accessor :host, :access_key, :secret_key, :version
|
39
|
+
|
40
|
+
def initialize(host, access_key, secret_key, options={})
|
41
|
+
@host = host
|
42
|
+
@access_key = access_key
|
43
|
+
@secret_key = secret_key
|
44
|
+
end
|
45
|
+
|
46
|
+
def process_ex(ex)
|
47
|
+
body = ex.http_body
|
48
|
+
puts 'EX BODY=' + body.to_s
|
49
|
+
decoded_ex = JSON.parse(ex.http_body)
|
50
|
+
exception = Exception.new(ex.message+":"+decoded_ex["msg"])
|
51
|
+
exception.set_backtrace(decoded_ex["backtrace"].split(",")) if decoded_ex["backtrace"]
|
52
|
+
raise exception
|
53
|
+
end
|
54
|
+
|
55
|
+
def get(method, params={}, options={})
|
56
|
+
begin
|
57
|
+
# ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params)
|
58
|
+
parse_response RestClient.get(append_params(url(method), add_params(method, params)), headers), options
|
59
|
+
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
60
|
+
process_ex(ex)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def post_file(method, file, params={}, options={})
|
65
|
+
begin
|
66
|
+
parse_response RestClient.post(url(method), add_params(method, params).merge!({:file=>file}), :multipart => true), options
|
67
|
+
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
68
|
+
process_ex(ex)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def post(method, params={}, options={})
|
73
|
+
begin
|
74
|
+
parse_response RestClient.post(url(method), add_params(method, params).to_json, headers), options
|
75
|
+
#ClientHelper.run_http(host, access_key, secret_key, :post, method, nil, params)
|
76
|
+
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
77
|
+
process_ex(ex)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
def put(method, body, options={})
|
83
|
+
begin
|
84
|
+
parse_response RestClient.put(url(method), add_params(method, body).to_json, headers), options
|
85
|
+
#ClientHelper.run_http(host, access_key, secret_key, :put, method, body, nil)
|
86
|
+
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
87
|
+
process_ex(ex)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def delete(method, params={}, options={})
|
92
|
+
begin
|
93
|
+
parse_response RestClient.delete(append_params(url(method), add_params(method, params))), options
|
94
|
+
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
95
|
+
process_ex(ex)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def url(command_path)
|
100
|
+
url = host + command_path
|
101
|
+
url
|
102
|
+
end
|
103
|
+
|
104
|
+
def add_params(command_path, hash)
|
105
|
+
v = version||"0.1"
|
106
|
+
ts = SimpleWorker::Api::Signatures.generate_timestamp(Time.now.gmtime)
|
107
|
+
# puts 'timestamp = ' + ts
|
108
|
+
sig = case v
|
109
|
+
when "0.2"
|
110
|
+
SimpleWorker::Api::Signatures.generate_signature(command_path + SimpleWorker::Api::Signatures.hash_to_s(hash), ts, secret_key)
|
111
|
+
when "0.1"
|
112
|
+
SimpleWorker::Api::Signatures.generate_signature(command_path, ts, secret_key)
|
113
|
+
end
|
114
|
+
|
115
|
+
extra_params = {'sigv'=>v, 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
|
116
|
+
hash.merge!(extra_params)
|
117
|
+
end
|
118
|
+
|
119
|
+
def append_params(host, params)
|
120
|
+
host += "?"
|
121
|
+
i = 0
|
122
|
+
params.each_pair do |k, v|
|
123
|
+
host += "&" if i > 0
|
124
|
+
host += k + "=" + CGI.escape(v)
|
125
|
+
i +=1
|
126
|
+
end
|
127
|
+
return host
|
128
|
+
end
|
129
|
+
|
130
|
+
def headers
|
131
|
+
user_agent = "SimpleWorker Ruby Client"
|
132
|
+
headers = {'User-Agent' => user_agent}
|
133
|
+
end
|
134
|
+
|
135
|
+
def parse_response(response, options={})
|
136
|
+
# puts 'PARSE RESPONSE!'
|
137
|
+
unless options[:parse] == false
|
138
|
+
begin
|
139
|
+
return JSON.parse(response.to_s)
|
140
|
+
rescue => ex
|
141
|
+
puts 'response that caused error = ' + response.to_s
|
142
|
+
raise ex
|
143
|
+
end
|
144
|
+
else
|
145
|
+
response
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
@@ -1,152 +1,14 @@
|
|
1
1
|
require 'base64'
|
2
2
|
require 'logger'
|
3
|
-
require 'appoxy_api'
|
4
3
|
require 'zip'
|
5
4
|
require 'rest_client'
|
6
5
|
require 'json'
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
@@logger = Logger.new(STDOUT)
|
11
|
-
@@logger.level = Logger::INFO
|
12
|
-
|
13
|
-
def self.logger
|
14
|
-
@@logger
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.api_version
|
18
|
-
3
|
19
|
-
end
|
20
|
-
|
21
|
-
module Api
|
22
|
-
|
23
|
-
# Subclass must define:
|
24
|
-
# host: endpoint url for service
|
25
|
-
class Client
|
26
|
-
|
27
|
-
@@logger = Logger.new(STDOUT)
|
28
|
-
@@logger.level = Logger::INFO
|
29
|
-
|
30
|
-
def self.logger
|
31
|
-
@@logger
|
32
|
-
end
|
33
|
-
|
34
|
-
attr_accessor :host, :access_key, :secret_key, :version
|
35
|
-
|
36
|
-
def initialize(host, access_key, secret_key, options={})
|
37
|
-
@host = host
|
38
|
-
@access_key = access_key
|
39
|
-
@secret_key = secret_key
|
40
|
-
end
|
41
|
-
|
42
|
-
def process_ex(ex)
|
43
|
-
body = ex.http_body
|
44
|
-
puts 'EX BODY=' + body.to_s
|
45
|
-
decoded_ex = JSON.parse(ex.http_body)
|
46
|
-
exception = Exception.new(ex.message+":"+decoded_ex["msg"])
|
47
|
-
exception.set_backtrace(decoded_ex["backtrace"].split(",")) if decoded_ex["backtrace"]
|
48
|
-
raise exception
|
49
|
-
end
|
50
|
-
|
51
|
-
def get(method, params={}, options={})
|
52
|
-
begin
|
53
|
-
# ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params)
|
54
|
-
parse_response RestClient.get(append_params(url(method), add_params(method, params)), headers), options
|
55
|
-
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
56
|
-
process_ex(ex)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def post_file(method, file, params={}, options={})
|
61
|
-
begin
|
62
|
-
parse_response RestClient.post(url(method), add_params(method, params).merge!({:file=>file}), :multipart => true), options
|
63
|
-
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
64
|
-
process_ex(ex)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def post(method, params={}, options={})
|
69
|
-
begin
|
70
|
-
parse_response RestClient.post(url(method), add_params(method, params).to_json, headers), options
|
71
|
-
#ClientHelper.run_http(host, access_key, secret_key, :post, method, nil, params)
|
72
|
-
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
73
|
-
process_ex(ex)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
7
|
+
require_relative 'api'
|
77
8
|
|
78
|
-
|
79
|
-
begin
|
80
|
-
parse_response RestClient.put(url(method), add_params(method, body).to_json, headers), options
|
81
|
-
#ClientHelper.run_http(host, access_key, secret_key, :put, method, body, nil)
|
82
|
-
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
83
|
-
process_ex(ex)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def delete(method, params={}, options={})
|
88
|
-
begin
|
89
|
-
parse_response RestClient.delete(append_params(url(method), add_params(method, params))), options
|
90
|
-
rescue RestClient::BadRequest, RestClient::InternalServerError => ex
|
91
|
-
process_ex(ex)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def url(command_path)
|
96
|
-
url = host + command_path
|
97
|
-
url
|
98
|
-
end
|
99
|
-
|
100
|
-
def add_params(command_path, hash)
|
101
|
-
v = version||"0.1"
|
102
|
-
ts = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
|
103
|
-
# puts 'timestamp = ' + ts
|
104
|
-
sig = case v
|
105
|
-
when "0.2"
|
106
|
-
Appoxy::Api::Signatures.generate_signature(command_path + Appoxy::Api::Signatures.hash_to_s(hash), ts, secret_key)
|
107
|
-
when "0.1"
|
108
|
-
Appoxy::Api::Signatures.generate_signature(command_path, ts, secret_key)
|
109
|
-
end
|
110
|
-
|
111
|
-
extra_params = {'sigv'=>v, 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
|
112
|
-
hash.merge!(extra_params)
|
113
|
-
end
|
114
|
-
|
115
|
-
def append_params(host, params)
|
116
|
-
host += "?"
|
117
|
-
i = 0
|
118
|
-
params.each_pair do |k, v|
|
119
|
-
host += "&" if i > 0
|
120
|
-
host += k + "=" + CGI.escape(v)
|
121
|
-
i +=1
|
122
|
-
end
|
123
|
-
return host
|
124
|
-
end
|
125
|
-
|
126
|
-
def headers
|
127
|
-
user_agent = "Appoxy API Ruby Client"
|
128
|
-
headers = {'User-Agent' => user_agent}
|
129
|
-
end
|
130
|
-
|
131
|
-
def parse_response(response, options={})
|
132
|
-
puts 'PARSE RESPONSE!'
|
133
|
-
unless options[:parse] == false
|
134
|
-
begin
|
135
|
-
return JSON.parse(response.to_s)
|
136
|
-
rescue => ex
|
137
|
-
puts 'response that caused error = ' + response.to_s
|
138
|
-
raise ex
|
139
|
-
end
|
140
|
-
else
|
141
|
-
response
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
9
|
+
module SimpleWorker
|
148
10
|
|
149
|
-
class Service <
|
11
|
+
class Service < SimpleWorker::Api::Client
|
150
12
|
|
151
13
|
attr_accessor :config
|
152
14
|
|
@@ -223,7 +85,7 @@ module SimpleWorker
|
|
223
85
|
gem_name =(gem_info[:require] || gem_info[:name].match(/^[a-zA-Z0-9\-_]+/)[0])
|
224
86
|
puts "Searching for #{gem_name}..."
|
225
87
|
searcher = Gem::GemPathSearcher.new
|
226
|
-
gems
|
88
|
+
gems = searcher.find_all(gem_name)
|
227
89
|
# gems = searcher.init_gemspecs.select { |gem| gem.name==gem_name }
|
228
90
|
puts 'gems found=' + gems.inspect
|
229
91
|
gems = gems.select { |g| g.version.version==gem_info[:version] } if gem_info[:version]
|
@@ -312,7 +174,7 @@ module SimpleWorker
|
|
312
174
|
f.add(File.basename(m), m)
|
313
175
|
end
|
314
176
|
if merged_mailers && merged_mailers.size > 0
|
315
|
-
|
177
|
+
# puts " MERGED MAILERS" + merged_mailers.inspect
|
316
178
|
merged_mailers.each do |mailer|
|
317
179
|
SimpleWorker.logger.debug "Collecting mailer #{mailer[:name]}"
|
318
180
|
f.add(File.basename(mailer[:filename]), mailer[:filename])
|
@@ -435,4 +297,4 @@ module SimpleWorker
|
|
435
297
|
|
436
298
|
end
|
437
299
|
|
438
|
-
end
|
300
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_worker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 6
|
8
|
+
- 2
|
9
|
+
version: 0.6.2
|
6
10
|
platform: ruby
|
7
11
|
authors:
|
8
12
|
- Travis Reeder
|
@@ -10,27 +14,32 @@ autorequire:
|
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
16
|
|
13
|
-
date: 2011-05-
|
17
|
+
date: 2011-05-29 00:00:00 -07:00
|
18
|
+
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
21
|
+
name: zip
|
17
22
|
prerelease: false
|
18
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
19
24
|
none: false
|
20
25
|
requirements:
|
21
26
|
- - ">="
|
22
27
|
- !ruby/object:Gem::Version
|
23
|
-
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
24
31
|
type: :runtime
|
25
32
|
version_requirements: *id001
|
26
33
|
- !ruby/object:Gem::Dependency
|
27
|
-
name:
|
34
|
+
name: rest-client
|
28
35
|
prerelease: false
|
29
36
|
requirement: &id002 !ruby/object:Gem::Requirement
|
30
37
|
none: false
|
31
38
|
requirements:
|
32
39
|
- - ">="
|
33
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
34
43
|
version: "0"
|
35
44
|
type: :runtime
|
36
45
|
version_requirements: *id002
|
@@ -46,12 +55,14 @@ files:
|
|
46
55
|
- lib/rails2_init.rb
|
47
56
|
- lib/railtie.rb
|
48
57
|
- lib/simple_worker.rb
|
58
|
+
- lib/simple_worker/api.rb
|
49
59
|
- lib/simple_worker/base.rb
|
50
60
|
- lib/simple_worker/config.rb
|
51
61
|
- lib/simple_worker/service.rb
|
52
62
|
- lib/simple_worker/used_in_worker.rb
|
53
63
|
- rails/init.rb
|
54
64
|
- README.markdown
|
65
|
+
has_rdoc: true
|
55
66
|
homepage: http://github.com/appoxy/simple_worker
|
56
67
|
licenses: []
|
57
68
|
|
@@ -65,17 +76,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
76
|
requirements:
|
66
77
|
- - ">="
|
67
78
|
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
68
81
|
version: "0"
|
69
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
83
|
none: false
|
71
84
|
requirements:
|
72
85
|
- - ">="
|
73
86
|
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
74
89
|
version: "0"
|
75
90
|
requirements: []
|
76
91
|
|
77
92
|
rubyforge_project:
|
78
|
-
rubygems_version: 1.7
|
93
|
+
rubygems_version: 1.3.7
|
79
94
|
signing_key:
|
80
95
|
specification_version: 3
|
81
96
|
summary: The official SimpleWorker gem for http://www.simpleworker.com
|