Class: Cosmo::Job::Data
- Inherits:
-
Object
- Object
- Cosmo::Job::Data
- Defined in:
- lib/cosmo/job/data.rb,
sig/cosmo/job/data.rbs
Constant Summary collapse
- DEFAULTS =
{ stream: :default, retry: 3, dead: true, limit: nil }.freeze
Instance Attribute Summary collapse
-
#jid ⇒ ::String
readonly
Returns the value of attribute jid.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ { jid: ::String, class: ::String, args: Array[untyped], retry: Integer, dead: bool }
- #batch_id ⇒ Object
- #dead ⇒ Boolean
-
#initialize(class_name, args, options = nil) ⇒ Data
constructor
A new instance of Data.
- #retries ⇒ Integer
- #stream(target: false) ⇒ Symbol
- #subject(target: false) ⇒ Array[::String]
- #to_args ⇒ [::String, ::String, Hash[Symbol, untyped]]
- #to_json(*_args) ⇒ ::String?
- #validate! ⇒ void
Constructor Details
#initialize(class_name, args, options = nil) ⇒ Data
Returns a new instance of Data.
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, = nil) @class_name = class_name @args = args @options = Hash() 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.
14 15 16 |
# File 'lib/cosmo/job/data.rb', line 14 def jid @jid end |
Class Method Details
Instance Method Details
#as_json ⇒ { 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_id ⇒ Object
29 30 31 |
# File 'lib/cosmo/job/data.rb', line 29 def batch_id @options[:batch_id] end |
#dead ⇒ Boolean
75 76 77 |
# File 'lib/cosmo/job/data.rb', line 75 def dead @options[:dead].nil? ? DEFAULTS[:dead] : @options[:dead] end |
#retries ⇒ 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
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]
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]]
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?
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.
64 65 66 |
# File 'lib/cosmo/job/data.rb', line 64 def validate! raise ArgumentError, "stream is not provided" unless @options[:stream] end |