simple_worker 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_worker.rb CHANGED
@@ -1,18 +1,14 @@
1
- require File.join(File.dirname(__FILE__), 'http_enabled')
2
- require File.join(File.dirname(__FILE__), 'api_auth')
1
+ require 'appoxy_api'
3
2
  require File.join(File.dirname(__FILE__), 'worker')
4
- require File.join(File.dirname(__FILE__), 'simple_worker_error')
5
3
 
6
4
 
7
5
  module SimpleWorker
8
6
 
9
- class Service < SimpleWorker::ApiAuth
10
-
11
- include SimpleWorker::HttpEnabled
7
+ class Service < Appoxy::Api::Client
12
8
 
13
9
  def initialize(access_key, secret_key, options={})
14
10
  puts 'Starting SimpleWorker::Service...'
15
- super(access_key, secret_key, options)
11
+ super("http://api.simpleworkr.com/api/", access_key, secret_key, options)
16
12
  end
17
13
 
18
14
  # Options:
@@ -23,26 +19,22 @@ module SimpleWorker
23
19
  mystring = f.read
24
20
  end
25
21
  options = {"code"=>mystring, "class_name"=>class_name}
26
- response = run_http(@host, @access_key, @secret_key, :post, "code/put", nil, options)
27
- # puts "response=" + response
28
- parse_response response
22
+ ret = post("code/put", options)
23
+ ret
29
24
  end
30
25
 
31
26
  #
32
27
  # data:
33
28
  def queue(class_name, data={})
34
-
35
- params = nil
36
29
  if !data.is_a?(Array)
37
30
  data = [data]
38
31
  end
39
32
  hash_to_send = {}
40
- hash_to_send["data"] = data
33
+ hash_to_send["payload"] = data
41
34
  hash_to_send["class_name"] = class_name
42
35
  if defined?(RAILS_ENV)
43
36
  hash_to_send["rails_env"] = RAILS_ENV
44
37
  end
45
- # puts 'hash_to_send=' + hash_to_send.inspect
46
38
  return queue_raw(class_name, hash_to_send)
47
39
 
48
40
  end
@@ -50,21 +42,10 @@ module SimpleWorker
50
42
  def queue_raw(class_name, data={})
51
43
  params = nil
52
44
  hash_to_send = data
53
- # puts 'hash_to_send=' + hash_to_send.inspect
54
- response = run_http(@host, @access_key, @secret_key, :put, "queue/add", hash_to_send, params)
55
- # puts "response=" + response
56
-
57
- parse_response response
58
-
59
- end
45
+ hash_to_send["class_name"] = class_name
46
+ ret = post("queue/add", hash_to_send)
47
+ ret
60
48
 
61
- def parse_response(response)
62
- begin
63
- return ActiveSupport::JSON.decode(response)
64
- rescue => ex
65
- puts 'response that caused error = ' + response.to_s
66
- raise ex
67
- end
68
49
  end
69
50
 
70
51
 
@@ -80,52 +61,43 @@ module SimpleWorker
80
61
  #
81
62
  def schedule(class_name, data, schedule)
82
63
  raise "Schedule must be a hash." if !schedule.is_a? Hash
83
- params = nil
84
- if !data.is_a?(Array)
85
- data = [data]
86
- end
64
+ # if !data.is_a?(Array)
65
+ # data = [data]
66
+ # end
87
67
  hash_to_send = {}
88
- hash_to_send["data"] = data
68
+ hash_to_send["payload"] = data
89
69
  hash_to_send["class_name"] = class_name
90
70
  hash_to_send["schedule"] = schedule
91
- # puts 'hash_to_send=' + hash_to_send.inspect
92
- response = run_http(@host, @access_key, @secret_key, :put, "queue/schedule", hash_to_send, params)
93
- # puts "response=" + response
94
- parse_response response
71
+ puts 'about to send ' + hash_to_send.inspect
72
+ ret = post("scheduler/schedule", hash_to_send)
73
+ ret
95
74
  end
96
75
 
97
- #
98
- #
99
- #
100
- def get_scheduled_tasks(schedule_id)
101
- params = nil
102
- if !data.is_a?(Array)
103
- data = [data]
104
- end
76
+ def cancel_schedule(scheduled_task_id)
77
+ raise "Must include a schedule id." if scheduled_task_id.blank?
105
78
  hash_to_send = {}
