Skip to content

Commit

Permalink
feat(Job): Add support for before and after lifecycle methods
Browse files Browse the repository at this point in the history
A `before` and/or an `after` method can be defined on the Job instance.
This will be called as a lifecycle method either `before` or `after`
the job is ran.
  • Loading branch information
elpete committed Feb 12, 2024
1 parent 0c939ca commit 8cf8390
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions models/Providers/AbstractQueueProvider.cfc
Expand Up @@ -109,6 +109,9 @@ component accessors="true" {
}

beforeJobRun( job );
if ( structKeyExists( job, "before" ) ) {
job.before();
}

variables.interceptorService.announce( "onCBQJobMarshalled", { "job" : job } );

Expand Down Expand Up @@ -153,6 +156,9 @@ component accessors="true" {
}
);

if ( structKeyExists( job, "after" ) ) {
job.after();
}
afterJobRun( job );

ensureSuccessfulBatchJobIsRecorded( job );
Expand Down
6 changes: 6 additions & 0 deletions models/Providers/SyncProvider.cfc
Expand Up @@ -41,6 +41,9 @@ component accessors="true" extends="AbstractQueueProvider" {
}

beforeJobRun( arguments.job );
if ( structKeyExists( job, "before" ) ) {
job.before();
}

variables.interceptorService.announce( "onCBQJobMarshalled", { "job" : arguments.job } );

Expand Down Expand Up @@ -83,6 +86,9 @@ component accessors="true" extends="AbstractQueueProvider" {
}
);

if ( structKeyExists( job, "after" ) ) {
job.after();
}
afterJobRun( job );

ensureSuccessfulBatchJobIsRecorded( job );
Expand Down
28 changes: 28 additions & 0 deletions tests/resources/AbstractQueueProviderSpec.cfc
Expand Up @@ -2,6 +2,11 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {

function run() {
describe( "queue provider - #getProviderMapping()#", function() {
beforeEach( function() {
structDelete( application, "jobBeforeCalled" );
structDelete( application, "jobAfterCalled" );
} );

it( "can manually release a job back on to a queue with a given delay", function() {
var provider = getProvider();
$spy( provider, "releaseJob" );
Expand Down Expand Up @@ -78,6 +83,29 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
var callLog = provider.$callLog()[ "releaseJob" ][ 1 ];
expect( provider.getBackoffForJob( callLog[ 1 ], callLog[ 2 ] ) ).toBe( 2, "The delay [2] should have been passed to the provider" );
} );

it( "calls a before and after method if present as lifecycle methods", () => {
var provider = getProvider();
var workerPool = makeWorkerPool( provider );
var job = getInstance( "BeforeAndAfterJob" )
.setId( randRange( 1, 1000 ) );

param application.jobBeforeCalled = false;
param application.jobAfterCalled = false;

expect( application.jobBeforeCalled ).toBeFalse();
expect( application.jobAfterCalled ).toBeFalse();

// work the job
var jobFuture = provider.marshalJob( job, workerPool );
// if it is an async operation, wait for it to finish
if ( !isNull( jobFuture ) ) {
jobFuture.get();
}

expect( application.jobBeforeCalled ).toBeTrue( "The before method should have been called" );
expect( application.jobAfterCalled ).toBeTrue( "The after method should have been called" );
} );
} );
}

Expand Down
15 changes: 15 additions & 0 deletions tests/resources/app/models/Jobs/BeforeAndAfterJob.cfc
@@ -0,0 +1,15 @@
component extends="cbq.models.Jobs.AbstractJob" {

function handle() {
// do nothing
}

function before() {
application.jobBeforeCalled = true;
}

function after() {
application.jobAfterCalled = true;
}

}

0 comments on commit 8cf8390

Please sign in to comment.