Class: Cosmo::Job::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmo/job/data.rb,
sig/cosmo/job/data.rbs

Constant Summary collapse

DEFAULTS =

Returns:

  • ({ stream: Symbol, retry: Integer, dead: bool, limit: nil })
{ stream: :default, retry: 3, dead: true, limit: nil }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, args, options = nil) ⇒ Data

Returns a new instance of Data.

Parameters:

  • class_name (::String)
  • args (Array[untyped])
  • options (Hash[Symbol, untyped], nil) (defaults to: nil)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cosmo/job/data.rb', line 16

def initialize(class_name, args, options = nil)
  @class_name = class_name
  @args = args
  @options = Hash(options)
  validate!

  @at = @options[:at].to_i if @options[:at]
  @at ||= Time.now.to_i + @options[:in].to_i if @options[:in]
  @subject = @options[:subject] if @options[:subject]

  @jid = SecureRandom.hex(12)
end

Instance Attribute Details

#jid::String (readonly)

Returns the value of attribute jid.

Returns:

  • (::String)


14
15
16
# File 'lib/cosmo/job/data.rb', line 14

def jid
  @jid
end

Class Method Details

.default_retryInteger

Returns:

  • (Integer)


10
11
12
# File 'lib/cosmo/job/data.rb', line 10

def self.default_retry
  Config[:max_retries] || DEFAULTS[:retry]
end

Instance Method Details

#as_json{ jid: ::String, class: ::String, args: Array[untyped], retry: Integer, dead: bool }

Returns:

  • ({ jid: ::String, class: ::String, args: Array[untyped], retry: Integer, dead: bool })


43
44
45
46
# File 'lib/cosmo/job/data.rb', line 43

def as_json
  json = { jid: jid, class: @class_name, args: @args, retry: retries, dead: dead }
  batch_id ? json.merge(batch_id: batch_id) : json
end

#batch_idObject



29
30
31
# File 'lib/cosmo/job/data.rb', line 29

def batch_id
  @options[:batch_id]
end

#deadBoolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/cosmo/job/data.rb', line 75

def dead
  @options[:dead].nil? ? DEFAULTS[:dead] : @options[:dead]
end

#retriesInteger

Returns:

  • (Integer)


68
69
70
71
72
73
# File 'lib/cosmo/job/data.rb', line 68

def retries
  return self.class.default_retry if @options[:retry].nil?
  return 0 if @options[:retry] == false

  @options[:retry]
end

#stream(target: false) ⇒ Symbol

Parameters:

  • target: (Boolean) (defaults to: false)

Returns:

  • (Symbol)


33
34
35
36
37
# File 'lib/cosmo/job/data.rb', line 33

def stream(target: false)
  return @options[:stream] if target

  @at ? :scheduled : @options[:stream]
end

#subject(target: false) ⇒ Array[::String]

Parameters:

  • target: (Boolean) (defaults to: false)

Returns:

  • (Array[::String])


39
40
41
# File 'lib/cosmo/job/data.rb', line 39

def subject(target: false)
  ["jobs", stream(target:).to_s, Utils::String.underscore(@class_name)]
end

#to_args[::String, ::String, Hash[Symbol, untyped]]

Returns:

  • ([::String, ::String, Hash[Symbol, untyped]])


52
53
54
55
56
57
58
59
60
# File 'lib/cosmo/job/data.rb', line 52

def to_args
  headers = { "Nats-Msg-Id" => jid }
  if @at
    headers.merge!("X-Execute-At" => @at.to_i,
                   "X-Stream" => stream(target: true),
                   "X-Subject" => subject(target: true).join("."))
  end
  [@subject || subject.join("."), to_json, { stream: stream, header: headers }]
end

#to_json(*_args) ⇒ ::String?

Parameters:

  • _args (Object)

Returns:

  • (::String, nil)


48
49
50
# File 'lib/cosmo/job/data.rb', line 48

def to_json(*_args)
  Utils::Json.dump(as_json)
end

#validate!void

This method returns an undefined value.

Raises:



64
65
66
# File 'lib/cosmo/job/data.rb', line 64

def validate!
  raise ArgumentError, "stream is not provided" unless @options[:stream]
end