simple_worker 0.0.1 → 0.0.2
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/lib/http_enabled.rb +2 -2
- data/lib/simple_worker.rb +53 -0
- data/test/simple_worker_tests.rb +16 -9
- data/test/test_runner.rb +28 -0
- metadata +4 -2
data/lib/http_enabled.rb
CHANGED
@@ -15,7 +15,7 @@ end
|
|
15
15
|
|
16
16
|
module SimpleWorker
|
17
17
|
|
18
|
-
DEFAULT_HOST = "http://simpleworker.appoxy.com/api/
|
18
|
+
DEFAULT_HOST = "http://simpleworker.appoxy.com/api/"
|
19
19
|
|
20
20
|
module HttpEnabled
|
21
21
|
|
@@ -49,7 +49,7 @@ module SimpleWorker
|
|
49
49
|
else
|
50
50
|
parameters = {} if parameters.nil?
|
51
51
|
parameters.update(extra_params)
|
52
|
-
puts 'params=' + parameters.inspect
|
52
|
+
# puts 'params=' + parameters.inspect
|
53
53
|
|
54
54
|
end
|
55
55
|
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'http_enabled')
|
2
|
+
require File.join(File.dirname(__FILE__), 'api_auth')
|
3
|
+
|
4
|
+
module SimpleWorker
|
5
|
+
|
6
|
+
class Base < SimpleWorker::ApiAuth
|
7
|
+
|
8
|
+
include SimpleWorker::HttpEnabled
|
9
|
+
|
10
|
+
def initialize(access_key, secret_key)
|
11
|
+
super(access_key, secret_key)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Options:
|
15
|
+
# - :callback_url
|
16
|
+
def upload(filename, class_name, options={})
|
17
|
+
mystring = nil
|
18
|
+
file = File.open(filename, "r") do |f|
|
19
|
+
mystring = f.read
|
20
|
+
end
|
21
|
+
options = {"code"=>mystring, "class_name"=>class_name}
|
22
|
+
response = run_http(@access_key, @secret_key, :post, "code/put", nil, options)
|
23
|
+
puts "response=" + response
|
24
|
+
return ActiveSupport::JSON.decode(response)
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def queue(class_name, data={})
|
29
|
+
|
30
|
+
params = nil
|
31
|
+
if !data.is_a?(Array)
|
32
|
+
data = [data]
|
33
|
+
end
|
34
|
+
hash_to_send = {}
|
35
|
+
hash_to_send["data"] = data
|
36
|
+
hash_to_send["class_name"] = class_name
|
37
|
+
puts 'hash_to_send=' + hash_to_send.inspect
|
38
|
+
response = run_http(@access_key, @secret_key, :put, "queue/add", hash_to_send, params)
|
39
|
+
puts "response=" + response
|
40
|
+
return ActiveSupport::JSON.decode(response)
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def status(task_id)
|
45
|
+
data = {"task_id"=>task_id}
|
46
|
+
#puts run_http(@access_key, @secret_key, :post, "queue/status", nil, {"task_id"=>@task_id})
|
47
|
+
response = run_http(@access_key, @secret_key, :get, "queue/status", nil, data)
|
48
|
+
puts "response=" + response
|
49
|
+
return ActiveSupport::JSON.decode(response)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/test/simple_worker_tests.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'test/unit'
|
2
|
+
require 'yaml'
|
3
|
+
require File.join(File.dirname(__FILE__), '../lib/simple_worker')
|
2
4
|
|
3
5
|
class SimpleWorkerTests < Test::Unit::TestCase
|
4
6
|
|
5
7
|
def setup
|
6
|
-
@config = YAML::load(File.open(File.expand_path("~/.
|
8
|
+
@config = YAML::load(File.open(File.expand_path("~/.test-configs/simple_worker.yml")))
|
7
9
|
#puts @config.inspect
|
8
10
|
@access_key = @config['simple_worker']['access_key']
|
9
11
|
@secret_key = @config['simple_worker']['secret_key']
|
@@ -15,22 +17,27 @@ class SimpleWorkerTests < Test::Unit::TestCase
|
|
15
17
|
|
16
18
|
def test_queue
|
17
19
|
|
20
|
+
worker = SimpleWorker::Base.new(@access_key, @secret_key)
|
21
|
+
|
18
22
|
# Upload latest runner code
|
19
|
-
|
20
|
-
uploader.put("../workers/test_runner.rb", "TestRunner")
|
23
|
+
worker.upload(File.join(File.dirname(__FILE__), "./test_runner.rb"), "TestRunner")
|
21
24
|
|
22
25
|
# Add something to queue, get task ID back
|
23
|
-
queue = SimpleWorker::Queue.new(@access_key, @secret_key)
|
24
26
|
# Single task
|
25
|
-
response_hash = queue
|
27
|
+
response_hash = worker.queue("TestRunner", {"s3_key"=>"single runner", "times"=>10})
|
26
28
|
# task set
|
27
|
-
response_hash = queue
|
29
|
+
response_hash = worker.queue("TestRunner", [{"id"=>"local_id", "s3_key"=>"some key", "times"=>4}, {"s3_key"=>"some key2", "times"=>3}, {"s3_key"=>"some key", "times"=>2}])
|
28
30
|
|
29
31
|
# Check status
|
30
|
-
status = SimpleWorker::Status.new(@access_key, @secret_key)
|
31
32
|
tasks = response_hash["tasks"]
|
32
|
-
tasks.
|
33
|
-
|
33
|
+
while tasks.size > 0
|
34
|
+
tasks.each do |t|
|
35
|
+
status_response = worker.status(t["task_id"])
|
36
|
+
puts 'status for ' + t["task_id"] + ' = ' + status_response["status"]
|
37
|
+
if status_response["status"] == "complete"
|
38
|
+
tasks.delete(t)
|
39
|
+
end
|
40
|
+
end
|
34
41
|
end
|
35
42
|
|
36
43
|
|
data/test/test_runner.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
class TestRunner
|
3
|
+
|
4
|
+
|
5
|
+
def run(data=nil)
|
6
|
+
puts 'running the runner for leroy '.upcase + ' with data: ' + data.inspect
|
7
|
+
@times = data["times"].to_i
|
8
|
+
@times.times do |i|
|
9
|
+
puts 'running at ' + i.to_s
|
10
|
+
sleep 1
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_complete(params=nil)
|
15
|
+
puts 'SET COMPLETE YAY!' + params[:task_set_id]
|
16
|
+
end
|
17
|
+
|
18
|
+
def progress
|
19
|
+
if @count
|
20
|
+
return @count / @times
|
21
|
+
end
|
22
|
+
return 0.0
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
end
|
28
|
+
|
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.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Reeder
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,6 +25,7 @@ files:
|
|
25
25
|
- lib/api_auth.rb
|
26
26
|
- lib/http_enabled.rb
|
27
27
|
- lib/queue.rb
|
28
|
+
- lib/simple_worker.rb
|
28
29
|
- lib/status.rb
|
29
30
|
- lib/uploader.rb
|
30
31
|
- README.markdown
|
@@ -58,3 +59,4 @@ specification_version: 3
|
|
58
59
|
summary: Classified
|
59
60
|
test_files:
|
60
61
|
- test/simple_worker_tests.rb
|
62
|
+
- test/test_runner.rb
|