Skip to content

Commit

Permalink
Allow configuration of Rails queue adapter with :good_job (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon authored Jul 13, 2020
1 parent 651dda3 commit 75e1728
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 13 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ Inspired by [Delayed::Job](https://github.com/collectiveidea/delayed_job) and [Q
Add this line to your application's Gemfile:

```ruby
gem 'good_job', github: 'bensheldon/good_job'
gem 'good_job'
```

And then execute:
```bash
$ bundle
$ bundle install
```

## Usage
Expand Down Expand Up @@ -56,11 +56,21 @@ $ bundle

1. Configure the ActiveJob adapter:
```ruby
# config/environments/production.rb
config.active_job.queue_adapter = GoodJob::Adapter.new
# config/application.rb
config.active_job.queue_adapter = :good_job
```

By default, using `:good_job` is equivalent to manually configuring the adapter:

```ruby
# config/environments/development.rb
config.active_job.queue_adapter = GoodJob::Adapter.new(inline: true)
# config/environments/test.rb
config.active_job.queue_adapter = GoodJob::Adapter.new(inline: true)
# config/environments/production.rb
config.active_job.queue_adapter = GoodJob::Adapter.new
```

1. In production, the scheduler is designed to run in its own process:
Expand Down
13 changes: 13 additions & 0 deletions lib/active_job/queue_adapters/good_job_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ActiveJob
module QueueAdapters
class GoodJobAdapter < GoodJob::Adapter
def initialize
if Rails.env.development? || Rails.env.test?
super(inline: true)
else
super(inline: false)
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require 'good_job/adapter'
require 'good_job/pg_locks'

require 'active_job/queue_adapters/good_job_adapter'

module GoodJob
include Logging

Expand Down
2 changes: 0 additions & 2 deletions lib/good_job/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ def shutdown(wait: true) # rubocop:disable Lint/UnusedMethodArgument
nil
end

private

def inline?
@inline
end
Expand Down
43 changes: 43 additions & 0 deletions spec/active_job/queue_adapters/good_job_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rails_helper'

describe ActiveJob::QueueAdapters::GoodJobAdapter do
it 'inherits from GoodJob::Adapter' do
expect(described_class).to be < GoodJob::Adapter
end

it 'allows usage of ActiveJob symbol' do
ActiveJob::Base.queue_adapter = :good_job
expect(ActiveJob::Base.queue_adapter).to be_a described_class
end

describe '#initialize' do
before { allow(Rails.env).to receive(:test?).and_return(false) }

context 'when in development environment' do
before { allow(Rails.env).to receive(:development?).and_return(true) }

it 'runs inline' do
adapter = described_class.new
expect(adapter.inline?).to eq true
end
end

context 'when in test environment' do
before { allow(Rails.env).to receive(:test?).and_return(true) }

it 'runs inline' do
adapter = described_class.new
expect(adapter.inline?).to eq true
end
end

context 'when in production environment' do
before { allow(Rails.env).to receive(:production?).and_return(true) }

it 'runs in normal mode' do
adapter = described_class.new
expect(adapter.inline?).to eq false
end
end
end
end
7 changes: 1 addition & 6 deletions spec/good_job/scheduler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
require 'rails_helper'

RSpec.describe GoodJob::Scheduler do
around do |example|
original_adapter = ActiveJob::Base.queue_adapter
before do
ActiveJob::Base.queue_adapter = adapter
example.run
ActiveJob::Base.queue_adapter = original_adapter
end

before do
stub_const "RUN_JOBS", Concurrent::Array.new
stub_const "THREAD_JOBS", Concurrent::Hash.new(Concurrent::Array.new)

Expand Down
7 changes: 7 additions & 0 deletions spec/support/reset_rails_queue_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RSpec.configure do |config|
config.around do |example|
original_adapter = ActiveJob::Base.queue_adapter
example.run
ActiveJob::Base.queue_adapter = original_adapter
end
end

0 comments on commit 75e1728

Please sign in to comment.