Class: Tina4::QueueMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0) ⇒ QueueMessage

Returns a new instance of QueueMessage.



10
11
12
13
14
15
16
17
18
19
# File 'lib/tina4/queue.rb', line 10

def initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0)
  @id = id || SecureRandom.uuid
  @topic = topic
  @payload = payload
  @created_at = Time.now
  @attempts = attempts
  @priority = priority
  @available_at = available_at
  @status = :pending
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



7
8
9
# File 'lib/tina4/queue.rb', line 7

def attempts
  @attempts
end

#available_atObject (readonly)

Returns the value of attribute available_at.



7
8
9
# File 'lib/tina4/queue.rb', line 7

def available_at
  @available_at
end

#created_atObject (readonly)

Returns the value of attribute created_at.



7
8
9
# File 'lib/tina4/queue.rb', line 7

def created_at
  @created_at
end

#errorObject (readonly)

Returns the value of attribute error.



70
71
72
# File 'lib/tina4/queue.rb', line 70

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/tina4/queue.rb', line 7

def id
  @id
end

#payloadObject (readonly)

Returns the value of attribute payload.



7
8
9
# File 'lib/tina4/queue.rb', line 7

def payload
  @payload
end

#priorityObject (readonly)

Returns the value of attribute priority.



7
8
9
# File 'lib/tina4/queue.rb', line 7

def priority
  @priority
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/tina4/queue.rb', line 8

def status
  @status
end

#topicObject (readonly)

Returns the value of attribute topic.



7
8
9
# File 'lib/tina4/queue.rb', line 7

def topic
  @topic
end

Instance Method Details

#completeObject

Mark this job as completed.



54
55
56
# File 'lib/tina4/queue.rb', line 54

def complete
  @status = :completed
end

#fail(reason = "") ⇒ Object

Mark this job as failed with a reason.



59
60
61
62
63
# File 'lib/tina4/queue.rb', line 59

def fail(reason = "")
  @status = :failed
  @error = reason
  @attempts += 1
end

#increment_attempts!Object



49
50
51
# File 'lib/tina4/queue.rb', line 49

def increment_attempts!
  @attempts += 1
end

#reject(reason = "") ⇒ Object

Reject this job with a reason. Alias for fail().



66
67
68
# File 'lib/tina4/queue.rb', line 66

def reject(reason = "")
  fail(reason)
end

#retry(queue:, delay_seconds: 0) ⇒ Object

Re-queue this message with incremented attempts. Delegates to the queue’s backend via the queue reference.



23
24
25
26
27
28
29
# File 'lib/tina4/queue.rb', line 23

def retry(queue:, delay_seconds: 0)
  @attempts += 1
  @status = :pending
  @available_at = delay_seconds > 0 ? Time.now + delay_seconds : nil
  queue.backend.enqueue(self)
  self
end

#to_hashObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tina4/queue.rb', line 31

def to_hash
  h = {
    id: @id,
    topic: @topic,
    payload: @payload,
    created_at: @created_at.iso8601,
    attempts: @attempts,
    status: @status,
    priority: @priority
  }
  h[:available_at] = @available_at.iso8601 if @available_at
  h
end

#to_json(*_args) ⇒ Object



45
46
47
# File 'lib/tina4/queue.rb', line 45

def to_json(*_args)
  JSON.generate(to_hash)
end