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

Add debug warning message when a job is enqueued within a batch/bulk capture but not to the GoodJob Adapter #1339

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions lib/good_job/active_job_extensions/batches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ module ActiveJobExtensions
module Batches
extend ActiveSupport::Concern

included do
before_enqueue do |job|
GoodJob.logger.debug("#{job.class} was enqueued within a batch or bulk capture block but is not using the GoodJob Adapter; the job will not appear in GoodJob.") if (GoodJob::Bulk.current_buffer || GoodJob::Batch.current_batch_id) && !job.class.queue_adapter.is_a?(GoodJob::Adapter)
end
end

def batch
@_batch ||= CurrentThread.execution&.batch&.to_batch if CurrentThread.execution.present? && CurrentThread.execution.active_job_id == job_id
end
Expand Down
32 changes: 32 additions & 0 deletions spec/lib/good_job/active_job_extensions/batches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,36 @@ def perform
expect(RESULTS).to eq []
end
end

describe "enequeue" do
context 'when job does not have GoodJob Adapter' do
before do
allow(GoodJob.logger).to receive(:debug).and_call_original

stub_const("TestJob", Class.new(ActiveJob::Base) do
include GoodJob::ActiveJobExtensions::Batches
self.queue_adapter = :inline

def perform
nil
end
end)
end

it 'warns when enqueued in a bulk capture block' do
GoodJob::Bulk.capture { TestJob.perform_later }
expect(GoodJob.logger).to have_received(:debug).with(/TestJob was enqueued within a batch or bulk capture block but is not using the GoodJob Adapter; the job will not appear in GoodJob./)
end

it 'warns when enqueued in a batch capture block' do
GoodJob::Batch.enqueue { TestJob.perform_later }
expect(GoodJob.logger).to have_received(:debug).with(/TestJob was enqueued within a batch or bulk capture block but is not using the GoodJob Adapter; the job will not appear in GoodJob./)
end

it 'warns when directly added to a batch' do
GoodJob::Batch.enqueue(TestJob.new)
expect(GoodJob.logger).to have_received(:debug).with(/TestJob was enqueued within a batch or bulk capture block but is not using the GoodJob Adapter; the job will not appear in GoodJob./)
end
end
end
end