-
Notifications
You must be signed in to change notification settings - Fork 367
Better mechanism for running delayed jobs inline #4700
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
Merged
philippthun
merged 1 commit into
cloudfoundry:main
from
sap-contributions:inline-mechanism-dj
Dec 15, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,10 @@ def perform | |
| end | ||
| end | ||
|
|
||
| def inline? | ||
| true | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def logger | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| module VCAP::CloudController | ||
| module Jobs | ||
| class InlineRunner < Enqueuer | ||
| class << self | ||
| def setup | ||
| Delayed::Worker.delay_jobs = lambda do |job| | ||
| unwrapped_job = unwrap_job(job.payload_object) | ||
| !(unwrapped_job.respond_to?(:inline?) && unwrapped_job.inline?) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def run(job) | ||
| raise ArgumentError.new("job must define a method 'inline?' which returns 'true'") unless job.respond_to?(:inline?) && job.inline? | ||
|
|
||
| Delayed::Job.enqueue(TimeoutJob.new(job, job_timeout(job)), @opts) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| require 'spec_helper' | ||
| require 'cloud_controller/clock/inline_runner' | ||
|
|
||
| ########### Note ########### | ||
| # This test modifies the global setting Delayed::Worker.delay_jobs which might affect other tests running in parallel | ||
| # It is recommended to run this test separately | ||
| ############################ | ||
|
|
||
| module VCAP::CloudController | ||
| RSpec.describe Jobs::InlineRunner, job_context: :clock do | ||
| before(:all) do | ||
| @original_delay_jobs = Delayed::Worker.delay_jobs | ||
| end | ||
|
|
||
| after(:all) do | ||
| Delayed::Worker.delay_jobs = @original_delay_jobs | ||
| end | ||
|
|
||
| before do | ||
| Delayed::Worker.delay_jobs = nil | ||
| end | ||
|
|
||
| describe '#setup' do | ||
| it 'sets up delay_jobs' do | ||
| expect(Delayed::Worker.delay_jobs).to be_nil | ||
|
|
||
| Jobs::InlineRunner.setup | ||
|
|
||
| expect(Delayed::Worker.delay_jobs).to be_instance_of(Proc) | ||
| end | ||
| end | ||
|
|
||
| describe '#run' do | ||
| let(:job) { double('inline_job', inline?: true, perform: nil) } | ||
| let(:timeout) { 123 } | ||
| let(:opts) { { queue: 'queue' } } | ||
|
|
||
| before do | ||
| Jobs::InlineRunner.setup | ||
| end | ||
|
|
||
| it 'calls Delayed::Job.enqueue with the job wrapped in a TimeoutJob' do | ||
| expect_any_instance_of(Jobs::InlineRunner).to receive(:job_timeout).with(job).and_return(timeout) | ||
| expect(Jobs::TimeoutJob).to receive(:new).with(job, timeout).and_call_original | ||
| expect(Delayed::Job).to receive(:enqueue).with(instance_of(Jobs::TimeoutJob), opts).and_call_original | ||
| expect(Delayed::Worker).to receive(:delay_job?).and_wrap_original do |method, *args| | ||
| result = method.call(*args) | ||
| expect(result).to be(false) | ||
| result | ||
| end | ||
| expect(job).to receive(:perform) | ||
|
|
||
| Jobs::InlineRunner.new(opts).run(job) | ||
| end | ||
|
|
||
| context 'when the job does not define inline?' do | ||
| let(:job) { double('non_inline_job') } | ||
|
|
||
| it 'raises an ArgumentError' do | ||
| expect { Jobs::InlineRunner.new.run(job) }.to raise_error(ArgumentError, "job must define a method 'inline?' which returns 'true'") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.