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
Instance Method Details
#client ⇒ Client
108 109 110 |
# File 'lib/cosmo/job.rb', line 108 def client @client ||= Client.instance end |
#concurrency_key(args) ⇒ ::String?
Derive the fully-scoped concurrency key for a given args array.
64 65 66 67 68 69 70 71 |
# File 'lib/cosmo/job.rb', line 64 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.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cosmo/job.rb', line 50 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]
102 103 104 |
# File 'lib/cosmo/job.rb', line 102 def @default_options ||= (superclass.respond_to?(:default_options) ? superclass. : Data::DEFAULTS).dup end |
#limits_concurrency? ⇒ Boolean
44 45 46 |
# File 'lib/cosmo/job.rb', line 44 def limits_concurrency? !! end |
#options(**config) ⇒ Hash[Symbol, untyped] Also known as: cosmo_options
35 36 37 38 39 40 41 |
# File 'lib/cosmo/job.rb', line 35 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 .merge!(config) end |
#perform(*args, async: true, **options) ⇒ ::String?
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cosmo/job.rb', line 73 def perform(*args, async: true, **) 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 Publisher.publish_job(data) end |
#perform_async(*args) ⇒ ::String
86 87 88 |
# File 'lib/cosmo/job.rb', line 86 def perform_async(*args) perform(*args) end |
#perform_at(timestamp, *args) ⇒ ::String
90 91 92 |
# File 'lib/cosmo/job.rb', line 90 def perform_at(, *args) perform(*args, at: ) end |
#perform_in(interval, *args) ⇒ ::String
94 95 96 |
# File 'lib/cosmo/job.rb', line 94 def perform_in(interval, *args) perform(*args, in: interval) end |
#perform_sync(*args) ⇒ void
This method returns an undefined value.
98 99 100 |
# File 'lib/cosmo/job.rb', line 98 def perform_sync(*args) perform(*args, async: false) end |