Skip to content

Commit

Permalink
fix: remove not found container from db
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Sep 2, 2022
1 parent 3b49588 commit aadf8e9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions GZCTF/Services/ContainerChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ContainerChecker(IServiceScopeFactory provider, ILogger<ContainerChecker>

public Task StartAsync(CancellationToken cancellationToken)
{
timer = new Timer(Execute, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
timer = new Timer(Execute, null, TimeSpan.Zero, TimeSpan.FromMinutes(3));
logger.SystemLog("容器生命周期检查已启动", TaskStatus.Success, LogLevel.Debug);
return Task.CompletedTask;
}
Expand All @@ -32,9 +32,9 @@ private async void Execute(object? state)

foreach (var container in await containerRepo.GetDyingContainers())
{
logger.SystemLog($"移除到期容器 [{container.ContainerId[..12]}]");
await containerService.DestoryContainer(container);
await containerRepo.RemoveContainer(container);
logger.SystemLog($"移除到期容器 [{container.ContainerId[..12]}]");
}
}

Expand Down
8 changes: 8 additions & 0 deletions GZCTF/Services/K8sService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ public async Task DestoryContainer(Container container, CancellationToken token
await kubernetesClient.CoreV1.DeleteNamespacedServiceAsync(container.ContainerId, "gzctf", cancellationToken: token);
await kubernetesClient.CoreV1.DeleteNamespacedPodAsync(container.ContainerId, "gzctf", cancellationToken: token);
}
catch(HttpOperationException e)
{
if (e.Response.StatusCode == HttpStatusCode.NotFound)
{
container.Status = ContainerStatus.Destoryed;
return;
}
}
catch (Exception e)
{
logger.LogError(e, "删除容器失败");
Expand Down

0 comments on commit aadf8e9

Please sign in to comment.