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.



99
100
101
102
103
# File 'lib/fog/rackspace/queues.rb', line 99

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.



97
98
99
# File 'lib/fog/rackspace/queues.rb', line 97

def claims
  @claims
end

#messagesObject

Returns the value of attribute messages.



97
98
99
# File 'lib/fog/rackspace/queues.rb', line 97

def messages
  @messages
end

#metadataObject

Returns the value of attribute metadata.



97
98
99
# File 'lib/fog/rackspace/queues.rb', line 97

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



97
98
99
# File 'lib/fog/rackspace/queues.rb', line 97

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.



158
159
160
161
162
# File 'lib/fog/rackspace/queues.rb', line 158

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.



146
147
148
149
150
151
152
# File 'lib/fog/rackspace/queues.rb', line 146

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.



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/fog/rackspace/queues.rb', line 174

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:



169
170
171
# File 'lib/fog/rackspace/queues.rb', line 169

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

#claimedInteger

The number of messages currently held by a claim.

Returns:

  • (Integer)


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

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

#freeInteger

The number of messages not held by any claim.

Returns:

  • (Integer)


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

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

#newestMockMessage|UndefinedObject

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

Returns:



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

def newest
  @messages.last
end

#oldestMockMessage|UndefinedObject

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

Returns:



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

def oldest
  @messages.first
end

#totalInteger

The total number of messages currently on the queue.

Returns:

  • (Integer)


108
109
110
# File 'lib/fog/rackspace/queues.rb', line 108

def total
  @messages.size
end