Skip to content

Commit fbe3764

Browse files
committed
chore: Add attempts field to job options in BackendsService
1 parent 4d7e73a commit fbe3764

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

src/core/backends/backends.service.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
HttpException,
44
HttpStatus,
55
Injectable,
6+
RequestTimeoutException,
67
UnprocessableEntityException,
78
} from '@nestjs/common';
89
import { ModuleRef } from '@nestjs/core';
@@ -12,6 +13,7 @@ import { IdentityState } from '~/management/identities/_enums/states.enum';
1213
import { Identities } from '~/management/identities/_schemas/identities.schema';
1314
import { IdentitiesService } from '~/management/identities/identities.service';
1415
import { JobState } from '../jobs/_enums/state.enum';
16+
import { JobState as StateOfJob } from 'bullmq';
1517
import { Jobs } from '../jobs/_schemas/jobs.schema';
1618
import { JobsService } from '../jobs/jobs.service';
1719
import { Tasks } from '../tasks/_schemas/tasks.schema';
@@ -218,7 +220,10 @@ export class BackendsService extends AbstractQueueProcessor {
218220
concernedTo,
219221
payload,
220222
},
221-
options?.job,
223+
{
224+
...options?.job,
225+
attempts: 1,
226+
},
222227
);
223228
const optionals = {};
224229
if (!options?.async) {
@@ -255,8 +260,17 @@ export class BackendsService extends AbstractQueueProcessor {
255260

256261
if (!options?.async) {
257262
let error: Error;
263+
258264
try {
259265
const response = await job.waitUntilFinished(this.queueEvents, options.syncTimeout || DEFAULT_SYNC_TIMEOUT);
266+
267+
if (response?.status > 0) {
268+
const jobError: Error & { response: any } = new Error() as unknown as Error & { response: any };
269+
jobError.response = response;
270+
271+
throw jobError;
272+
}
273+
260274
let jobStoreUpdated: ModifyResult<Query<Jobs, Jobs>> = null;
261275

262276
if (!options?.disableLogs) {
@@ -280,9 +294,12 @@ export class BackendsService extends AbstractQueueProcessor {
280294

281295
return [jobStoreUpdated as unknown as Jobs, response];
282296
} catch (err) {
297+
this.logger.error(`Error while executing job ${job.id}`, err);
283298
error = err;
284299
}
285300

301+
const stateOfJob = await job.getState();
302+
286303
let jobFailed: ModifyResult<Query<Jobs, Jobs>> = null;
287304
if (!options?.disableLogs) {
288305
jobFailed = await this.jobsService.update<Jobs>(jobStore._id, {
@@ -297,29 +314,42 @@ export class BackendsService extends AbstractQueueProcessor {
297314
},
298315
});
299316
}
317+
300318
if (concernedTo && !!options?.updateStatus) {
301319
await this.identitiesService.model.findByIdAndUpdate(concernedTo, {
302320
$set: {
303321
state: IdentityState.ON_ERROR,
304322
},
305323
});
306324
}
307-
if (options?.timeoutDiscard !== false) {
325+
326+
if (options?.timeoutDiscard && stateOfJob !== 'completed' && stateOfJob !== 'failed') {
308327
job.discard();
309-
throw new BadRequestException({
310-
status: HttpStatus.BAD_REQUEST,
328+
329+
throw new RequestTimeoutException({
330+
status: HttpStatus.REQUEST_TIMEOUT,
311331
message: `Sync job ${job.id} failed to finish in time`,
312332
error,
313333
job: jobFailed as unknown as Jobs,
314334
});
315335
}
316-
throw new UnprocessableEntityException({
317-
status: HttpStatus.UNPROCESSABLE_ENTITY,
318-
message: `Job now continue to run in background ${job.id}, timeout wait until finish reached`,
336+
337+
if (error && stateOfJob !== 'completed' && stateOfJob !== 'failed') {
338+
throw new UnprocessableEntityException({
339+
status: HttpStatus.UNPROCESSABLE_ENTITY,
340+
message: `Job now continue to run in background ${job.id}, timeout wait until finish reached`,
341+
error,
342+
job: jobFailed as unknown as Jobs,
343+
});
344+
}
345+
346+
throw new BadRequestException({
347+
status: HttpStatus.BAD_REQUEST,
319348
error,
320-
job: jobFailed as unknown as Jobs,
349+
job: (error as any).response,
321350
});
322351
}
352+
323353
return [jobStore?.toObject() || null, null];
324354
}
325355
}

src/management/passwd/passwd.service.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { InjectRedis } from '@nestjs-modules/ioredis';
2-
import { BadRequestException, Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
2+
import { BadRequestException, HttpStatus, Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
33
import * as crypto from 'crypto';
44
import Redis from 'ioredis';
55
import { AbstractService } from '~/_common/abstracts/abstract.service';
@@ -55,8 +55,26 @@ export class PasswdService extends AbstractService {
5555
updateStatus: false,
5656
});
5757
} catch (e) {
58+
let job = undefined;
59+
let _debug = undefined;
5860
this.logger.error("Error while changing password. " + e + ` (uid=${passwdDto?.uid})`);
59-
throw new BadRequestException('Une erreur est survenue : Mot de passe incorrect ou utilisateur inconnu');
61+
62+
if (e?.response?.statusCode === HttpStatus.BAD_REQUEST) {
63+
job = {};
64+
job['status'] = e?.response?.job?.status;
65+
}
66+
67+
if (process.env.NODE_ENV === 'development') {
68+
_debug = e?.response?.error?.response;
69+
}
70+
71+
throw new BadRequestException({
72+
message: 'Une erreur est survenue : Mot de passe incorrect ou utilisateur inconnu',
73+
error: "Bad Request",
74+
statusCode: 400,
75+
job,
76+
_debug,
77+
});
6078
}
6179
}
6280

0 commit comments

Comments
 (0)