Class: Fog::Rackspace::Queues::Mock::MockClaim

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

Overview

Reservation indicating that a consumer is in the process of handling a collection of messages from a queue.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, ttl, grace) ⇒ MockClaim

Create a new MockClaim. Clients should use Fog::Rackspace::Queues::Mock::MockQueue#add_claim instead.

Parameters:

  • queue (MockQueue) —

    The queue that owns this claim.

  • ttl (Integer) —

    Duration, in seconds, that this queue should last.

  • grace (Integer) —

    Extra life granted to messages within this claim after this claim expires, to give another consumer a chance to process it.



260
261
262
263
264
265
# File 'lib/fog/rackspace/queues.rb', line 260

def initialize(queue, ttl, grace)
  @queue = queue
  @id = Fog::Mock.random_hex(24)
  @ttl, @grace = ttl, grace
  touch!
end

Instance Attribute Details

#grace ⇒ Object

Returns the value of attribute grace.



252
253
254
# File 'lib/fog/rackspace/queues.rb', line 252

def grace
  @grace
end

#id ⇒ Object

Returns the value of attribute id.



252
253
254
# File 'lib/fog/rackspace/queues.rb', line 252

def id
  @id
end

#queue ⇒ Object

Returns the value of attribute queue.



252
253
254
# File 'lib/fog/rackspace/queues.rb', line 252

def queue
  @queue
end

#ttl ⇒ Object

Returns the value of attribute ttl.



252
253
254
# File 'lib/fog/rackspace/queues.rb', line 252

def ttl
  @ttl
end

Instance Method Details

#age ⇒ Integer

Determine how long ago this claim was created, in seconds.

Returns:

  • (Integer)


282
283
284
# File 'lib/fog/rackspace/queues.rb', line 282

def age
  Time.now.to_i - @created
end

#expired? ⇒ Boolean

Determine if this claim has lasted longer than its designated ttl.

Returns:

  • (Boolean)


289
290
291
# File 'lib/fog/rackspace/queues.rb', line 289

def expired?
  age > ttl
end

#message_end_of_life ⇒ Integer

Calculate the time at which messages belonging to this claim should expire.

Returns:

  • (Integer) —

    Seconds since the epoch.



275
276
277
# File 'lib/fog/rackspace/queues.rb', line 275

def message_end_of_life
  @created + @ttl + @grace
end

#messages ⇒ Array<Message>

Access the collection of messages owned by this claim.

Returns:



296
297
298
# File 'lib/fog/rackspace/queues.rb', line 296

def messages
  @queue.messages.select { |m| m.claim == self }
end

#to_h ⇒ Hash

Convert this claim into a GET payload.

Returns:



303
304
305
306
307
308
309
310
311
312
# File 'lib/fog/rackspace/queues.rb', line 303

def to_h
  ms = messages.map { |m| m.to_h }

  {
    "age" => age,
    "href" => "#{PATH_BASE}/#{@queue.name}/claims/#{@id}",
    "ttl" => @ttl,
    "messages" => ms
  }
end

#touch! ⇒ Object

Set or reset the creation time of the claim to the present.



268
269
270
# File 'lib/fog/rackspace/queues.rb', line 268

def touch!
  @created = Time.now.to_i
end