Skip to content

Commit

Permalink
Merge pull request #262 from Pinelab-studio/feat/error-processing-hook
Browse files Browse the repository at this point in the history
feat: allow option to trigger additional actions when a processing er…
  • Loading branch information
martijnvdbrug authored Sep 26, 2023
2 parents 352344c + 180da13 commit 5b6d45f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/vendure-plugin-google-cloud-tasks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// TODO set correct version number + date and the changes you've made connected to the PR. See this example for the correct format: https://github.com/Pinelab-studio/pinelab-vendure-plugins/blob/main/packages/vendure-plugin-invoices/CHANGELOG.md
# 1.1.2 (2023-09-26)

- Added `onJobFailure` option to inspect errors from failed jobs([#262](https://github.com/Pinelab-studio/pinelab-vendure-plugins/pull/262))
2 changes: 1 addition & 1 deletion packages/vendure-plugin-google-cloud-tasks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinelab/vendure-plugin-google-cloud-tasks",
"version": "1.1.1",
"version": "1.1.2",
"description": "Vendure plugin for using worker jobs with Google Cloud Tasks",
"author": "Martijn van de Brug <[email protected]>",
"homepage": "https://pinelab-plugins.com/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ export class CloudTasksHandler implements OnApplicationBootstrap {
res.sendStatus(200);
return;
} catch (error: any) {
if (CloudTasksPlugin.options.onJobFailure) {
try {
await CloudTasksPlugin.options.onJobFailure(error);
} catch (e: any) {
Logger.error(
`Error in 'onJobFailure': ${e}`,
CloudTasksPlugin.loggerCtx
);
}
}
if (attempts === job.retries) {
// This was the final attempt, so mark the job as failed
Logger.error(
Expand All @@ -134,16 +144,15 @@ export class CloudTasksHandler implements OnApplicationBootstrap {
})
);
res.sendStatus(200); // Return 200 to prevent more retries
return;
} else {
// More attempts remain, so return 500 to trigger a retry
Logger.warn(
`Failed to handle message ${message.id} after ${attempts} attempts. Retrying... ${error}`,
CloudTasksPlugin.loggerCtx
);
res.sendStatus(500);
return;
}
return;
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/vendure-plugin-google-cloud-tasks/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export interface CloudTaskOptions {
projectId: string;
location: string;
authSecret: string;
/**
* Custom error handler for when a job fails.
* Useful for when you'd like to inspect specific errors in your project.
*/
onJobFailure?: (error: any) => void | Promise<void>;
/**
* Optional suffix, I.E. for differentiating between test, acc and prod queues
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/vendure-plugin-google-cloud-tasks/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ describe('CloudTasks job queue e2e', () => {
authSecret: 'some-secret',
queueSuffix: 'plugin-test',
defaultJobRetries: 50,
onJobFailure: async (error) => {
console.log('Custom error handler', error);
},
})
);
testConfig.plugins.push(DefaultSearchPlugin);
Expand Down

0 comments on commit 5b6d45f

Please sign in to comment.