class MultiBackgroundJob::Adapters::Sidekiq

This is a Sidekiq adapter that converts MultiBackgroundJob::Worker object into a sidekiq readable format and then push the jobs into the service.

Attributes

queue[R]
worker[R]

Public Class Methods

coerce_to_worker(payload, **options) click to toggle source

Coerces the raw payload into an instance of Worker @param payload [Hash] The job as json from redis @options options [Hash] list of options that will be passed along to the Worker instance @return [MultiBackgroundJob::Worker] and instance of MultiBackgroundJob::Worker

# File lib/multi_background_job/adapters/sidekiq.rb, line 26
def self.coerce_to_worker(payload, **options)
  raise(Error, 'invalid payload') unless payload.is_a?(Hash)
  raise(Error, 'invalid payload') unless payload['class'].is_a?(String)

  options[:retry] ||= payload['retry'] if payload.key?('retry')
  options[:queue] ||= payload['queue'] if payload.key?('queue')

  MultiBackgroundJob[payload['class'], **options].tap do |worker|
    worker.with_args(*Array(payload['args'])) if payload.key?('args')
    worker.with_job_jid(payload['jid']) if payload.key?('jid')
    worker.created_at(payload['created_at']) if payload.key?('created_at')
    worker.enqueued_at(payload['enqueued_at']) if payload.key?('enqueued_at')
    worker.at(payload['at']) if payload.key?('at')
    worker.unique(payload['uniq']) if payload.key?('uniq')
  end
end
new(worker) click to toggle source
# File lib/multi_background_job/adapters/sidekiq.rb, line 10
def initialize(worker)
  @worker = worker
  @queue = worker.options.fetch(:queue, 'default')

  @payload = worker.payload.merge(
    'class' => worker.worker_class,
    'retry' => worker.options.fetch(:retry, true),
    'queue' => @queue,
  )
  @payload['created_at'] ||= Time.now.to_f
end
push(worker) click to toggle source

Initializes adapter and push job into the sidekiq service

@param worker [MultiBackgroundJob::Worker] An instance of MultiBackgroundJob::Worker @return [Hash] Job payload @see push method for more details

# File lib/multi_background_job/adapters/sidekiq.rb, line 48
def self.push(worker)
  new(worker).push
end

Public Instance Methods

push() click to toggle source

Push sidekiq to the Sidekiq(Redis actually).

* If job has the 'at' key. Then schedule it
* Otherwise enqueue for immediate execution

@return [Hash] Payload that was sent to redis

# File lib/multi_background_job/adapters/sidekiq.rb, line 57
def push
  @payload['enqueued_at'] = Time.now.to_f
  # Optimization to enqueue something now that is scheduled to go out now or in the past
  if (timestamp = @payload.delete('at')) && (timestamp > Time.now.to_f)
    MultiBackgroundJob.redis_pool.with do |redis|
      redis.zadd(scheduled_queue_name, timestamp.to_f.to_s, to_json(@payload))
    end
  else
    MultiBackgroundJob.redis_pool.with do |redis|
      redis.lpush(immediate_queue_name, to_json(@payload))
    end
  end
  @payload
end

Protected Instance Methods

immediate_queue_name() click to toggle source
# File lib/multi_background_job/adapters/sidekiq.rb, line 82
def immediate_queue_name
  "#{namespace}:queue:#{queue}"
end
namespace() click to toggle source
# File lib/multi_background_job/adapters/sidekiq.rb, line 74
def namespace
  MultiBackgroundJob.config.redis_namespace
end
scheduled_queue_name() click to toggle source
# File lib/multi_background_job/adapters/sidekiq.rb, line 78
def scheduled_queue_name
  "#{namespace}:schedule"
end
to_json(value) click to toggle source
# File lib/multi_background_job/adapters/sidekiq.rb, line 86
def to_json(value)
  MultiJson.dump(value, mode: :compat)
end