class MultiBackgroundJob::Worker

Attributes

arguments[R]
options[R]
payload[R]
unique_job[R]
worker_class[R]

Public Class Methods

coerce(service:, payload:, **opts) click to toggle source
# File lib/multi_background_job/worker.rb, line 18
def self.coerce(service:, payload:, **opts)
  SERVICES.fetch(service).coerce_to_worker(payload, **opts)
end
new(worker_class, **options) click to toggle source
# File lib/multi_background_job/worker.rb, line 11
def initialize(worker_class, **options)
  @worker_class = worker_class
  @options = options
  @payload = {}
  unique(@options.delete(:uniq)) if @options.key?(:uniq)
end

Public Instance Methods

==(other)
Alias for: eql?
at(timestamp)
Alias for: in
eql?(other) click to toggle source
# File lib/multi_background_job/worker.rb, line 100
def eql?(other)
  return false unless other.is_a?(self.class)

  worker_class == other.worker_class && \
    payload == other.payload &&
    options == other.options &&
    unique_job == other.unique_job
end
Also aliased as: ==
in(timestamp) click to toggle source

Schedule the time when a job will be executed. Jobs which are scheduled in the past are enqueued for immediate execution. @param timestamp [Numeric] timestamp, numeric or something that acts numeric. @return self

# File lib/multi_background_job/worker.rb, line 48
def in(timestamp)
  now = Time.now.to_f
  timestamp = Time.parse(timestamp) if timestamp.is_a?(String)
  int = timestamp.respond_to?(:strftime) ? timestamp.to_f : now + timestamp.to_f
  return self if int <= now

  @payload['at'] = int
  @payload['created_at'] = now

  self
end
Also aliased as: at
push(to: nil) click to toggle source

@param :to [Symbol] Adapter key @return Response of service @see MultiBackgroundJob::Adapters::** for more details

# File lib/multi_background_job/worker.rb, line 87
def push(to: nil)
  to ||= options[:service]
  unless SERVICES.include?(to)
    raise Error, format('Service %<to>p is not implemented. Please use one of %<list>p', to: to, list: SERVICES.keys)
  end

  @payload['created_at'] ||= Time.now.to_f
  worker_to_push = with_job_jid
  MultiBackgroundJob.config.middleware.invoke(worker_to_push, to) do
    SERVICES[to].push(worker_to_push)
  end
end
unique(value) click to toggle source

Wrap uniq options

@param value [Hash] Unique configurations with `across`, `timeout` and `unlock_policy` @return self

# File lib/multi_background_job/worker.rb, line 65
def unique(value)
  value = {} if value == true
  @unique_job = \
    case value
    when Hash then UniqueJob.coerce(value)
    when UniqueJob then value
    else
      nil
    end

  self
end
unique_job?() click to toggle source
# File lib/multi_background_job/worker.rb, line 110
def unique_job?
  unique_job.is_a?(UniqueJob)
end
with_args(*args) click to toggle source

Adds arguments to the job @return self

# File lib/multi_background_job/worker.rb, line 39
def with_args(*args)
  @payload['args'] = args

  self
end
with_job_jid(jid = nil) click to toggle source
# File lib/multi_background_job/worker.rb, line 78
def with_job_jid(jid = nil)
  @payload['jid'] ||= jid || MultiBackgroundJob.jid

  self
end