106
- hash_to_send["data"] = data
107
- hash_to_send["class_name"] = class_name
108
- # puts 'hash_to_send=' + hash_to_send.inspect
109
- response = run_http(@host, @access_key, @secret_key, :put, "queue/add", hash_to_send, params)
110
- # puts "response=" + response
111
- parse_response response
79
+ hash_to_send["scheduled_task_id"] = scheduled_task_id
80
+ ret = post("scheduler/cancel", hash_to_send)
81
+ ret
82
+ end
83
+
84
+ def get_schedules()
85
+ hash_to_send = {}
86
+ ret = get("scheduler/list", hash_to_send)
87
+ ret
112
88
  end
113
89
 
114
90
  def status(task_id)
115
91
  data = {"task_id"=>task_id}
116
- #puts run_http(@access_key, @secret_key, :post, "queue/status", nil, {"task_id"=>@task_id})
117
- response = run_http(@host, @access_key, @secret_key, :get, "task/status", nil, data)
118
- # puts "response=" + response
119
- parse_response response
92
+ ret = get("task/status", data)
93
+ ret
120
94
  end
121
95
 
122
96
  def log(task_id)
123
97
  data = {"task_id"=>task_id}
124
- #puts run_http(@access_key, @secret_key, :post, "queue/status", nil, {"task_id"=>@task_id})
125
- response = run_http(@host, @access_key, @secret_key, :get, "task/log", nil, data)
126
- # puts "response=" + response
127
- ret = parse_response response
128
- ret["log"] = Base64.decode64(ret["log"])
98
+ ret = get("task/log", data)
99
+ puts 'ret=' + ret.inspect
100
+ # ret["log"] = Base64.decode64(ret["log"])
129
101
  ret
130
102
  end
131
103
 
@@ -3,7 +3,8 @@ require 'simple_worker'
3
3
  class ScheduledWorker < SimpleWorker::Base
4
4
 
5
5
  def scheduled_run(data=nil)
6
-
6
+ log "This is scheduled yes it is"
7
+ log data.inspect
7
8
  end
8
9
 
9
10
  end
@@ -22,16 +22,16 @@ class SimpleWorkerTests < Test::Unit::TestCase
22
22
 
23
23
 
24
24
  # Upload latest runner code
25
- @worker.upload(File.join(File.dirname(__FILE__), "./test_runner.rb"), "TestWorker")
25
+ @worker.upload(File.join(File.dirname(__FILE__), "./test_worker.rb"), "TestWorker")
26
26
 
27
27
  # Add something to queue, get task ID back
28
28
  # Single task
29
- response_hash_single = @worker.queue("TestRunner", {"s3_key"=>"single runner", "times"=>10})
29
+ response_hash_single = @worker.queue("TestWorker", {"s3_key"=>"single runner", "times"=>10})
30
30
 
31
31
  # task set
32
- 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}])
33
- #
34
- # Check status
32
+ response_hash = @worker.queue("TestWorker", [{"id"=>"local_id", "s3_key"=>"some key", "times"=>4}, {"s3_key"=>"some key2", "times"=>3}, {"s3_key"=>"some key", "times"=>2}])
33
+
34
+ # # Check status
35
35
  tasks = response_hash["tasks"]
36
36
  while tasks.size > 0
37
37
  tasks.each do |t|
@@ -46,28 +46,29 @@ class SimpleWorkerTests < Test::Unit::TestCase
46
46
  # lets try to get the log now too
47
47
  task_id = response_hash_single["tasks"][0]["task_id"]
48
48
  puts 'task_id=' + task_id
49
- puts 'log=' + @worker.log(task_id).inspect
49
+ status_with_log = @worker.log(task_id)
50
+ puts 'log=' + status_with_log.inspect
50
51
 
51
52
  end
52
53
 
53
54
  def test_scheduled
54
55
 
55
56
  # Upload latest runner code
56
- @worker.upload(File.join(File.dirname(__FILE__), "./scheduled_runner.rb"), "ScheduledWorker")
57
+ @worker.upload(File.join(File.dirname(__FILE__), "./scheduled_worker.rb"), "ScheduledWorker")
57
58
 
