Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search-indexer): Wait before doing another request to Elasticsearch in case of 429 status code #16215

Merged
merged 6 commits into from
Sep 30, 2024
70 changes: 45 additions & 25 deletions libs/cms/src/lib/search/contentful.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,37 +483,57 @@ export class ContentfulService {
let response: ApiResponse<SearchResponse<MappedData>> | null = null
let total = -1

let delay = 500
let retries = 3

while (response === null || items.length < total) {
response = await this.elasticService.findByQuery(elasticIndex, {
query: {
bool: {
should: idsChunk.map((id) => ({
nested: {
path: 'tags',
query: {
bool: {
must: [
{
term: {
'tags.key': id,
try {
response = await this.elasticService.findByQuery(elasticIndex, {
query: {
bool: {
should: idsChunk.map((id) => ({
nested: {
path: 'tags',
query: {
bool: {
must: [
{
term: {
'tags.key': id,
},
},
},
{
term: {
'tags.type': 'hasChildEntryWithId',
{
term: {
'tags.type': 'hasChildEntryWithId',
},
},
},
],
],
},
},
},
},
})),
minimum_should_match: 1,
})),
minimum_should_match: 1,
},
},
},
size,
from: (page - 1) * size,
})
size,
from: (page - 1) * size,
})
// Reset variables in case we successfully receive a response
delay = 500
retries = 2
} catch (error) {
if (error?.statusCode === 429 && retries > 0) {
await new Promise((resolve) => {
setTimeout(resolve, delay)
})
// Keep track of how often and for how long we should wait in case of failure
retries -= 1
delay *= 2
continue
} else {
throw error
}
}

if (response.body.hits.hits.length === 0) {
total = response.body.hits.total.value
Expand Down
Loading