Class: Fog::Rackspace::Queues::Mock::MockQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/queues.rb

Overview

An in-memory Queue implementation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ MockQueue

Returns a new instance of MockQueue.



105
106
107
108
109
# File 'lib/fog/rackspace/queues.rb', line 105

def initialize(name)
  @name, @metadata = name, {}
  @messages, @claims = [], {}
  @id_counter = Fog::Mock.random_hex(24).to_i(16)
end

Instance Attribute Details

#claimsObject

Returns the value of attribute claims.



103
104
105
# File 'lib/fog/rackspace/queues.rb', line 103

def claims
  @claims
end

#messagesObject

Returns the value of attribute messages.



103
104
105
# File 'lib/fog/rackspace/queues.rb', line 103

def messages
  @messages
end

#metadataObject

Returns the value of attribute metadata.



103
104
105
# File 'lib/fog/rackspace/queues.rb', line 103

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



103
104
105
# File 'lib/fog/rackspace/queues.rb', line 103

def name
  @name
end

Instance Method Details

#add_claim(ttl, grace) ⇒ Object

Create a new MockClaim.

Parameters:

  • ttl (Integer)

    Time-to-live for the claim.

  • grace (Integer)

    Grace period that’s added to messages included in this claim.



164
165
166
167
168
# File 'lib/fog/rackspace/queues.rb', line 164

def add_claim(ttl, grace)
  claim = MockClaim.new(self, ttl, grace)
  claims[claim.id] = claim
  claim
end

#add_message(client_id, data, ttl) ⇒ MockMessage

Append a new message to the queue.

Parameters:

  • client_id (String)

    UUID for the service object.

  • data (Hash)

    Message payload.

  • ttl (Integer)

    Number of seconds that the message should exist.

Returns:

  • (MockMessage)

    The message object that was created.



152
153
154
155
156
157
158
# File 'lib/fog/rackspace/queues.rb', line 152

def add_message(client_id, data, ttl)
  id = @id_counter.to_s(16)
  @id_counter += 1
  message = MockMessage.new(id, self, client_id, data, ttl)
  @messages << message
  message
end

#ageoffObject

Remove any messages or claims whose ttls have expired.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fog/rackspace/queues.rb', line 180

def ageoff
  messages.reject! { |m| m.expired? }

  claims.keys.dup.each do |id|
    claim = claims[id]
    if claim.expired? || claim.messages.empty?
      claim.messages.each { |m| m.claim = nil }
      claims.delete(id)
    end
  end
end

#claim!(claim_id) ⇒ MockClaim

Access an existing MockClaim by id.

Parameters:

  • claim_id (String)

    An ID of an existing claim.

Returns:



175
176
177
# File 'lib/fog/rackspace/queues.rb', line 175

def claim!(claim_id)
  claims[claim_id] or raise NotFound.new
end

#claimedInteger

The number of messages currently held by a claim.

Returns:

  • (Integer)


121
122
123
# File 'lib/fog/rackspace/queues.rb', line 121

def claimed
  @messages.count { |msg| msg.claimed? }
end

#freeInteger

The number of messages not held by any claim.

Returns:

  • (Integer)


128
129
130
# File 'lib/fog/rackspace/queues.rb', line 128

def free
  @messages.count { |msg| ! msg.claimed? }
end

#newestMockMessage|UndefinedObject

The most recently published message on this queue, or ‘nil`.

Returns:



142
143
144
# File 'lib/fog/rackspace/queues.rb', line 142

def newest
  @messages.last
end

#oldestMockMessage|UndefinedObject

The oldest published message on this queue, or ‘nil`.

Returns:



135
136
137
# File 'lib/fog/rackspace/queues.rb', line 135

def oldest
  @messages.first
end

#totalInteger

The total number of messages currently on the queue.

Returns:

  • (Integer)


114
115
116
# File 'lib/fog/rackspace/queues.rb', line 114

def total
  @messages.size
end