Sha256: c8ab26215f37e880618bf3186edee1575b65884c39f8556b40b37accc9b8bb43

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'cloudtasker/worker'

module Cloudtasker
  # 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: https://github.com/rails/rails/issues/36363
  #
  class WorkerWrapper
    include Worker

    attr_accessor :worker_name

    #
    # Build a new instance of the class.
    #
    # @param [String] worker_class The name of the worker class.
    # @param [Hash] **opts The worker arguments.
    #
    def initialize(worker_name:, **opts)
      @worker_name = worker_name
      super(**opts)
    end

    #
    # Override parent. Return the underlying worker class name.
    #
    # @return [String] The worker class.
    #
    def job_class_name
      worker_name
    end

    #
    # Return a new instance of the worker with the same args and metadata
    # but with a different id.
    #
    # @return [Cloudtasker::WorkerWrapper] <description>
    #
    def new_instance
      self.class.new(worker_name: worker_name, job_queue: job_queue, job_args: job_args, job_meta: job_meta)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cloudtasker-0.14.rc1 lib/cloudtasker/worker_wrapper.rb
cloudtasker-0.13.2 lib/cloudtasker/worker_wrapper.rb
cloudtasker-0.13.1 lib/cloudtasker/worker_wrapper.rb
cloudtasker-0.13.0 lib/cloudtasker/worker_wrapper.rb
cloudtasker-0.13.rc2 lib/cloudtasker/worker_wrapper.rb
cloudtasker-0.13.rc1 lib/cloudtasker/worker_wrapper.rb