Class: PerfectQueue::SimpleDBBackend

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

Constant Summary collapse

MAX_SELECT_ROW =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Backend

#close

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
# File 'lib/perfectqueue/backend/simpledb.rb', line 6

def initialize(key_id, secret_key, domain)
  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.



21
22
23
# File 'lib/perfectqueue/backend/simpledb.rb', line 21

def consistent_read
  @consistent_read
end

Instance Method Details

#acquire(timeout, now = Time.now.to_i) ⇒ Object



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

def acquire(timeout, now=Time.now.to_i)
  while true
    # TODO support resource limit
    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, nil)
        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



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

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



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

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/perfectqueue/backend/simpledb.rb', line 28

def list(&block)
  # TODO support resource limit
  @domain.items.select('timeout', 'data', 'created_at',
                      :where => "created_at != '' AND timeout > '#{int_encode(0)}'",
                      :order => [:timeout, :asc],
                      :consistent_read => @consistent_read) {|itemdata|
    id = itemdata.name
    attrs = itemdata.attributes
    next unless attrs['created_at'].first
    created_at = int_decode(attrs['created_at'].first)
    data = attrs['data'].first
    timeout = int_decode(attrs['timeout'].first)
    yield id, created_at, data, timeout, nil
  }
end

#submit(id, data, time = Time.now.to_i, resource = nil) ⇒ Object



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

def submit(id, data, time=Time.now.to_i, resource=nil)
  # TODO support resource limit
  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



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

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



23
24
25
26
# File 'lib/perfectqueue/backend/simpledb.rb', line 23

def use_consistent_read(b=true)
  @consistent_read = b
  self
end