Module: Cosmo::Job::ClassMethods
- Defined in:
- lib/cosmo/job.rb,
sig/cosmo/job.rbs
Instance Method Summary collapse
- #client ⇒ Client
-
#concurrency_key(args) ⇒ ::String?
Derive the fully-scoped concurrency key for a given args array.
-
#concurrency_options ⇒ { limit: Integer, key: Proc?, duration: Integer }?
Returns a normalized concurrency config hash, or
nilwhen not configured. - #default_options ⇒ Hash[Symbol, untyped]
- #limits_concurrency? ⇒ Boolean
- #options(**config) ⇒ Hash[Symbol, untyped] (also: #cosmo_options)
- #perform(*args, async: true, **options) ⇒ ::String?
- #perform_async(*args) ⇒ ::String
- #perform_at(timestamp, *args) ⇒ ::String
- #perform_in(interval, *args) ⇒ ::String
- #perform_sync(*args) ⇒ void
-
#publish(data, batch) ⇒ void
The batch is reserved a pending slot before we know the publish will succeed (must happen in that order -- see Batch#jobs).
-
#retry_in(_data = nil) ⇒ Proc?
Returns the
retry_inProc/lambda (taking(count, exception)) configured for this job class, ornilwhen unset.
Instance Method Details
#client ⇒ Client
145 146 147 |
# File 'lib/cosmo/job.rb', line 145 def client @client ||= Client.instance end |
#concurrency_key(args) ⇒ ::String?
Derive the fully-scoped concurrency key for a given args array.
87 88 89 90 91 92 93 94 |
# File 'lib/cosmo/job.rb', line 87 def concurrency_key(args) config = return unless config base = Utils::String.underscore(name) suffix = config[:key]&.call(*args) suffix ? "#{base}/#{suffix}" : base end |
#concurrency_options ⇒ { limit: Integer, key: Proc?, duration: Integer }?
Returns a normalized concurrency config hash, or nil when not configured.
Always contains :limit, :key, :duration, and :retry_in.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cosmo/job.rb', line 73 def value = .dig(:limit, :concurrency) return unless value duration = .dig(:limit, :duration).to_i retry_in = .dig(:limit, :retry_in)&.to_i || (duration / 2) case value when Integer then { limit: value, key: nil, duration: duration, retry_in: retry_in } when Hash then { limit: value.fetch(:to), key: value[:key], duration: duration, retry_in: retry_in } end end |
#default_options ⇒ Hash[Symbol, untyped]
139 140 141 |
# File 'lib/cosmo/job.rb', line 139 def @default_options ||= (superclass.respond_to?(:default_options) ? superclass. : Data::DEFAULTS.merge(retry: Data.default_retry)).dup end |
#limits_concurrency? ⇒ Boolean
60 61 62 |
# File 'lib/cosmo/job.rb', line 60 def limits_concurrency? !! end |
#options(**config) ⇒ Hash[Symbol, untyped] Also known as: cosmo_options
49 50 51 52 53 54 55 56 57 |
# File 'lib/cosmo/job.rb', line 49 def (**config) if config[:limit] && config.dig(:limit, :concurrency) && !config.dig(:limit, :duration).to_i.positive? raise ArgumentError, "limit: duration is required when concurrency is set" end raise ArgumentError, "retry_in must be callable, e.g. ->(count, exception) { ... }" if config[:retry_in] && !config[:retry_in].respond_to?(:call) .merge!(config) end |
#perform(*args, async: true, **options) ⇒ ::String?
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cosmo/job.rb', line 96 def perform(*args, async: true, **) batch = Batch.current if async [:batch_id] = batch.bid if batch data = Data.new(name, args, .merge()) unless async payload = Utils::Json.parse(data.to_args[1]) raise ArgumentError, "Cannot parse payload" unless payload new.perform(*payload[:args]) return end publish(data, batch) end |
#perform_async(*args) ⇒ ::String
123 124 125 |
# File 'lib/cosmo/job.rb', line 123 def perform_async(*args) perform(*args) end |
#perform_at(timestamp, *args) ⇒ ::String
127 128 129 |
# File 'lib/cosmo/job.rb', line 127 def perform_at(, *args) perform(*args, at: ) end |
#perform_in(interval, *args) ⇒ ::String
131 132 133 |
# File 'lib/cosmo/job.rb', line 131 def perform_in(interval, *args) perform(*args, in: interval) end |
#perform_sync(*args) ⇒ void
This method returns an undefined value.
135 136 137 |
# File 'lib/cosmo/job.rb', line 135 def perform_sync(*args) perform(*args, async: false) end |
#publish(data, batch) ⇒ void
This method returns an undefined value.
The batch is reserved a pending slot before we know the publish will succeed (must happen in that order -- see Batch#jobs). Roll it back if it never actually made it onto the stream, so the batch doesn't hang waiting for a completion that will never arrive.
115 116 117 118 119 120 121 |
# File 'lib/cosmo/job.rb', line 115 def publish(data, batch) batch&.register_job! Publisher.publish_job(data) rescue StandardError batch&.rollback_job! raise end |
#retry_in(_data = nil) ⇒ Proc?
Returns the retry_in Proc/lambda (taking (count, exception)) configured for this job class, or
nil when unset. Overridable by wrapper job classes (e.g. the ActiveJob executor) that need to
resolve it from something other than self.
67 68 69 |
# File 'lib/cosmo/job.rb', line 67 def retry_in(_data = nil) [:retry_in] end |