Class: PerfectQueue::SimpleDBBackend
- Defined in:
- lib/perfectqueue/backend/simpledb.rb
Constant Summary collapse
- MAX_SELECT_ROW =
32
Instance Attribute Summary collapse
-
#consistent_read ⇒ Object
Returns the value of attribute consistent_read.
Instance Method Summary collapse
- #acquire(timeout, now = Time.now.to_i) ⇒ Object
- #cancel(id, delete_timeout = 3600, now = Time.now.to_i) ⇒ Object
- #finish(token, delete_timeout = 3600, now = Time.now.to_i) ⇒ Object
-
#initialize(key_id, secret_key, domain) ⇒ SimpleDBBackend
constructor
A new instance of SimpleDBBackend.
- #list(&block) ⇒ Object
- #submit(id, data, time = Time.now.to_i) ⇒ Object
- #update(token, timeout) ⇒ Object
- #use_consistent_read(b = true) ⇒ Object
Methods inherited from Backend
Constructor Details
#initialize(key_id, secret_key, domain) ⇒ SimpleDBBackend
Returns a new instance of SimpleDBBackend.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 6 def initialize(key_id, secret_key, domain) gem "aws-sdk" require 'aws' @consistent_read = false @db = AWS::SimpleDB.new( :access_key_id => key_id, :secret_access_key => secret_key) @domain_name = domain @domain = @db.domains[@domain_name] unless @domain.exists? @domain = @db.domains.create(@domain_name) end end |
Instance Attribute Details
#consistent_read ⇒ Object
Returns the value of attribute consistent_read.
22 23 24 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 22 def consistent_read @consistent_read end |
Instance Method Details
#acquire(timeout, now = Time.now.to_i) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 45 def acquire(timeout, now=Time.now.to_i) while true rows = 0 @domain.items.select('timeout', 'data', 'created_at', :where => "timeout <= '#{int_encode(now)}'", :order => [:timeout, :asc], :consistent_read => @consistent_read, :limit => MAX_SELECT_ROW) {|itemdata| begin id = itemdata.name attrs = itemdata.attributes salt = attrs['created_at'].first if !salt || salt.empty? # finished/canceled task @domain.items[id].delete(:if=>{'created_at'=>''}) else created_at = int_decode(salt) @domain.items[id].attributes.replace('timeout'=>int_encode(timeout), :if=>{'timeout'=>attrs['timeout'].first}) data = attrs['data'].first return [id,salt], Task.new(id, created_at, data) end rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::AttributeDoesNotExist end rows += 1 } if rows < MAX_SELECT_ROW return nil end end end |
#cancel(id, delete_timeout = 3600, now = Time.now.to_i) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 105 def cancel(id, delete_timeout=3600, now=Time.now.to_i) salt = @domain.items[id].attributes['created_at'].first unless salt return false end token = [id,salt] finish(token, delete_timeout, now) end |
#finish(token, delete_timeout = 3600, now = Time.now.to_i) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 83 def finish(token, delete_timeout=3600, now=Time.now.to_i) begin id, salt = *token @domain.items[id].attributes.replace('timeout'=>int_encode(now+delete_timeout), 'created_at'=>'', :if=>{'created_at'=>salt}) return true rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::AttributeDoesNotExist return false end end |
#list(&block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 29 def list(&block) @domain.items.each {|item| id = item.name attrs = item.data.attributes salt = attrs['created_at'].first if salt && !salt.empty? created_at = int_decode(salt) data = attrs['data'].first timeout = int_decode(attrs['timeout'].first) yield id, created_at, data, timeout end } end |
#submit(id, data, time = Time.now.to_i) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 114 def submit(id, data, time=Time.now.to_i) begin @domain.items[id].attributes.replace('timeout'=>int_encode(time), 'created_at'=>int_encode(time), 'data'=>data, :unless=>'timeout') return true rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::ExistsAndExpectedValue return nil end end |
#update(token, timeout) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 94 def update(token, timeout) begin id, salt = *token @domain.items[id].attributes.replace('timeout'=>int_encode(timeout), :if=>{'created_at'=>salt}) rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::AttributeDoesNotExist raise CanceledError, "Task id=#{id} is canceled." end nil end |
#use_consistent_read(b = true) ⇒ Object
24 25 26 27 |
# File 'lib/perfectqueue/backend/simpledb.rb', line 24 def use_consistent_read(b=true) @consistent_read = b self end |