58
59
  start_at = 10.seconds.since
59
60
  #start_at = start_at.gmtime # testing different timezone
60
61
  puts 'start_at =' + start_at.inspect
61
- response_hash = @worker.schedule("TestRunner", {"msg"=>"One time test."}, {:start_at=>start_at})
62
+ response_hash = @worker.schedule("ScheduledWorker", {"msg"=>"One time test."}, {:start_at=>start_at})
62
63
  puts 'response_hash=' + response_hash.inspect
63
64
 
64
65
  start_at = 10.seconds.since
65
- response_hash = @worker.schedule("TestRunner", {"msg"=>"Run times test"}, {:start_at=>start_at, :run_every=>30, :run_times=>3})
66
+ response_hash = @worker.schedule("ScheduledWorker", {"msg"=>"Run times test"}, {:start_at=>start_at, :run_every=>30, :run_times=>3})
66
67
  puts 'response_hash=' + response_hash.inspect
67
68
 
68
69
  start_at = 10.seconds.since
69
70
  end_at = 2.minutes.since
70
- response_hash = @worker.schedule("TestRunner", {"msg"=>"End at test"}, {:start_at=>start_at, :run_every=>30, :end_at=>end_at, :run_times=>20})
71
+ response_hash = @worker.schedule("ScheduledWorker", {"msg"=>"End at test"}, {:start_at=>start_at, :run_every=>30, :end_at=>end_at, :run_times=>20})
71
72
  puts 'response_hash=' + response_hash.inspect
72
73
 
73
74
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - Travis Reeder
@@ -9,10 +14,21 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-12-13 00:00:00 -08:00
17
+ date: 2010-04-16 00:00:00 -07:00
13
18
  default_executable:
14
- dependencies: []
15
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: appoxy_api
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
16
32
  description: I could tell you, but then I'd have to...
17
33
  email: travis@appoxy.com
18
34
  executables: []
@@ -22,10 +38,7 @@ extensions: []
22
38
  extra_rdoc_files:
23
39
  - README.markdown
24
40
  files:
25
- - lib/api_auth.rb
26
- - lib/http_enabled.rb
27
41
  - lib/simple_worker.rb
28
- - lib/simple_worker_error.rb
29
42
  - lib/worker.rb
30
43
  - README.markdown
31
44
  has_rdoc: true
@@ -41,22 +54,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
54
  requirements:
42
55
  - - ">="
43
56
  - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
44
59
  version: "0"
45
- version:
46
60
  required_rubygems_version: !ruby/object:Gem::Requirement
47
61
  requirements:
48
62
  - - ">="
49
63
  - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
50
66
  version: "0"
51
- version:
52
67
  requirements: []
53
68
 
54
69
  rubyforge_project:
55
- rubygems_version: 1.3.5
70
+ rubygems_version: 1.3.6
56
71
  signing_key:
57
72
  specification_version: 3
58
73
  summary: Classified
59
74
  test_files:
60
75
  - test/scheduled_worker.rb
61
- - test/simple_worker_tests.rb
76
+ - test/test_simple_worker.rb
62
77
  - test/test_worker.rb
