Skip to content

Commit

Permalink
Merge pull request #403 from que-rb/support-rails-7.1
Browse files Browse the repository at this point in the history
Support Rails 7.1+ by adding ActiveJob Que adapter removed from Rails
  • Loading branch information
oeoeaio authored Oct 16, 2023
2 parents d58b3f4 + 0c66d03 commit 4360dff
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ jobs:
strategy:
matrix:
ruby_version: ['2.7', '3.0', '3.1', '3.2']
rails_gemfile: ['6.0', '6.1', '7.0']
rails_gemfile: ['6.0', '6.1', '7.0', '7.1']
postgres_version: ['14']
include:
# Postgres versions
- { ruby_version: '3.2', rails_gemfile: '7.0', postgres_version: '9' }
- { ruby_version: '3.2', rails_gemfile: '7.0', postgres_version: '10' }
- { ruby_version: '3.2', rails_gemfile: '7.0', postgres_version: '11' }
- { ruby_version: '3.2', rails_gemfile: '7.0', postgres_version: '12' }
- { ruby_version: '3.2', rails_gemfile: '7.0', postgres_version: '13' }
- { ruby_version: '3.2', rails_gemfile: '7.0', postgres_version: '14' }
- { ruby_version: '3.2', rails_gemfile: '7.1', postgres_version: '9' }
- { ruby_version: '3.2', rails_gemfile: '7.1', postgres_version: '10' }
- { ruby_version: '3.2', rails_gemfile: '7.1', postgres_version: '11' }
- { ruby_version: '3.2', rails_gemfile: '7.1', postgres_version: '12' }
- { ruby_version: '3.2', rails_gemfile: '7.1', postgres_version: '13' }
- { ruby_version: '3.2', rails_gemfile: '7.1', postgres_version: '14' }
exclude: []
name: "Test: Ruby ${{ matrix.ruby_version }}, Rails ${{ matrix.rails_gemfile }}, PostgreSQL ${{ matrix.postgres_version }}"
services:
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ source 'https://rubygems.org'
group :development, :test do
gem 'rake'

gem 'activerecord', '~> 6.0', require: nil
gem 'activejob', '~> 6.0', require: nil
gem 'activerecord', '~> 7.1.0', require: nil
gem 'activejob', '~> 7.1.0', require: nil
gem 'sequel', require: nil
gem 'connection_pool', require: nil
gem 'pond', require: nil
Expand Down
35 changes: 35 additions & 0 deletions lib/que/active_job/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,41 @@ def run(args)
end
end

# This is the ActiveJob Que adapter for Rails 7.1+, given ActiveJob::QueueAdapters::QueAdapter has been removed from ActiveJob.
# For backwards compatibility, the class name of ActiveJob::QueueAdapters::QueAdapter::JobWrapper must remain the same, given this string would be in old job records in the database
if ActiveJob.gem_version >= Gem::Version.new('7.1')
module ActiveJob
module QueueAdapters
# Work around `autoload QueAdapter` being left over in ActiveJob after the adapter was removed
remove_const(:QueAdapter) if const_defined?(:QueAdapter)

class QueAdapter
def enqueue(job)
job_options = { priority: job.priority, queue: job.queue_name }
que_job = JobWrapper.enqueue job.serialize, **job_options
job.provider_job_id = que_job.attrs["job_id"]
que_job
end

def enqueue_at(job, timestamp)
job_options = { priority: job.priority, queue: job.queue_name, run_at: Time.at(timestamp) }
que_job = JobWrapper.enqueue job.serialize, **job_options
job.provider_job_id = que_job.attrs["job_id"]
que_job
end

private

class JobWrapper < Que::Job
def run(job_data)
Base.execute job_data
end
end
end
end
end
end

class ActiveJob::QueueAdapters::QueAdapter
class JobWrapper < Que::Job
extend Que::ActiveJob::WrapperExtensions::ClassMethods
Expand Down
23 changes: 23 additions & 0 deletions spec/gemfiles/Gemfile-rails-7.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
source 'https://rubygems.org'

gem 'que', path: '../..'

group :development, :test do
gem 'rake'

gem 'activerecord', '~> 7.1.0', require: nil
gem 'activejob', '~> 7.1.0', require: nil
gem 'sequel', require: nil
gem 'connection_pool', require: nil
gem 'pond', require: nil
gem 'pg', require: nil, platform: :ruby
gem 'pg_jruby', require: nil, platform: :jruby
end

group :test do
gem 'minitest', '~> 5.10.1'
gem 'minitest-profile', '0.0.2'
gem 'minitest-hooks', '1.4.0'
gem 'pry'
gem 'pg_examiner', '~> 0.5.2'
end
8 changes: 4 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# Silence Ruby warnings.
$VERBOSE = nil

require 'que'

# ActiveRecord and ActiveJob require ActiveSupport, which affects a bunch of
# core classes and may change some behavior that we rely on, so only bring it in
# in some spec runs.
if ENV['USE_RAILS'] == 'true'
require 'active_record'
require 'active_job'

require 'que/active_job/extensions'

ActiveJob::Base.queue_adapter = :que
ActiveJob::Base.logger = nil

# require 'que/active_job/extensions'
end

require 'que'

# Libraries necessary for tests.
require 'uri'
require 'pg'
Expand Down

0 comments on commit 4360dff

Please sign in to comment.