Skip to content

Commit

Permalink
feat(checkForJob): Handle the failed status automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentdesmares committed Jan 3, 2020
1 parent 848d53d commit e248c53
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
42 changes: 32 additions & 10 deletions lib/worker/checkForJobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -131,7 +153,7 @@ function checkForJobs(_a) {
}
}
})];
case 7:
case 8:
result = _e.sent();
if (looping) {
return [2 /*return*/, checkForJobs({
Expand All @@ -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*/];
}
});
});
Expand Down
23 changes: 22 additions & 1 deletion src/worker/checkForJobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/job.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]`;
21 changes: 21 additions & 0 deletions tests/job.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit e248c53

Please sign in to comment.