data/lib/api_auth.rb DELETED
@@ -1,15 +0,0 @@
1
- module SimpleWorker
2
-
3
- class ApiAuth
4
-
5
- attr_accessor :host
6
-
7
- def initialize(access_key, secret_key, options={})
8
- @access_key = access_key
9
- @secret_key = secret_key
10
- @host = options[:host] || "http://simpleworker.appoxy.com/api/"
11
-
12
- end
13
- end
14
-
15
- end
data/lib/http_enabled.rb DELETED
@@ -1,119 +0,0 @@
1
- require 'rubygems'
2
- require 'active_support'
3
- require 'net/http'
4
- require 'base64'
5
-
6
- require File.join(File.dirname(__FILE__), 'simple_worker_error')
7
-
8
- begin
9
- require 'digest/hmac'
10
- USE_EMBEDDED_HMAC = false
11
- rescue
12
- puts "HMAC, not found in standard lib." + $!.message
13
- require 'hmac-sha1'
14
- USE_EMBEDDED_HMAC = true
15
- end
16
-
17
- module SimpleWorker
18
-
19
-
20
- module HttpEnabled
21
-
22
- # body is a hash
23
- def run_http(host, access_key, secret_key, http_method, command_path, body=nil, parameters={}, extra_headers=nil)
24
- ts = generate_timestamp(Time.now.gmtime)
25
- # puts 'timestamp = ' + ts
26
- sig = generate_signature_v0(command_path, ts, secret_key)
27
- # puts "My signature = " + sig
28
- url = host + command_path
29
- # puts url
30
-
31
- user_agent = "Ruby Client"
32
- headers = {'User-Agent' => user_agent}
33
-
34
- if !extra_headers.nil?
35
- extra_headers.each_pair do |k, v|
36
- headers[k] = v
37
- end
38
- end
39
-
40
- extra_params = {'sigv'=>"0.1", 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
41
- if http_method == :put
42
- body.update(extra_params)
43
- else
44
- parameters = {} if parameters.nil?
45
- parameters.update(extra_params)
46
- # puts 'params=' + parameters.inspect
47
-
48
- end
49
-
50
-
51
- uri = URI.parse(url)
52
- #puts 'body=' + body.to_s
53
- if (http_method == :put)
54
- req = Net::HTTP::Put.new(uri.path)
55
- body = ActiveSupport::JSON.encode(body)
56
- req.body = body unless body.nil?
57
- elsif (http_method == :post)
58
- req = Net::HTTP::Post.new(uri.path)
59
- if !parameters.nil?
60
- req.set_form_data(parameters)
61
- else
62
- req.body = body unless body.nil?
63
- end
64
- elsif (http_method == :delete)
65
- req = Net::HTTP::Delete.new(uri.path)
66
- if !parameters.nil?
67
- req.set_form_data(parameters)
68
- end
69
- else
70
- req = Net::HTTP::Get.new(uri.path)
71
- if !parameters.nil?
72
- req.set_form_data(parameters)
73
- end
74
- end
75
- headers.each_pair do |k, v|
76
- req[k] = v
77
- end
78
- # req.each_header do |k, v|
79
- # puts 'header ' + k + '=' + v
80
- #end
81
- res = Net::HTTP.start(uri.host, uri.port) do |http|
82
- http.request(req)
83
- end
84
-
85
- ret = ''
86
- case res
87
- when Net::HTTPSuccess
88
- # puts 'response body=' + res.body
89
- ret = res.body
90
- when Net::HTTPClientError
91
- raise SimpleWorker::ClientError.new(res.class.name, ActiveSupport::JSON.decode(res.body))
92
- else
93
- #res.error
94
- raise SimpleWorker::ServiceError.new(res.class.name, res.body)
95
- end
96
- return ret
97
- end
98
-
99
- def generate_timestamp(gmtime)
100
- return gmtime.strftime("%Y-%m-%dT%H:%M:%SZ")
101
- end
102
-
103
- def generate_signature_v0(operation, timestamp, secret_key)
104
- HttpEnabled.generate_signature_v0(operation, timestamp, secret_key)
105
- end
106
-
107
- def self.generate_signature_v0(operation, timestamp, secret_key)
108
- if USE_EMBEDDED_HMAC
109
- my_sha_hmac = HMAC::SHA1.digest(secret_key, operation + timestamp)
110
- else
111
- my_sha_hmac = Digest::HMAC.digest(operation + timestamp, secret_key, Digest::SHA1)
112
- end
113
- my_b64_hmac_digest = Base64.encode64(my_sha_hmac).strip
114
- return my_b64_hmac_digest
115
- end
116
- end
117
-
118
- end
119
-
@@ -1,22 +0,0 @@
1
- module SimpleWorker
2
- class ClientError < StandardError
3
-
4
- attr_reader :response_hash
5
-
6
- def initialize(class_name, response_hash)
7
- puts 'response-hash=' + response_hash.inspect
8
- super("#{class_name} - #{response_hash["msg"]}")
9
- @response_hash = response_hash
10
- end
11
- end
12
-
13
- class ServiceError < StandardError
14
- attr_reader :body
15
-
16
- def initialize(class_name, body)
17
- super("#{class_name}")
18
- @body = body
19
-
20
- end
21
- end
22
- end