Module: Wurk::Worker::ClassMethods
- Defined in:
- lib/wurk/worker.rb
Overview
rubocop:disable Metrics/ModuleLength
Instance Method Summary collapse
- #build_client ⇒ Object
- #clear ⇒ Object
- #client_push(item) ⇒ Object
- #delay ⇒ Object (also: #delay_for, #delay_until)
-
#drain ⇒ Object
Run & remove every fake job for this class — including ones it enqueues mid-drain.
- #execute_job(worker, args) ⇒ Object
-
#get_sidekiq_options ⇒ Object
Sidekiq’s public API name — wire-compat sacred.
- #inherited(subclass) ⇒ Object
-
#jobs ⇒ Object
Fake jobs enqueued for this class, across every queue.
- #perform_async ⇒ Object
- #perform_bulk(items) ⇒ Object
- #perform_in(interval) ⇒ Object (also: #perform_at)
- #perform_inline ⇒ Object (also: #perform_sync)
-
#perform_one ⇒ Object
Run & remove the first fake job for this class; EmptyQueueError if none.
-
#process_job(job_hash) ⇒ Object
Execute a normalized job hash through the inline server-middleware chain (empty by default — see Wurk::Testing.server_middleware).
-
#queue ⇒ Object
— Sidekiq::Testing class-level helpers (spec §24.3) ————– Only meaningful in :fake / :inline mode; the in-memory store is empty otherwise.
- #queue_as(queue) ⇒ Object
- #set(opts) ⇒ Object
- #sidekiq_options(opts = {}) ⇒ Object
- #sidekiq_options_hash ⇒ Object
- #sidekiq_retries_exhausted(&block) ⇒ Object
- #sidekiq_retry_in(&block) ⇒ Object
Instance Method Details
#build_client ⇒ Object
123 124 125 126 |
# File 'lib/wurk/worker.rb', line 123 def build_client pool = ['pool'] Wurk::Client.new(pool: pool) end |
#clear ⇒ Object
141 142 143 |
# File 'lib/wurk/worker.rb', line 141 def clear ::Wurk::Queues.clear_class(to_s) end |
#client_push(item) ⇒ Object
117 118 119 120 121 |
# File 'lib/wurk/worker.rb', line 117 def client_push(item) raise ArgumentError, "Job arguments to #{name || self} must have string keys" if symbol_keyed?(item) build_client.push(item) end |
#delay ⇒ Object Also known as: delay_for, delay_until
182 183 184 |
# File 'lib/wurk/worker.rb', line 182 def delay(*) raise ArgumentError, "#{name || self}.delay is removed in Sidekiq 7+. Use #{name || 'klass'}.perform_async." end |
#drain ⇒ Object
Run & remove every fake job for this class — including ones it enqueues mid-drain. Returns the count processed.
147 148 149 150 151 152 153 154 |
# File 'lib/wurk/worker.rb', line 147 def drain count = 0 while (job = ::Wurk::Queues.shift_class(to_s)) process_job(job) count += 1 end count end |
#execute_job(worker, args) ⇒ Object
178 179 180 |
# File 'lib/wurk/worker.rb', line 178 def execute_job(worker, args) worker.perform(*args) end |
#get_sidekiq_options ⇒ Object
Sidekiq’s public API name — wire-compat sacred. Must stay ‘get_sidekiq_options`.
75 76 77 |
# File 'lib/wurk/worker.rb', line 75 def # rubocop:disable Naming/AccessorMethodName @sidekiq_options_hash ||= # rubocop:disable Naming/MemoizedInstanceVariableName end |
#inherited(subclass) ⇒ Object
188 189 190 191 192 193 |
# File 'lib/wurk/worker.rb', line 188 def inherited(subclass) super subclass.instance_variable_set(:@sidekiq_options_hash, .dup) inherit_block(subclass, :@sidekiq_retry_in_block) inherit_block(subclass, :@sidekiq_retries_exhausted_block) end |
#jobs ⇒ Object
Fake jobs enqueued for this class, across every queue.
137 138 139 |
# File 'lib/wurk/worker.rb', line 137 def jobs ::Wurk::Queues.jobs_by_class[to_s] || [] end |
#perform_async ⇒ Object
95 96 97 |
# File 'lib/wurk/worker.rb', line 95 def perform_async(*) Wurk::Worker::Setter.new(self, {}).perform_async(*) end |
#perform_bulk(items) ⇒ Object
109 110 111 |
# File 'lib/wurk/worker.rb', line 109 def perform_bulk(items, **) Wurk::Worker::Setter.new(self, {}).perform_bulk(items, **) end |
#perform_in(interval) ⇒ Object Also known as: perform_at
104 105 106 |
# File 'lib/wurk/worker.rb', line 104 def perform_in(interval, *) Wurk::Worker::Setter.new(self, {}).perform_in(interval, *) end |
#perform_inline ⇒ Object Also known as: perform_sync
99 100 101 |
# File 'lib/wurk/worker.rb', line 99 def perform_inline(*) new.perform(*) end |
#perform_one ⇒ Object
Run & remove the first fake job for this class; EmptyQueueError if none.
157 158 159 160 161 162 |
# File 'lib/wurk/worker.rb', line 157 def perform_one job = ::Wurk::Queues.shift_class(to_s) raise ::Wurk::Testing::EmptyQueueError, "no #{self} jobs were found" if job.nil? process_job(job) end |
#process_job(job_hash) ⇒ Object
Execute a normalized job hash through the inline server-middleware chain (empty by default — see Wurk::Testing.server_middleware). Returns the value of the server-middleware ‘invoke` (i.e. the worker’s ‘perform` return), matching Sidekiq::Testing — so `perform_one` yields the job result.
169 170 171 172 173 174 175 176 |
# File 'lib/wurk/worker.rb', line 169 def process_job(job_hash) instance = new instance.jid = job_hash['jid'] instance.bid = job_hash['bid'] if instance.respond_to?(:bid=) ::Wurk::Testing.server_middleware.invoke(instance, job_hash, job_hash['queue'] || queue) do execute_job(instance, job_hash['args']) end end |
#queue ⇒ Object
— Sidekiq::Testing class-level helpers (spec §24.3) ————– Only meaningful in :fake / :inline mode; the in-memory store is empty otherwise.
132 133 134 |
# File 'lib/wurk/worker.rb', line 132 def queue ['queue'] end |
#queue_as(queue) ⇒ Object
83 84 85 |
# File 'lib/wurk/worker.rb', line 83 def queue_as(queue) ('queue' => queue.to_s) end |
#set(opts) ⇒ Object
113 114 115 |
# File 'lib/wurk/worker.rb', line 113 def set(opts) Wurk::Worker::Setter.new(self, opts) end |
#sidekiq_options(opts = {}) ⇒ Object
69 70 71 72 |
# File 'lib/wurk/worker.rb', line 69 def (opts = {}) merged = .merge(opts.transform_keys(&:to_s)) @sidekiq_options_hash = merged end |
#sidekiq_options_hash ⇒ Object
79 80 81 |
# File 'lib/wurk/worker.rb', line 79 def end |
#sidekiq_retries_exhausted(&block) ⇒ Object
91 92 93 |
# File 'lib/wurk/worker.rb', line 91 def sidekiq_retries_exhausted(&block) self.sidekiq_retries_exhausted_block = block end |
#sidekiq_retry_in(&block) ⇒ Object
87 88 89 |
# File 'lib/wurk/worker.rb', line 87 def sidekiq_retry_in(&block) self.sidekiq_retry_in_block = block end |