Class: Textus::Domain::Jobs::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/domain/jobs/job.rb

Overview

A unit of deferred work. Pure data. The id is ‘<type>:<digest>` where the digest is over the args with sorted keys, so logically-identical enqueues collide on the same id — which is how the Queue dedups (the file already exists). At-least-once delivery means handlers must be idempotent.

Constant Summary collapse

DIGEST_BYTES =
16

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, args:, enqueued_by: nil, attempts: 0, max_attempts: 3, last_error: nil) ⇒ Job

Returns a new instance of Job.



17
18
19
20
21
22
23
24
# File 'lib/textus/domain/jobs/job.rb', line 17

def initialize(type:, args:, enqueued_by: nil, attempts: 0, max_attempts: 3, last_error: nil)
  @type = type.to_s
  @args = stringify(args)
  @enqueued_by = enqueued_by
  @attempts = attempts
  @max_attempts = max_attempts
  @last_error = last_error
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



14
15
16
# File 'lib/textus/domain/jobs/job.rb', line 14

def args
  @args
end

#attemptsObject

Returns the value of attribute attempts.



15
16
17
# File 'lib/textus/domain/jobs/job.rb', line 15

def attempts
  @attempts
end

#enqueued_byObject (readonly)

Returns the value of attribute enqueued_by.



14
15
16
# File 'lib/textus/domain/jobs/job.rb', line 14

def enqueued_by
  @enqueued_by
end

#last_errorObject

Returns the value of attribute last_error.



15
16
17
# File 'lib/textus/domain/jobs/job.rb', line 15

def last_error
  @last_error
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



14
15
16
# File 'lib/textus/domain/jobs/job.rb', line 14

def max_attempts
  @max_attempts
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/textus/domain/jobs/job.rb', line 14

def type
  @type
end

Class Method Details

.from_h(hash) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/textus/domain/jobs/job.rb', line 37

def self.from_h(hash)
  new(
    type: hash["type"], args: hash["args"] || {}, enqueued_by: hash["enqueued_by"],
    attempts: hash["attempts"] || 0, max_attempts: hash["max_attempts"] || 3,
    last_error: hash["last_error"]
  )
end

Instance Method Details

#idObject



26
27
28
# File 'lib/textus/domain/jobs/job.rb', line 26

def id
  "#{@type}:#{digest}"
end

#to_hObject



30
31
32
33
34
35
# File 'lib/textus/domain/jobs/job.rb', line 30

def to_h
  {
    "type" => @type, "args" => @args, "enqueued_by" => @enqueued_by,
    "attempts" => @attempts, "max_attempts" => @max_attempts, "last_error" => @last_error
  }
end