Class: Tina4::Job

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Job.



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

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

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#available_atObject (readonly)

Returns the value of attribute available_at.



7
8
9
# File 'lib/tina4/job.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/job.rb', line 7

def created_at
  @created_at
end

#errorObject (readonly)

Returns the value of attribute error.



78
79
80
# File 'lib/tina4/job.rb', line 78

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#priorityObject (readonly)

Returns the value of attribute priority.



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

def priority
  @priority
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#topicObject (readonly)

Returns the value of attribute topic.



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

def topic
  @topic
end

Instance Method Details

#completeObject

Mark this job as completed.



62
63
64
# File 'lib/tina4/job.rb', line 62

def complete
  @status = :completed
end

#fail(reason = "") ⇒ Object

Mark this job as failed with a reason.



67
68
69
70
71
# File 'lib/tina4/job.rb', line 67

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

#increment_attempts!Object



57
58
59
# File 'lib/tina4/job.rb', line 57

def increment_attempts!
  @attempts += 1
end

#reject(reason = "") ⇒ Object

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



74
75
76
# File 'lib/tina4/job.rb', line 74

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

#retry(delay_seconds: 0) ⇒ Object

Re-queue this message with incremented attempts. Uses the stored queue reference (set at construction time).

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
# File 'lib/tina4/job.rb', line 24

def retry(delay_seconds: 0)
  q = @queue
  raise ArgumentError, "No queue reference — set at construction" unless q

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

#to_arrayObject



35
36
37
# File 'lib/tina4/job.rb', line 35

def to_array
  [@id, @topic, @payload, @priority, @attempts]
end

#to_hashObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tina4/job.rb', line 39

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



53
54
55
# File 'lib/tina4/job.rb', line 53

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