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

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)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cosmo/job/data.rb', line 12

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)


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

def jid
  @jid
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 })


35
36
37
38
39
40
41
42
43
# File 'lib/cosmo/job/data.rb', line 35

def as_json
  {
    jid: jid,
    class: @class_name,
    args: @args,
    retry: retries,
    dead: dead
  }
end

#deadBoolean

Returns:

  • (Boolean)


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

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

#retriesInteger, bool

Returns:

  • (Integer, bool)


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

def retries
  @options[:retry].nil? ? DEFAULTS[:retry] : @options[:retry]
end

#stream(target: false) ⇒ Symbol

Parameters:

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

Returns:

  • (Symbol)


25
26
27
28
29
# File 'lib/cosmo/job/data.rb', line 25

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])


31
32
33
# File 'lib/cosmo/job/data.rb', line 31

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]])


49
50
51
52
53
54
55
56
57
# File 'lib/cosmo/job/data.rb', line 49

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)


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

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

#validate!void

This method returns an undefined value.

Raises:



61
62
63
# File 'lib/cosmo/job/data.rb', line 61

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