Class: ActiveJob::QueueAdapters::SidekiqAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_job/queue_adapters/sidekiq_adapter.rb

Overview

Sidekiq adapter for Active Job

To use Sidekiq set the queue_adapter config to :sidekiq.

Rails.application.config.active_job.queue_adapter = :sidekiq

Defined Under Namespace

Classes: JobWrapper

Instance Method Summary collapse

Instance Method Details

#enqueue(job) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
38
39
40
# File 'lib/active_job/queue_adapters/sidekiq_adapter.rb', line 35

def enqueue(job)
  job.provider_job_id = JobWrapper.set(
    wrapped: job.class,
    queue: job.queue_name
  ).perform_async(job.serialize)
end

#enqueue_after_transaction_commit?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines whether enqueuing should happen implicitly to after commit when called from inside a transaction.

Returns:

  • (Boolean)


30
31
32
# File 'lib/active_job/queue_adapters/sidekiq_adapter.rb', line 30

def enqueue_after_transaction_commit?
  true
end

#enqueue_all(jobs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_job/queue_adapters/sidekiq_adapter.rb', line 51

def enqueue_all(jobs)
  enqueued_count = 0
  jobs.group_by(&:class).each do |job_class, same_class_jobs|
    same_class_jobs.group_by(&:queue_name).each do |queue, same_class_and_queue_jobs|
      immediate_jobs, scheduled_jobs = same_class_and_queue_jobs.partition { |job| job.scheduled_at.nil? }

      if immediate_jobs.any?
        jids = Sidekiq::Client.push_bulk(
          "class" => JobWrapper,
          "wrapped" => job_class,
          "queue" => queue,
          "args" => immediate_jobs.map { |job| [job.serialize] }
        )
        enqueued_count += jids.compact.size
      end

      if scheduled_jobs.any?
        jids = Sidekiq::Client.push_bulk(
          "class" => JobWrapper,
          "wrapped" => job_class,
          "queue" => queue,
          "args" => scheduled_jobs.map { |job| [job.serialize] },
          "at" => scheduled_jobs.map { |job| job.scheduled_at&.to_f }
        )
        enqueued_count += jids.compact.size
      end
    end
  end
  enqueued_count
end

#enqueue_at(job, timestamp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
47
48
# File 'lib/active_job/queue_adapters/sidekiq_adapter.rb', line 43

def enqueue_at(job, timestamp)
  job.provider_job_id = JobWrapper.set(
    wrapped: job.class,
    queue: job.queue_name
  ).perform_at(timestamp, job.serialize)
end