deleteTidbDataServiceEndpointAndWait function
Future<void>
deleteTidbDataServiceEndpointAndWait(
- TidbDataServiceApi api, {
- required String appId,
- required String endpointName,
- Duration pollInterval = const Duration(seconds: 2),
- int maxAttempts = 30,
Deletes one endpoint and waits for its automatic deployment to finish.
Implementation
Future<void> deleteTidbDataServiceEndpointAndWait(
TidbDataServiceApi api, {
required String appId,
required String endpointName,
Duration pollInterval = const Duration(seconds: 2),
int maxAttempts = 30,
}) async {
final deploymentsPath = "dataApps/$appId/deployments";
Future<Map<String, dynamic>> latestDeployment() async {
final response = await api.dataService(
"GET",
deploymentsPath,
query: {"pageSize": "1"},
);
final deployments = response["deployments"];
if (deployments is! List || deployments.isEmpty) {
return const {};
}
final latest = deployments.first;
if (latest is! Map) {
return const {};
}
return latest.map(
(key, value) => MapEntry(key.toString(), value),
);
}
final previousDeployment = await latestDeployment();
final previousName = previousDeployment["name"]?.toString() ?? "";
await api.dataService("DELETE", endpointName);
for (var attempt = 0; attempt < maxAttempts; attempt++) {
final deployment = await latestDeployment();
final name = deployment["name"]?.toString() ?? "";
final status = deployment["status"]?.toString().toLowerCase() ?? "";
if (name.isEmpty || name == previousName) {
await Future<void>.delayed(pollInterval);
continue;
}
if (status == "success") {
return;
}
if (status == "failed") {
throw StateError(
"TiDB Data Service endpoint deletion deployment failed: "
"${deployment["statusErrorMessage"]}",
);
}
await Future<void>.delayed(pollInterval);
}
throw StateError(
"Timed out waiting for the TiDB Data Service endpoint deletion "
"deployment.",
);
}