-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathContainerStatusMonitor.cs
More file actions
264 lines (235 loc) · 11.3 KB
/
ContainerStatusMonitor.cs
File metadata and controls
264 lines (235 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright 2022 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.IO.Abstractions;
using Ardalis.GuardClauses;
using Docker.DotNet.Models;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Monai.Deploy.Messaging.API;
using Monai.Deploy.Messaging.Events;
using Monai.Deploy.Messaging.Messages;
using Monai.Deploy.Storage.API;
using Monai.Deploy.WorkflowManager.Common.Configuration;
using Monai.Deploy.WorkflowManager.TaskManager.API;
using Monai.Deploy.WorkflowManager.TaskManager.Podman.Logging;
namespace Monai.Deploy.WorkflowManager.TaskManager.Podman
{
public interface IContainerStatusMonitor
{
Task Start(TaskDispatchEvent taskDispatchEvent,
TimeSpan containerTimeout,
string containerId,
ContainerVolumeMount intermediateVolumeMount,
IReadOnlyList<ContainerVolumeMount> outputVolumeMounts,
CancellationToken cancellationToken = default);
}
public class ContainerStatusMonitor : IContainerStatusMonitor, IDisposable
{
private readonly IOptions<WorkflowManagerOptions> _options;
private readonly IServiceScope _scope;
private readonly ILogger<ContainerStatusMonitor> _logger;
private readonly IFileSystem _fileSystem;
private bool _disposedValue;
public ContainerStatusMonitor(
IServiceScopeFactory serviceScopeFactory,
ILogger<ContainerStatusMonitor> logger,
IFileSystem fileSystem,
IOptions<WorkflowManagerOptions> options)
{
if (serviceScopeFactory is null)
{
throw new ArgumentNullException(nameof(serviceScopeFactory));
}
_scope = serviceScopeFactory.CreateScope();
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_options = options ?? throw new ArgumentNullException(nameof(options));
}
public async Task Start(
TaskDispatchEvent taskDispatchEvent,
TimeSpan containerTimeout,
string containerId,
ContainerVolumeMount intermediateVolumeMount,
IReadOnlyList<ContainerVolumeMount> outputVolumeMounts,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(taskDispatchEvent, nameof(taskDispatchEvent));
ArgumentNullException.ThrowIfNull(containerTimeout, nameof(containerTimeout));
ArgumentNullException.ThrowIfNullOrWhiteSpace(containerId, nameof(containerId));
var podmanClientFactory = _scope.ServiceProvider.GetService<IPodmanClientFactory>() ?? throw new ServiceNotFoundException(nameof(IPodmanClientFactory));
var dockerClient = podmanClientFactory.CreateClient(new Uri(taskDispatchEvent.TaskPluginArguments[Keys.BaseUrl]));
var pollingPeriod = TimeSpan.FromSeconds(1);
var timeToRetry = (int)containerTimeout.TotalSeconds;
var completed = false;
while (timeToRetry-- > 0)
{
try
{
var response = await dockerClient.Containers.InspectContainerAsync(containerId, cancellationToken).ConfigureAwait(false);
if (IsContainerCompleted(response.State))
{
completed = true;
break;
}
}
catch (Exception ex)
{
_logger.ErrorMonitoringContainerStatus(containerId, ex);
}
await Task.Delay(pollingPeriod, cancellationToken).ConfigureAwait(false);
}
if (completed)
{
try
{
await UploadOutputArtifacts(intermediateVolumeMount, outputVolumeMounts, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorMonitoringContainerStatus(containerId, ex);
throw;
}
try
{
await SendCallbackMessage(taskDispatchEvent, containerId).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorMonitoringContainerStatus(containerId, ex);
}
}
else
{
_logger.TimedOutMonitoringContainerStatus(containerId);
}
}
internal static bool IsContainerCompleted(ContainerState state)
{
if (Strings.DockerEndStates.Contains(state.Status, StringComparer.InvariantCultureIgnoreCase) &&
!string.IsNullOrWhiteSpace(state.FinishedAt))
{
return true;
}
return false;
}
private async Task UploadOutputArtifacts(ContainerVolumeMount intermediateVolumeMount, IReadOnlyList<ContainerVolumeMount> outputVolumeMounts, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(outputVolumeMounts, nameof(outputVolumeMounts));
var storageService = _scope.ServiceProvider.GetService<IStorageService>() ?? throw new ServiceNotFoundException(nameof(IStorageService));
var contentTypeProvider = _scope.ServiceProvider.GetService<IContentTypeProvider>() ?? throw new ServiceNotFoundException(nameof(IContentTypeProvider));
if (intermediateVolumeMount is not null)
{
await UploadOutputArtifacts(storageService, contentTypeProvider, intermediateVolumeMount.Source, intermediateVolumeMount.TaskManagerPath, cancellationToken).ConfigureAwait(false);
}
foreach (var output in outputVolumeMounts)
{
await UploadOutputArtifacts(storageService, contentTypeProvider, output.Source, output.TaskManagerPath, cancellationToken).ConfigureAwait(false);
}
}
private async Task UploadOutputArtifacts(IStorageService storageService, IContentTypeProvider contentTypeProvider, Messaging.Common.Storage destination, string artifactsPath, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(destination, nameof(destination));
ArgumentNullException.ThrowIfNullOrWhiteSpace(artifactsPath, nameof(artifactsPath));
IEnumerable<string> files;
try
{
files = _fileSystem.Directory.EnumerateFiles(artifactsPath, "*", SearchOption.AllDirectories);
}
catch (Exception ex)
{
throw new ContainerMonitorException("Directory doesn't exist or no permission to access the directory.", ex);
}
if (!files.Any())
{
_logger.NoFilesFoundForUpload(artifactsPath);
}
foreach (var file in files)
{
try
{
var relativePart = file.StartsWith(artifactsPath, StringComparison.Ordinal)
? file.Substring(artifactsPath.Length)
: file;
relativePart = relativePart.TrimStart('/');
var objectName = string.IsNullOrEmpty(destination.RelativeRootPath)
? relativePart
: destination.RelativeRootPath.TrimEnd('/') + "/" + relativePart;
_logger.UploadingFile(file, destination.Bucket, objectName);
if (!contentTypeProvider.TryGetContentType(file, out var contentType))
{
contentType = GetContentType(_fileSystem.Path.GetExtension(file));
}
_logger.ContentTypeForFile(objectName, contentType);
using var stream = _fileSystem.File.OpenRead(file);
await storageService.PutObjectAsync(destination.Bucket, objectName, stream, stream.Length, contentType, new Dictionary<string, string>(), cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorUploadingFile(file, ex);
throw;
}
}
}
private static string GetContentType(string? ext)
{
if (string.IsNullOrWhiteSpace(ext))
{
return Strings.MimeTypeUnknown;
}
return ext.ToLowerInvariant() switch
{
Strings.FileExtensionDicom => Strings.MimeTypeDicom,
_ => Strings.MimeTypeUnknown
};
}
private async Task SendCallbackMessage(TaskDispatchEvent taskDispatchEvent, string containerId)
{
ArgumentNullException.ThrowIfNull(taskDispatchEvent, nameof(taskDispatchEvent));
ArgumentNullException.ThrowIfNullOrWhiteSpace(containerId, nameof(containerId));
_logger.SendingCallbackMessage(containerId);
var message = new JsonMessage<TaskCallbackEvent>(new TaskCallbackEvent
{
CorrelationId = taskDispatchEvent.CorrelationId,
ExecutionId = taskDispatchEvent.ExecutionId,
Identity = containerId,
Outputs = taskDispatchEvent.Outputs ?? new List<Messaging.Common.Storage>(),
TaskId = taskDispatchEvent.TaskId,
WorkflowInstanceId = taskDispatchEvent.WorkflowInstanceId,
}, applicationId: Strings.ApplicationId, correlationId: taskDispatchEvent.CorrelationId);
var messageBrokerPublisherService = _scope.ServiceProvider.GetService<IMessageBrokerPublisherService>() ?? throw new ServiceNotFoundException(nameof(IMessageBrokerPublisherService));
await messageBrokerPublisherService.Publish(_options.Value.Messaging.Topics.TaskCallbackRequest, message.ToMessage()).ConfigureAwait(false);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_scope.Dispose();
}
_disposedValue = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}