Skip to content

Commit

Permalink
fix(#32): moved retries to level of executing the job query
Browse files Browse the repository at this point in the history
  • Loading branch information
JanssenBrm committed Jan 23, 2024
1 parent 0fe63a9 commit fd821c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
10 changes: 6 additions & 4 deletions src/jobs/services/database/database.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,17 @@ describe('DatabaseService', () => {
});

it('should retry getting the job ID if the request failed', async () => {
const getJobID = jest.spyOn(service, 'queryJobs').mockResolvedValue([]);
const getJobID = jest
.spyOn(service, 'executeJobQuery')
.mockResolvedValue([]);

const id = await service.getJobDocId('foobar');
expect(getJobID).toBeCalledTimes(5);
expect(id).toBeUndefined();
});

it('should add the deleted filter by default when querying jobs', async () => {
jest.spyOn(esService, 'search').mockReturnValueOnce({
jest.spyOn(esService, 'search').mockReturnValue({
body: {
hits: {
hits: [],
Expand All @@ -145,11 +147,11 @@ describe('DatabaseService', () => {
.mockReturnValueOnce(QUERIES[1].input);

await service.queryJobs(QUERIES[1].input);
expect(deletedFilter).toBeCalledTimes(1);
expect(deletedFilter).toBeCalledTimes(5);
});

it('should not add the deleted filter when querying jobs and deleted flag is set to true', async () => {
jest.spyOn(esService, 'search').mockReturnValueOnce({
jest.spyOn(esService, 'search').mockReturnValue({
body: {
hits: {
hits: [],
Expand Down
58 changes: 40 additions & 18 deletions src/jobs/services/database/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,39 @@ export class DatabaseService {
deleted?: boolean,
limit?: number,
idsOnly?: boolean,
): Promise<Job[] | string[]> {
let retries = 1;

while (retries <= 5) {
const results = await this.executeJobQuery(
query,
deleted,
limit,
idsOnly,
);

if (results.length > 0) {
return results;
} else {
await UtilsService.sleep(500);
retries++;
}
}
return [];
}
/**
* Execute a query on the jobs index.
* @param query - ElasticSearch query to execute
* @param deleted - Flag indicating if the deleted docs should be included (true) or not (false)
* @param limit - The amount of documents to fetch (optional)
* @param idsOnly - Only return the IDs of the document
*/
/* istanbul ignore next */
public async executeJobQuery(
query: any,
deleted?: boolean,
limit?: number,
idsOnly?: boolean,
): Promise<Job[] | string[]> {
const queue = [];
let jobs: Job[] = [];
Expand Down Expand Up @@ -132,24 +165,13 @@ export class DatabaseService {
* @param jobId - Job ID to search for
*/
async getJobDocId(jobId: string): Promise<string> {
let retries = 1;
let id;
while (retries <= 5) {
const ids: string[] = (await this.queryJobs(
this.getJobQuery(jobId),
false,
1,
true,
)) as string[];
id = ids.length > 0 ? ids[0] : undefined;
if (id) {
return id;
} else {
await UtilsService.sleep(500);
retries++;
}
}
return undefined;
const ids: string[] = (await this.queryJobs(
this.getJobQuery(jobId),
false,
1,
true,
)) as string[];
return ids.length > 0 ? ids[0] : undefined;
}

/**
Expand Down

0 comments on commit fd821c2

Please sign in to comment.