em_s3 0.0.3 → 0.0.4

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.
@@ -1,3 +1,3 @@
1
1
  module EmS3
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,4 +1,4 @@
1
- class S3Agent < EM::Queue
1
+ class S3Agent
2
2
  include Singleton
3
3
  # Refer to http://eventmachine.rubyforge.org/EventMachine/Queue.html for interface details
4
4
  # This agent class solves the following problem
@@ -11,6 +11,10 @@ class S3Agent < EM::Queue
11
11
  def initialize
12
12
  @public_key = nil
13
13
  @private_key = nil
14
+ setup
15
+ end
16
+
17
+ def setup
14
18
  @obj_pools = {}
15
19
  end
16
20
 
@@ -20,17 +24,6 @@ class S3Agent < EM::Queue
20
24
  self
21
25
  end
22
26
 
23
- # Called from clients if due to some reason, they skip over the write part
24
- # @param [String] bucket
25
- # The name of the bucket
26
- # @param [String] object
27
- # The name of the object's key
28
- # @return [true]
29
- def revoke_service(bucket, object)
30
- @obj_pools.delete("#{bucket}:#{object}")
31
- true
32
- end
33
-
34
27
  # Requests any of :get or :put from S3.
35
28
  # If the object is not being processed now, service it immediately.
36
29
  # If not, push it in a queue and wait for the reactor to take it through.
@@ -43,27 +36,18 @@ class S3Agent < EM::Queue
43
36
  # @param [String] value
44
37
  # The object's value
45
38
  # @return [true]
46
- def request_service(method, bucket, object, value = nil, &blk)
47
- # Allow `put`s to go through.
48
- # For `get`s, check if the object is not in use and only then, allow to pass though
49
- if (!@obj_pools["#{bucket}:#{object}"]) || (method == :put)
50
- # Some client is using the agent.
51
- @obj_pools["#{bucket}:#{object}"] = true
39
+ def request_service(method, *args)
40
+ setup
41
+ self.send(method.to_sym, *args)
42
+ end
43
+
44
+ def method_missing(method, *args)
45
+ params = args[0]
46
+ q = (@obj_pools["#{params[:bucket]}:#{params[:object]}"] ||= EM::Queue.new)
47
+ q.push({:method => method, :params => params})
48
+ q.pop{|request|
52
49
  s3i = S3Interface.new(@public_key, @private_key)
53
- s3i.callback{|resp, status|
54
- # Unblock the other client only if this is a write
55
- revoke_service(bucket, object) if method == :put
56
- yield resp, status if block_given?
57
- }
58
- method == :get ? s3i.get_object(bucket, object) : s3i.put_object(bucket, object, value)
59
- else
60
- push({:bucket => bucket, :object => object, :value => value})
61
- pop{|request|
62
- request_service(request[:bucket], request[:object], request[:value]){|resp, status|
63
- yield resp, status if block_given?
64
- }
65
- }
66
- end
67
- true
50
+ s3i.send(method, params)
51
+ }
68
52
  end
69
53
  end
@@ -19,6 +19,7 @@ class S3Interface
19
19
  if (@num_tries += 1) < retry_count
20
20
  retry_request
21
21
  else
22
+ @cb.call resp, status if @cb
22
23
  succeed resp, status
23
24
  end
24
25
  }
@@ -34,7 +35,12 @@ class S3Interface
34
35
  # @param [String] content_type
35
36
  # The value's MIME type
36
37
  # @return [self]
37
- def put_object(bucket, object, value, content_type = 'binary/octet-stream')
38
+ def put(params)
39
+ bucket = params[:bucket]
40
+ object = params[:object]
41
+ value = params[:value]
42
+ content_type = params[:content_type]
43
+ cb = params[:cb]
38
44
  date = generate_date
39
45
  sign_string = generate_signed_string('PUT', 'private', bucket, object, content_type)
40
46
  signature = generate_signature(sign_string)
@@ -43,6 +49,7 @@ class S3Interface
43
49
  path = "/" << object
44
50
 
45
51
  @req_options = {:method => :put, :head => headers, :path => path, :body => value}
52
+ @cb = cb if cb
46
53
  @bucket = bucket
47
54
  try_request
48
55
  self
@@ -54,7 +61,11 @@ class S3Interface
54
61
  # @param [String] object
55
62
  # The name of the object's key
56
63
  # @return [self]
57
- def get_object(bucket, object)
64
+ def get(params)
65
+ bucket = params[:bucket]
66
+ object = params[:object]
67
+ content_type = params[:content_type]
68
+ cb = params[:cb]
58
69
  date = generate_date
59
70
  sign_string = generate_signed_string('GET', nil, bucket, object, 'text/plain')
60
71
  signature = generate_signature(sign_string)
@@ -63,6 +74,7 @@ class S3Interface
63
74
  path = "/" << object
64
75
 
65
76
  @req_options = {:method => :get, :head => headers, :path => path}
77
+ @cb = cb if cb
66
78
  @bucket = bucket
67
79
  try_request
68
80
  self
@@ -70,7 +82,7 @@ class S3Interface
70
82
 
71
83
  private
72
84
 
73
- # Perhaps an inappropriate method name, sonce we call it even the first time
85
+ # Perhaps an inappropriate method name, since we call it even the first time
74
86
  def retry_request
75
87
  # Explore persistent connections from within AWS
76
88
  s3_conn = EM::HttpRequest.new("http://#{@bucket}.s3.amazonaws.com")
@@ -78,6 +90,7 @@ class S3Interface
78
90
  s3_req = s3_conn.send(req_method, @req_options)
79
91
  s3_req.callback{|cli|
80
92
  if cli.response_header.http_status < 500
93
+ @cb.call cli.response, cli.response_header.http_status if @cb
81
94
  self.succeed cli.response, cli.response_header.http_status
82
95
  else # Some S3 issue
83
96
  self.fail cli.response, cli.response_header.http_status
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em_s3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-11 00:00:00.000000000 Z
12
+ date: 2012-04-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: em-http-request