Class: S3Agent

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/s3_agent.rb

Instance Method Summary collapse

Constructor Details

#initializeS3Agent

Refer to eventmachine.rubyforge.org/EventMachine/Queue.html for interface details This agent class solves the following problem RMW in S3. It has a pool of (s3) bucket, object names which are right now being processed. When a new request comes, we check if its right now sent out to S3. If yes, add it to the queue and add a pop request which will do the same thing again. If no, process it If a new read request for the same (object, bucket) comes, it will cycle through the push/pop cycle.



11
12
13
14
15
# File 'lib/s3_agent.rb', line 11

def initialize
  @public_key = nil
  @private_key = nil
  setup
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/s3_agent.rb', line 44

def method_missing(method, *args)
  params = args[0]
  q = (@obj_pools["#{params[:bucket]}:#{params[:object]}"] ||= EM::Queue.new)
  q.push({:method => method, :params => params})
  q.pop{|request|
    s3i = S3Interface.new(@public_key, @private_key)
    s3i.send(method, params)
  }
end

Instance Method Details

#request_service(method, *args) ⇒ true

Requests any of :get or :put from S3. If the object is not being processed now, service it immediately. If not, push it in a queue and wait for the reactor to take it through.

Parameters:

  • method (Symbol)

    The HTTP method in lower case. Either :get or :put

  • bucket (String)

    The name of the bucket

  • object (String)

    The name of the object’s key

  • value (String)

    The object’s value

Returns:

  • (true)


39
40
41
42
# File 'lib/s3_agent.rb', line 39

def request_service(method, *args)
  setup
  self.send(method.to_sym, *args)
end

#set_keys(public_key, private_key) ⇒ Object



21
22
23
24
25
# File 'lib/s3_agent.rb', line 21

def set_keys(public_key, private_key)
  @public_key ||= public_key
  @private_key ||= private_key
  self
end

#setupObject



17
18
19
# File 'lib/s3_agent.rb', line 17

def setup
  @obj_pools = {}
end