Class: Cloudtasker::WorkerWrapper

Inherits:
Object
  • Object
show all
Includes:
Worker
Defined in:
lib/cloudtasker/worker_wrapper.rb

Overview

A worker class used to schedule jobs without actually instantiating the worker class. This is useful for middlewares needing to enqueue jobs in a Rails initializer. Rails 6 complains about instantiating workers in an iniitializer because of autoloading in zeitwerk mode.

Downside of this wrapper: any cloudtasker_options specified on on the worker_class will be ignored.

See: github.com/rails/rails/issues/36363

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Worker

#==, #arguments_missing?, clear_all, #dispatch_deadline, drain_all, #execute, from_hash, from_json, included, #job_dead?, #job_duration, #job_max_retries, #job_must_die?, #job_queue, #logger, #reenqueue, #run_callback, #schedule, #schedule_time, #to_h, #to_json

Constructor Details

#initialize(worker_name:, **opts) ⇒ WorkerWrapper

Build a new instance of the class.

Parameters:

  • worker_class (String)

    The name of the worker class.

  • **opts (Hash)

    The worker arguments.



28
29
30
31
# File 'lib/cloudtasker/worker_wrapper.rb', line 28

def initialize(worker_name:, **opts)
  @worker_name = worker_name
  super(**opts)
end

Instance Attribute Details

#worker_nameObject

Returns the value of attribute worker_name.



20
21
22
# File 'lib/cloudtasker/worker_wrapper.rb', line 20

def worker_name
  @worker_name
end

Instance Method Details

#job_class_nameString

Override parent. Return the underlying worker class name.

Returns:

  • (String)

    The worker class.



38
39
40
# File 'lib/cloudtasker/worker_wrapper.rb', line 38

def job_class_name
  worker_name
end

#new_instanceCloudtasker::WorkerWrapper

Return a new instance of the worker with the same args and metadata but with a different id.

Returns:



48
49
50
# File 'lib/cloudtasker/worker_wrapper.rb', line 48

def new_instance
  self.class.new(worker_name: worker_name, job_queue: job_queue, job_args: job_args, job_meta: job_meta)
end