Class: PerfectSched::SimpleDBBackend
- Defined in:
- lib/perfectsched/backend/simpledb.rb
Constant Summary collapse
- MAX_SELECT_ROW =
4
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
- #add_checked(id, cron, delay, data, next_time, timeout, timezone) ⇒ Object
- #delete(id) ⇒ Object
- #finish(token, next_time, timeout) ⇒ Object
- #get(id) ⇒ Object
-
#initialize(key_id, secret_key, domain) ⇒ SimpleDBBackend
constructor
A new instance of SimpleDBBackend.
- #list(&block) ⇒ Object
- #modify_checked(id, cron, delay, data, timezone) ⇒ Object
- #modify_data_checked(id, data) ⇒ Object
- #modify_sched_checked(id, cron, delay) ⇒ Object
- #use_consistent_read(b = true) ⇒ Object
Methods inherited from Backend
#add, #close, #modify, #modify_data, #modify_sched
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/perfectsched/backend/simpledb.rb', line 6 def initialize(key_id, secret_key, domain) super() require 'aws-sdk' @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/perfectsched/backend/simpledb.rb', line 22 def consistent_read @consistent_read end |
Instance Method Details
#acquire(timeout, now = Time.now.to_i) ⇒ Object
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 82 83 84 85 |
# File 'lib/perfectsched/backend/simpledb.rb', line 52 def acquire(timeout, now=Time.now.to_i) while true rows = 0 @domain.items.select('timeout', 'next_time', 'cron', 'delay', 'data', 'timezone', :where => "timeout <= '#{int_encode(now)}'", :order => [:timeout, :asc], :consistent_read => @consistent_read, :limit => MAX_SELECT_ROW) {|itemdata| begin id = itemdata.name attrs = itemdata.attributes @domain.items[id].attributes.replace('timeout'=>int_encode(timeout), :if=>{'timeout'=>attrs['timeout'].first}) next_time = int_decode(attrs['next_time'].first) cron = attrs['cron'].first delay = int_decode(attrs['delay'].first) data = attrs['data'].first timezone = attrs['timezone'].first salt = int_encode(timeout) return [id,salt], Task.new(id, next_time, cron, delay, data, timezone) rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::AttributeDoesNotExist end rows += 1 } if rows < MAX_SELECT_ROW return nil end end end |
#add_checked(id, cron, delay, data, next_time, timeout, timezone) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/perfectsched/backend/simpledb.rb', line 98 def add_checked(id, cron, delay, data, next_time, timeout, timezone) begin hash = {} hash['timeout'] = int_encode(timeout) hash['next_time'] = int_encode(next_time) hash['cron'] = cron hash['delay'] = int_encode(delay) hash['data'] = data hash['timezone'] = timezone if timezone hash[:unless] = 'timeout' @domain.items[id].attributes.replace() return true rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::ExistsAndExpectedValue return nil end end |
#delete(id) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/perfectsched/backend/simpledb.rb', line 115 def delete(id) # TODO return value begin @domain.items[id].delete return true rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::AttributeDoesNotExist return false end end |
#finish(token, next_time, timeout) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/perfectsched/backend/simpledb.rb', line 87 def finish(token, next_time, timeout) begin id, salt = *token @domain.items[id].attributes.replace('timeout'=>int_encode(timeout), 'next_time'=>int_encode(next_time), :if=>{'timeout'=>salt}) return true rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::AttributeDoesNotExist return false end end |
#get(id) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/perfectsched/backend/simpledb.rb', line 125 def get(id) attrs = @domain.items[id].data.attributes cron = attrs['cron'].first unless cron return nil end delay = int_decode(attrs['delay'].first) data = attrs['data'].first timezone = attrs['timezone'].first next_time = int_decode(attrs['next_time'].first) return cron, delay, data, timezone, next_time end |
#list(&block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/perfectsched/backend/simpledb.rb', line 29 def list(&block) rows = 0 @domain.items.select('timeout', 'next_time', 'cron', 'delay', 'data', 'timezone', :where => "timeout > '#{int_encode(0)}'", # required by SimpleDB :order => [:timeout, :asc], :consistent_read => @consistent_read, :limit => MAX_SELECT_ROW) {|itemdata| id = itemdata.name attrs = itemdata.attributes next_time = int_decode(attrs['next_time'].first) cron = attrs['cron'].first delay = int_decode(attrs['delay'].first) data = attrs['data'].first timezone = attrs['timezone'].first timeout = int_decode(attrs['timeout'].first) yield id, cron, delay, data, next_time, timeout, timezone } end |
#modify_checked(id, cron, delay, data, timezone) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/perfectsched/backend/simpledb.rb', line 138 def modify_checked(id, cron, delay, data, timezone) unless get(id) return false end @domain.items[id].attributes.replace('cron'=>cron, 'delay'=>int_encode(delay), 'data'=>data, 'timezone'=>timezone) return true end |
#modify_data_checked(id, data) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/perfectsched/backend/simpledb.rb', line 154 def modify_data_checked(id, data) unless get(id) return false end @domain.items[id].attributes.replace('data'=>data) return true end |
#modify_sched_checked(id, cron, delay) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/perfectsched/backend/simpledb.rb', line 146 def modify_sched_checked(id, cron, delay) unless get(id) return false end @domain.items[id].attributes.replace('cron'=>cron, 'delay'=>int_encode(delay)) return true end |
#use_consistent_read(b = true) ⇒ Object
24 25 26 27 |
# File 'lib/perfectsched/backend/simpledb.rb', line 24 def use_consistent_read(b=true) @consistent_read = b self end |