Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ActiveJob adapter #354

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions lib/active_job/queue_adapters/queue_classic_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

if defined?(ActiveJob) && ActiveJob.version >= "8.0.0.alpha"
module ActiveJob
module QueueAdapters
# = queue_classic adapter for Active Job
#
# To use queue_classic set the queue_adapter config to +:queue_classic+.
#
# Rails.application.config.active_job.queue_adapter = :queue_classic
class QueueClassicAdapter < AbstractAdapter
def initialize(enqueue_after_transaction_commit: false)
@enqueue_after_transaction_commit = enqueue_after_transaction_commit
end

def enqueue_after_transaction_commit? # :nodoc:
@enqueue_after_transaction_commit
end

def enqueue(job) # :nodoc:
qc_job = build_queue(job.queue_name).enqueue("#{JobWrapper.name}.perform", job.serialize)
job.provider_job_id = qc_job["id"] if qc_job.is_a?(Hash)
qc_job
end

def enqueue_at(job, timestamp) # :nodoc:
queue = build_queue(job.queue_name)
unless queue.respond_to?(:enqueue_at)
raise NotImplementedError, "To be able to schedule jobs with queue_classic " \
"the QC::Queue needs to respond to `enqueue_at(timestamp, method, *args)`. " \
"You can implement this yourself or you can use the queue_classic-later gem."
end
qc_job = queue.enqueue_at(timestamp, "#{JobWrapper.name}.perform", job.serialize)
job.provider_job_id = qc_job["id"] if qc_job.is_a?(Hash)
qc_job
end

# Builds a +QC::Queue+ object to schedule jobs on.
#
# If you have a custom +QC::Queue+ subclass you'll need to subclass
# +ActiveJob::QueueAdapters::QueueClassicAdapter+ and override the
# <tt>build_queue</tt> method.
def build_queue(queue_name)
QC::Queue.new(queue_name)
end

class JobWrapper # :nodoc:
class << self
def perform(job_data)
Base.execute job_data
end
end
end
end
end
end
end