diff --git a/lib/worker/checkForJobs.js b/lib/worker/checkForJobs.js index 592b448..8fbc219 100644 --- a/lib/worker/checkForJobs.js +++ b/lib/worker/checkForJobs.js @@ -106,21 +106,43 @@ function checkForJobs(_a) { output = null; _e.label = 2; case 2: - _e.trys.push([2, 4, , 5]); + _e.trys.push([2, 4, , 6]); return [4 /*yield*/, processingFunction(job)]; case 3: output = _e.sent(); debug("Job's done", job.id); - return [3 /*break*/, 5]; + return [3 /*break*/, 6]; case 4: err_1 = _e.sent(); debug('Error during the job processing', err_1); - return [3 /*break*/, 5]; + return [4 /*yield*/, client.mutate({ + mutation: updateJobQuery, + variables: { + job: { + id: job.id, + status: 'failed', + output: "" + JSON.stringify(err_1) + } + } + })]; case 5: - debug('Updating job'); - _e.label = 6; + _e.sent(); + if (looping) { + return [2 /*return*/, checkForJobs({ + processingFunction: processingFunction, + uri: uri, + typeList: typeList, + workerId: workerId, + looping: looping, + loopTime: loopTime + })]; + } + return [2 /*return*/, err_1]; case 6: - _e.trys.push([6, 8, , 9]); + debug('Updating job'); + _e.label = 7; + case 7: + _e.trys.push([7, 9, , 10]); return [4 /*yield*/, client.mutate({ mutation: updateJobQuery, variables: { @@ -131,7 +153,7 @@ function checkForJobs(_a) { } } })]; - case 7: + case 8: result = _e.sent(); if (looping) { return [2 /*return*/, checkForJobs({ @@ -144,11 +166,11 @@ function checkForJobs(_a) { })]; } return [2 /*return*/, result.data.job]; - case 8: + case 9: err_2 = _e.sent(); debug('Failed to update the success status of the current job.', err_2); - return [3 /*break*/, 9]; - case 9: return [2 /*return*/]; + return [3 /*break*/, 10]; + case 10: return [2 /*return*/]; } }); }); diff --git a/src/worker/checkForJobs.ts b/src/worker/checkForJobs.ts index 80751b8..0038134 100644 --- a/src/worker/checkForJobs.ts +++ b/src/worker/checkForJobs.ts @@ -106,7 +106,28 @@ export default async function checkForJobs({ debug("Job's done", job.id) } catch (err) { debug('Error during the job processing', err) - // @todo update job status. + await client.mutate({ + mutation: updateJobQuery, + variables: { + job: { + id: job.id, + status: 'failed', + output: `${JSON.stringify(err)}` + } + } + }) + + if (looping) { + return checkForJobs({ + processingFunction, + uri, + typeList, + workerId, + looping, + loopTime + }) + } + return err } debug('Updating job') diff --git a/tests/__snapshots__/job.spec.js.snap b/tests/__snapshots__/job.spec.js.snap index e9d636d..f63f222 100644 --- a/tests/__snapshots__/job.spec.js.snap +++ b/tests/__snapshots__/job.spec.js.snap @@ -88,3 +88,5 @@ Object { "type": "b", } `; + +exports[`Test the job endpoint checkForJobs declare jobs as failed when an error is raised. 1`] = `[TypeError: a.awd is not a function]`; diff --git a/tests/job.spec.js b/tests/job.spec.js index fe47f4a..3f1fd85 100644 --- a/tests/job.spec.js +++ b/tests/job.spec.js @@ -176,6 +176,27 @@ describe('Test the job endpoint', () => { expect(jobEntity.endedAt).not.toBe(null) }) + it('checkForJobs declare jobs as failed when an error is raised.', async () => { + const job = await checkForJobs({ + typeList: ['a'], + uri, + processingFunction: async () => { + const a = {} + a.awd() + return a + }, + looping: false + }) + expect(job).not.toBeUndefined() + expect(job).not.toBe(null) + expect(job).toMatchSnapshot() + + const jobEntity = await models.job.findOne({ where: { id: 1 } }) + expect(jobEntity.startedAt).not.toBe(null) + expect(jobEntity.endedAt).not.toBe(null) + expect(jobEntity.status).toBe('failed') + }) + it('One can create a job of a given type.', async () => { const response = await request(server) .post('/graphql')