simple_worker 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +14 -0
- data/lib/api_auth.rb +11 -0
- data/lib/http_enabled.rb +121 -0
- data/lib/queue.rb +33 -0
- data/lib/status.rb +22 -0
- data/lib/uploader.rb +28 -0
- data/test/simple_worker_tests.rb +39 -0
- metadata +60 -0
data/README.markdown
ADDED
data/lib/api_auth.rb
ADDED
data/lib/http_enabled.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'net/http'
|
4
|
+
require 'base64'
|
5
|
+
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'digest/hmac'
|
9
|
+
USE_EMBEDDED_HMAC = false
|
10
|
+
rescue
|
11
|
+
puts "HMAC, not found in standard lib." + $!.message
|
12
|
+
require 'hmac-sha1'
|
13
|
+
USE_EMBEDDED_HMAC = true
|
14
|
+
end
|
15
|
+
|
16
|
+
module SimpleWorker
|
17
|
+
|
18
|
+
DEFAULT_HOST = "http://simpleworker.appoxy.com/api/v1/"
|
19
|
+
|
20
|
+
module HttpEnabled
|
21
|
+
|
22
|
+
|
23
|
+
def self.host
|
24
|
+
return DEFAULT_HOST
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
# body is a hash
|
29
|
+
def run_http(access_key, secret_key, http_method, command_path, body=nil, parameters={}, extra_headers=nil)
|
30
|
+
ts = generate_timestamp(Time.now.gmtime)
|
31
|
+
# puts 'timestamp = ' + ts
|
32
|
+
sig = generate_signature_v0(command_path, ts, secret_key)
|
33
|
+
# puts "My signature = " + sig
|
34
|
+
url = SimpleWorker::HttpEnabled.host + command_path
|
35
|
+
# puts url
|
36
|
+
|
37
|
+
user_agent = "Ruby Client"
|
38
|
+
headers = {'User-Agent' => user_agent}
|
39
|
+
|
40
|
+
if !extra_headers.nil?
|
41
|
+
extra_headers.each_pair do |k, v|
|
42
|
+
headers[k] = v
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
extra_params = {'sigv'=>"0.1", 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
|
47
|
+
if http_method == :put
|
48
|
+
body.update(extra_params)
|
49
|
+
else
|
50
|
+
parameters = {} if parameters.nil?
|
51
|
+
parameters.update(extra_params)
|
52
|
+
puts 'params=' + parameters.inspect
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
uri = URI.parse(url)
|
58
|
+
#puts 'body=' + body.to_s
|
59
|
+
if (http_method == :put)
|
60
|
+
req = Net::HTTP::Put.new(uri.path)
|
61
|
+
body = ActiveSupport::JSON.encode(body)
|
62
|
+
req.body = body unless body.nil?
|
63
|
+
elsif (http_method == :post)
|
64
|
+
req = Net::HTTP::Post.new(uri.path)
|
65
|
+
if !parameters.nil?
|
66
|
+
req.set_form_data(parameters)
|
67
|
+
else
|
68
|
+
req.body = body unless body.nil?
|
69
|
+
end
|
70
|
+
elsif (http_method == :delete)
|
71
|
+
req = Net::HTTP::Delete.new(uri.path)
|
72
|
+
if !parameters.nil?
|
73
|
+
req.set_form_data(parameters)
|
74
|
+
end
|
75
|
+
else
|
76
|
+
req = Net::HTTP::Get.new(uri.path)
|
77
|
+
if !parameters.nil?
|
78
|
+
req.set_form_data(parameters)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
headers.each_pair do |k, v|
|
82
|
+
req[k] = v
|
83
|
+
end
|
84
|
+
# req.each_header do |k, v|
|
85
|
+
# puts 'header ' + k + '=' + v
|
86
|
+
#end
|
87
|
+
res = Net::HTTP.start(uri.host, uri.port) do |http|
|
88
|
+
http.request(req)
|
89
|
+
end
|
90
|
+
|
91
|
+
ret = ''
|
92
|
+
case res
|
93
|
+
when Net::HTTPSuccess
|
94
|
+
# puts 'response body=' + res.body
|
95
|
+
ret = res.body
|
96
|
+
else
|
97
|
+
#res.error
|
98
|
+
puts 'HTTP ERROR: ' + res.class.name
|
99
|
+
puts res.body
|
100
|
+
ret = res.body
|
101
|
+
end
|
102
|
+
return ret
|
103
|
+
end
|
104
|
+
|
105
|
+
def generate_timestamp(gmtime)
|
106
|
+
return gmtime.strftime("%Y-%m-%dT%H:%M:%SZ")
|
107
|
+
end
|
108
|
+
|
109
|
+
def generate_signature_v0(operation, timestamp, secret_access_key)
|
110
|
+
if USE_EMBEDDED_HMAC
|
111
|
+
my_sha_hmac = HMAC::SHA1.digest(secret_access_key, operation + timestamp)
|
112
|
+
else
|
113
|
+
my_sha_hmac = Digest::HMAC.digest(operation + timestamp, secret_access_key, Digest::SHA1)
|
114
|
+
end
|
115
|
+
my_b64_hmac_digest = Base64.encode64(my_sha_hmac).strip
|
116
|
+
return my_b64_hmac_digest
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
data/lib/queue.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'http_enabled'
|
2
|
+
require 'api_auth'
|
3
|
+
|
4
|
+
module SimpleWorker
|
5
|
+
class Queue < SimpleWorker::ApiAuth
|
6
|
+
|
7
|
+
extend SimpleWorker::HttpEnabled
|
8
|
+
|
9
|
+
def initialize(access_key, secret_key, class_name)
|
10
|
+
super(access_key, secret_key)
|
11
|
+
@class_name = class_name
|
12
|
+
end
|
13
|
+
|
14
|
+
def add(class_name, data={})
|
15
|
+
|
16
|
+
params = nil
|
17
|
+
if !data.is_a?(Array)
|
18
|
+
data = [data]
|
19
|
+
end
|
20
|
+
hash_to_send = {}
|
21
|
+
hash_to_send["data"] = data
|
22
|
+
hash_to_send["class_name"] = class_name
|
23
|
+
puts 'hash_to_send=' + hash_to_send.inspect
|
24
|
+
res = run_http(access_key, secret_key, :put, "queue/add", hash_to_send, params)
|
25
|
+
puts "response=" + res
|
26
|
+
return ActiveSupport::JSON.decode(res)
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
data/lib/status.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'http_enabled'
|
2
|
+
require 'api_auth'
|
3
|
+
|
4
|
+
module SimpleWorker
|
5
|
+
class Status < SimpleWorker::ApiAuth
|
6
|
+
|
7
|
+
extend SimpleWorker::HttpEnabled
|
8
|
+
|
9
|
+
def initialize(access_key, secret_key, class_name)
|
10
|
+
super(access_key, secret_key)
|
11
|
+
@class_name = class_name
|
12
|
+
end
|
13
|
+
|
14
|
+
def check(task_id)
|
15
|
+
data = {"task_id"=>task_id}
|
16
|
+
#puts run_http(@access_key, @secret_key, :post, "queue/status", nil, {"task_id"=>@task_id})
|
17
|
+
puts "response=" + run_http(@access_key, @secret_key, :get, "queue/status", nil, data)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end
|
data/lib/uploader.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'http_enabled'
|
2
|
+
require 'api_auth'
|
3
|
+
|
4
|
+
module SimpleWorker
|
5
|
+
class Uploader < SimpleWorker::ApiAuth
|
6
|
+
|
7
|
+
extend SimpleWorker::HttpEnabled
|
8
|
+
|
9
|
+
def initialize(access_key, secret_key)
|
10
|
+
super(access_key, secret_key)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Options:
|
14
|
+
# - :callback_url
|
15
|
+
def put(filename, class_name, data={})
|
16
|
+
mystring = nil
|
17
|
+
file = File.open(filename, "r") do |f|
|
18
|
+
mystring = f.read
|
19
|
+
end
|
20
|
+
data = {"code"=>mystring, "class_name"=>class_name}
|
21
|
+
puts "response=" + run_http(@access_key, @secret_key, :post, "code/put", nil, data)
|
22
|
+
#puts "response=" + run_http(access_key, secret_key, :post, "queue/add", nil, data)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
class SimpleWorkerTests < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@config = YAML::load(File.open(File.expand_path("~/.testconfigs/simple_worker.yml")))
|
7
|
+
#puts @config.inspect
|
8
|
+
@access_key = @config['simple_worker']['access_key']
|
9
|
+
@secret_key = @config['simple_worker']['secret_key']
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_queue
|
17
|
+
|
18
|
+
# Upload latest runner code
|
19
|
+
uploader = SimpleWorker::Uploader.new(@access_key, @secret_key)
|
20
|
+
uploader.put("../workers/test_runner.rb", "TestRunner")
|
21
|
+
|
22
|
+
# Add something to queue, get task ID back
|
23
|
+
queue = SimpleWorker::Queue.new(@access_key, @secret_key)
|
24
|
+
# Single task
|
25
|
+
response_hash = queue.add("TestRunner", {"s3_key"=>"single runner", "times"=>10})
|
26
|
+
# task set
|
27
|
+
response_hash = queue.add("TestRunner", [{"id"=>"local_id", "s3_key"=>"some key", "times"=>4}, {"s3_key"=>"some key2", "times"=>3}, {"s3_key"=>"some key", "times"=>2}])
|
28
|
+
|
29
|
+
# Check status
|
30
|
+
status = SimpleWorker::Status.new(@access_key, @secret_key)
|
31
|
+
tasks = response_hash["tasks"]
|
32
|
+
tasks.each do |t|
|
33
|
+
status.check(t["task_id"])
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_worker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Travis Reeder
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-11 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: I could tell you, but then I'd have to...
|
17
|
+
email: travis@appoxy.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- lib/api_auth.rb
|
26
|
+
- lib/http_enabled.rb
|
27
|
+
- lib/queue.rb
|
28
|
+
- lib/status.rb
|
29
|
+
- lib/uploader.rb
|
30
|
+
- README.markdown
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/appoxy/simple_worker
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.3.5
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Classified
|
59
|
+
test_files:
|
60
|
+
- test/simple_worker_tests.rb
|