diff --git a/packages/vendure-plugin-google-cloud-tasks/CHANGELOG.md b/packages/vendure-plugin-google-cloud-tasks/CHANGELOG.md index 1cb8bee7..9aa46b4e 100644 --- a/packages/vendure-plugin-google-cloud-tasks/CHANGELOG.md +++ b/packages/vendure-plugin-google-cloud-tasks/CHANGELOG.md @@ -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)) diff --git a/packages/vendure-plugin-google-cloud-tasks/package.json b/packages/vendure-plugin-google-cloud-tasks/package.json index 8a1342ef..4aeafe40 100644 --- a/packages/vendure-plugin-google-cloud-tasks/package.json +++ b/packages/vendure-plugin-google-cloud-tasks/package.json @@ -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 ", "homepage": "https://pinelab-plugins.com/", diff --git a/packages/vendure-plugin-google-cloud-tasks/src/cloud-tasks.handler.ts b/packages/vendure-plugin-google-cloud-tasks/src/cloud-tasks.handler.ts index 1b2948f0..60f6cba9 100644 --- a/packages/vendure-plugin-google-cloud-tasks/src/cloud-tasks.handler.ts +++ b/packages/vendure-plugin-google-cloud-tasks/src/cloud-tasks.handler.ts @@ -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( @@ -134,7 +144,6 @@ 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( @@ -142,8 +151,8 @@ export class CloudTasksHandler implements OnApplicationBootstrap { CloudTasksPlugin.loggerCtx ); res.sendStatus(500); - return; } + return; } } diff --git a/packages/vendure-plugin-google-cloud-tasks/src/types.ts b/packages/vendure-plugin-google-cloud-tasks/src/types.ts index 86bea3c7..26143266 100644 --- a/packages/vendure-plugin-google-cloud-tasks/src/types.ts +++ b/packages/vendure-plugin-google-cloud-tasks/src/types.ts @@ -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; /** * Optional suffix, I.E. for differentiating between test, acc and prod queues */ diff --git a/packages/vendure-plugin-google-cloud-tasks/test/e2e.spec.ts b/packages/vendure-plugin-google-cloud-tasks/test/e2e.spec.ts index 37c38870..01e867ef 100644 --- a/packages/vendure-plugin-google-cloud-tasks/test/e2e.spec.ts +++ b/packages/vendure-plugin-google-cloud-tasks/test/e2e.spec.ts @@ -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);