Skip to content

Commit

Permalink
feat(clients): cleanup after replaceAllObjects failure [skip-bc] (gen…
Browse files Browse the repository at this point in the history
…erated)

algolia/api-clients-automation#3824

Co-authored-by: algolia-bot <[email protected]>
Co-authored-by: Pierre Millot <[email protected]>
Co-authored-by: Thomas Raffray <[email protected]>
  • Loading branch information
3 people committed Dec 31, 2024
1 parent 12b40f5 commit af3bd60
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/Bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ body:
id: client
attributes:
label: Client
description: Which API are you targetting?
description: Which API are you targeting?
options:
- All
- AB testing
Expand Down
90 changes: 48 additions & 42 deletions Sources/Search/Extra/SearchClientExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,51 +559,57 @@ public extension SearchClient {
) async throws -> ReplaceAllObjectsResponse {
let tmpIndexName = "\(indexName)_tmp_\(Int.random(in: 1_000_000 ..< 10_000_000))"

var copyOperationResponse = try await operationIndex(
indexName: indexName,
operationIndexParams: OperationIndexParams(
operation: .copy,
destination: tmpIndexName,
scope: [.settings, .rules, .synonyms]
),
requestOptions: requestOptions
)
do {
var copyOperationResponse = try await operationIndex(
indexName: indexName,
operationIndexParams: OperationIndexParams(
operation: .copy,
destination: tmpIndexName,
scope: [.settings, .rules, .synonyms]
),
requestOptions: requestOptions
)

let batchResponses = try await self.chunkedBatch(
indexName: tmpIndexName,
objects: objects,
waitForTasks: true,
batchSize: batchSize,
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
let batchResponses = try await self.chunkedBatch(
indexName: tmpIndexName,
objects: objects,
waitForTasks: true,
batchSize: batchSize,
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)

copyOperationResponse = try await operationIndex(
indexName: indexName,
operationIndexParams: OperationIndexParams(
operation: .copy,
destination: tmpIndexName,
scope: [.settings, .rules, .synonyms]
),
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)

let moveOperationResponse = try await self.operationIndex(
indexName: tmpIndexName,
operationIndexParams: OperationIndexParams(
operation: .move,
destination: indexName
),
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)
copyOperationResponse = try await operationIndex(
indexName: indexName,
operationIndexParams: OperationIndexParams(
operation: .copy,
destination: tmpIndexName,
scope: [.settings, .rules, .synonyms]
),
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)

return ReplaceAllObjectsResponse(
copyOperationResponse: copyOperationResponse,
batchResponses: batchResponses,
moveOperationResponse: moveOperationResponse
)
let moveOperationResponse = try await self.operationIndex(
indexName: tmpIndexName,
operationIndexParams: OperationIndexParams(
operation: .move,
destination: indexName
),
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)

return ReplaceAllObjectsResponse(
copyOperationResponse: copyOperationResponse,
batchResponses: batchResponses,
moveOperationResponse: moveOperationResponse
)
} catch {
_ = try? await self.deleteIndex(indexName: tmpIndexName)

throw error
}
}

/// Generate a secured API key
Expand Down

0 comments on commit af3bd60

Please sign in to comment.