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.



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

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

Instance Attribute Details

#graceObject

Returns the value of attribute grace.



258
259
260
# File 'lib/fog/rackspace/queues.rb', line 258

def grace
  @grace
end

#idObject

Returns the value of attribute id.



258
259
260
# File 'lib/fog/rackspace/queues.rb', line 258

def id
  @id
end

#queueObject

Returns the value of attribute queue.



258
259
260
# File 'lib/fog/rackspace/queues.rb', line 258

def queue
  @queue
end

#ttlObject

Returns the value of attribute ttl.



258
259
260
# File 'lib/fog/rackspace/queues.rb', line 258

def ttl
  @ttl
end

Instance Method Details

#ageInteger

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

Returns:

  • (Integer)


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

def age
  Time.now.to_i - @created
end

#expired?Boolean

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

Returns:

  • (Boolean)


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

def expired?
  age > ttl
end

#message_end_of_lifeInteger

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

Returns:

  • (Integer)

    Seconds since the epoch.



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

def message_end_of_life
  @created + @ttl + @grace
end

#messagesArray<Message>

Access the collection of messages owned by this claim.

Returns:



302
303
304
# File 'lib/fog/rackspace/queues.rb', line 302

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

#to_hHash

Convert this claim into a GET payload.

Returns:



309
310
311
312
313
314
315
316
317
318
# File 'lib/fog/rackspace/queues.rb', line 309

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.



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

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