Class: PerfectSched::SimpleDBBackend

Inherits:
Backend
  • Object
show all
Defined in:
lib/perfectsched/backend/simpledb.rb

Constant Summary collapse

MAX_SELECT_ROW =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

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_readObject

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



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
82
83
# File 'lib/perfectsched/backend/simpledb.rb', line 51

def acquire(timeout, now=Time.now.to_i)
  while true
    rows = 0
    @domain.items.select('timeout', 'next_time', 'cron', 'delay', 'data',
                        :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
        salt = int_encode(timeout)

        return [id,salt], Task.new(id, next_time, cron, delay, data)

      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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/perfectsched/backend/simpledb.rb', line 96

def add_checked(id, cron, delay, data, next_time, timeout)
  begin
    @domain.items[id].attributes.replace('timeout'=>int_encode(timeout), 'next_time'=>int_encode(next_time),
        'cron'=>cron, 'delay'=>int_encode(delay), 'data'=>data,
        :unless=>'timeout')
    return true
  rescue AWS::SimpleDB::Errors::ConditionalCheckFailed, AWS::SimpleDB::Errors::ExistsAndExpectedValue
    return nil
  end
end

#delete(id) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/perfectsched/backend/simpledb.rb', line 107

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



85
86
87
88
89
90
91
92
93
94
# File 'lib/perfectsched/backend/simpledb.rb', line 85

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



117
118
119
120
121
122
123
124
125
126
# File 'lib/perfectsched/backend/simpledb.rb', line 117

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
  return cron, delay, data
end

#list(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/perfectsched/backend/simpledb.rb', line 29

def list(&block)
  rows = 0
  @domain.items.select('timeout', 'next_time', 'cron', 'delay', 'data',
                      :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
    timeout = int_decode(attrs['timeout'].first)

    yield id, cron, delay, data, next_time, timeout
  }
end

#modify_checked(id, cron, delay, data) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/perfectsched/backend/simpledb.rb', line 128

def modify_checked(id, cron, delay, data)
  unless get(id)
    return false
  end
  @domain.items[id].attributes.replace('cron'=>cron, 'delay'=>int_encode(delay), 'data'=>data)
  return true
end

#modify_data_checked(id, data) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/perfectsched/backend/simpledb.rb', line 144

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



136
137
138
139
140
141
142
# File 'lib/perfectsched/backend/simpledb.rb', line 136

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