-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractTask.cs
442 lines (401 loc) · 24.2 KB
/
AbstractTask.cs
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System;
namespace QarnotSDK {
/// <summary>
/// This class manges tasks life cycle: submission, monitor, delete.
/// </summary>
public abstract partial class AQTask {
/// <summary>
/// Reference to the api connection.
/// </summary>
protected Connection _api;
/// <summary>
/// The task resource uri.
/// </summary>
protected string _uri = null;
internal TaskApi _taskApi { get; set; }
/// <summary>
/// The inner Connection object.
/// </summary>
[InternalDataApiName(IsFilterable=false, IsSelectable=false)]
public virtual Connection Connection { get { return _api; } }
/// <summary>
/// The task unique identifier. The Uuid is generated by the Api when the task is submitted.
/// </summary>
[InternalDataApiName(Name="Uuid")]
public virtual Guid Uuid { get { return _taskApi.Uuid; } }
internal AQTask() { }
internal AQTask(Connection qapi, TaskApi taskApi) {
_api = qapi;
_uri = "tasks/" + taskApi.Uuid.ToString();
_taskApi = taskApi;
}
internal virtual async Task<AQTask> InitializeAsync(Connection qapi, TaskApi taskApi) {
_api = qapi;
_uri = "tasks/" + taskApi.Uuid.ToString();
_taskApi = taskApi;
return await Task.FromResult(this);
}
#region public methods
/// <summary>
/// Abort a running task.
/// </summary>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task AbortAsync(CancellationToken cancellationToken = default(CancellationToken)) {
if (_api.IsReadOnly) throw new Exception("Can't abort tasks, this connection is configured in read-only mode");
using (var response = await _api._client.PostAsync(_uri + "/abort", null, cancellationToken))
await Utils.LookForErrorAndThrowAsync(_api._client, response, cancellationToken);
}
/// <summary>
/// Delete the task. If the task is running, the task is aborted and deleted.
/// </summary>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <param name="failIfDoesntExist">If set to false and the task doesn't exist, no exception is thrown. Default is true.</param>
/// <param name="purgeResources">Boolean to trigger all resource storages deletion. Default is false.</param>
/// <param name="purgeResults">Boolean to trigger result storage deletion. Default is false.</param>
/// <returns></returns>
public abstract Task DeleteAsync(CancellationToken cancellationToken,
bool failIfDoesntExist = false, bool purgeResources=false, bool purgeResults=false);
/// <summary>
/// Delete the task. If the task is running, the task is aborted and deleted.
/// </summary>
/// <param name="failIfDoesntExist">If set to false and the task doesn't exist, no exception is thrown. Default is true.</param>
/// <param name="purgeResources">Boolean to trigger all resource storages deletion. Default is false.</param>
/// <param name="purgeResults">Boolean to trigger result storage deletion. Default is false.</param>
/// <returns></returns>
public virtual async Task DeleteAsync(bool failIfDoesntExist = false, bool purgeResources=false, bool purgeResults=false)
=> await DeleteAsync(default(CancellationToken), failIfDoesntExist, purgeResources, purgeResults);
#endregion
#region resource updates/snapshots
/// <summary>
/// Request made on a running task to re-sync the resource buckets to the compute nodes.
/// 1 - Upload new files on your resource bucket,
/// 2 - Call this method,
/// 3 - The new files will appear on all the compute nodes in the $DOCKER_WORKDIR folder
/// Note: There is no way to know when the files are effectively transfered. This information is available on the compute node only.
/// Note: The update is additive only: files deleted from the bucket will NOT be deleted from the task's resources directory.
/// </summary>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task UpdateResourcesAsync(CancellationToken cancellationToken = default(CancellationToken)) {
if (_api.IsReadOnly) throw new Exception("Can't update resources, this connection is configured in read-only mode");
var reqMsg = new HttpRequestMessage(new HttpMethod("PATCH"), _uri);
using (var response = await _api._client.SendAsync(reqMsg, cancellationToken))
await Utils.LookForErrorAndThrowAsync(_api._client, response, cancellationToken);
}
/// <summary>
/// Request made on a running task to sync the result files in $DOCKER_WORKDIR on the compute node to the result bucket.
/// Note: There is no way to know when all the results are effectively transfered. This information is available by monitoring the
/// task ResultsCount or by checking the result bucket.
/// </summary>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task SnapshotAsync(CancellationToken cancellationToken = default(CancellationToken)) {
await TriggerSnapshotAsync(cancellationToken: cancellationToken);
}
/// <summary>
/// Start a snapshotting of the results with customized parameters.
/// It can have a different whitelist, blacklist, bucket and bucket prefix than a normal snapshot.
/// </summary>
/// <param name="whitelist">Specify a custom whitelist for this snapshot.</param>
/// <param name="blacklist">Specify a custom blacklist for this snapshot.</param>
/// <param name="bucket">Specify the name of a custom bucket this snapshot.</param>
/// <param name="bucketPrefix">Specify a custom prefix for this snapshot.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task TriggerSnapshotAsync(string whitelist = null, string blacklist = null, QBucket bucket = null, string bucketPrefix = null, CancellationToken cancellationToken = default(CancellationToken)) {
if (_api._shouldSanitizeBucketPaths)
{
bucketPrefix = Utils.GetSanitizedBucketPath(bucketPrefix, _api._showBucketWarnings, false);
}
var s = new UniqueSnapshot()
{
Whitelist = whitelist,
Blacklist = blacklist,
Bucket = bucket?.UniqueId,
BucketPrefix = bucketPrefix,
};
if (_api.IsReadOnly) throw new Exception("Can't request a snapshot, this connection is configured in read-only mode");
using (var response = await _api._client.PostAsJsonAsync<UniqueSnapshot>(_uri + "/snapshot", s, cancellationToken))
await Utils.LookForErrorAndThrowAsync(_api._client, response, cancellationToken);
}
/// <summary>
/// Start a periodic snapshotting of the results.
/// </summary>
/// <param name="interval">Interval in seconds between two snapshots.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task SnapshotPeriodicAsync(uint interval, CancellationToken cancellationToken = default(CancellationToken)) {
await TriggerPeriodicSnapshotAsync(interval, cancellationToken: cancellationToken);
}
/// <summary>
/// Start a periodic snapshotting of the results with customized parameters.
/// It can have a different whitelist, blacklist, bucket and bucket prefix than a normal snapshot.
/// </summary>
/// <param name="interval">Interval in seconds between two snapshots.</param>
/// <param name="whitelist">Specify a custom whitelist for this periodic snapshot.</param>
/// <param name="blacklist">Specify a custom blacklist for this periodic snapshot.</param>
/// <param name="bucket">Specify the name of a custom bucket for this periodic snapshot.</param>
/// <param name="bucketPrefix">Specify a custom prefix for this periodic snapshot.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task TriggerPeriodicSnapshotAsync(uint interval, string whitelist = null, string blacklist = null, QBucket bucket = null, string bucketPrefix = null, CancellationToken cancellationToken = default(CancellationToken)) {
if (_api._shouldSanitizeBucketPaths)
{
bucketPrefix = Utils.GetSanitizedBucketPath(bucketPrefix, _api._showBucketWarnings, false);
}
var s = new PeriodicSnapshot()
{
Interval = Convert.ToInt32(interval),
Whitelist = whitelist,
Blacklist = blacklist,
Bucket = bucket?.UniqueId,
BucketPrefix = bucketPrefix,
};
if (_api.IsReadOnly) throw new Exception("Can't configure snapshots, this connection is configured in read-only mode");
using (var response = await _api._client.PostAsJsonAsync<PeriodicSnapshot>(_uri + "/snapshot/periodic", s, cancellationToken))
await Utils.LookForErrorAndThrowAsync(_api._client, response, cancellationToken);
}
#endregion
#region stdin/stdout
/// <summary>
/// Copies the full content of the outputs received from the endpoint to the given stream
/// Note: the Content will rotate if it's too large
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="endpoint">Endpoint from which to get the output content.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
protected virtual async Task CopyOutputsToAsync(Stream destinationStream, string endpoint, CancellationToken cancellationToken = default(CancellationToken)) {
using (var response = await _api._client.GetAsync(endpoint, cancellationToken))
{
await Utils.LookForErrorAndThrowAsync(_api._client, response, cancellationToken);
await response.Content.CopyToAsync(destinationStream);
}
}
/// <summary>
/// Copies the full standard output to the given stream.
/// Note: the standard output will rotate if it's too large.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task CopyStdoutToAsync(Stream destinationStream, CancellationToken cancellationToken = default(CancellationToken)) {
await CopyOutputsToAsync(destinationStream, _uri + "/stdout", cancellationToken);
}
/// <summary>
/// Copies the full standard output of the task instance to the given stream.
/// Note: the standard output will rotate if it's too large.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="instanceId">(optional) Id of the instance of which we want the stdout. Will retrieve the stdout of all instances if null</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task CopyStdoutToAsync(Stream destinationStream, uint? instanceId, CancellationToken cancellationToken = default(CancellationToken)) {
var endpoint = string.Format("{0}/stdout/{1}", _uri, instanceId);
await CopyOutputsToAsync(destinationStream, endpoint, cancellationToken);
}
/// <summary>
/// Copies the full standard error to the given stream.
/// Note: the standard error will rotate if it's too large.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task CopyStderrToAsync(Stream destinationStream, CancellationToken cancellationToken = default(CancellationToken)) {
await CopyOutputsToAsync(destinationStream, _uri + "/stderr", cancellationToken);
}
/// <summary>
/// Copies the full standard error of the task instance to the given stream.
/// Note: the standard error will rotate if it's too large.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="instanceId">(optional) Id of the instance of which we want the stderr. Will retrieve the stderr of all instances if null</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task CopyStderrToAsync(Stream destinationStream, uint? instanceId, CancellationToken cancellationToken = default(CancellationToken)) {
var endpoint = string.Format("{0}/stderr/{1}", _uri, instanceId);
await CopyOutputsToAsync(destinationStream, endpoint, cancellationToken);
}
/// <summary>
/// Copies the fresh new content of the outputs since the last call to the given stream.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="endpoint">Endpoint from which to get the output content.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
protected virtual async Task CopyFreshOutputsToAsync(Stream destinationStream, string endpoint, CancellationToken cancellationToken = default(CancellationToken)) {
if (_api.IsReadOnly) throw new Exception("Can't retrieve fresh outputs, this connection is configured in read-only mode");
using (var response = await _api._client.PostAsync(endpoint, null, cancellationToken))
{
await Utils.LookForErrorAndThrowAsync(_api._client, response, cancellationToken);
await response.Content.CopyToAsync(destinationStream);
}
}
/// <summary>
/// Copies the fresh new standard output since the last call to the given stream.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task CopyFreshStdoutToAsync(Stream destinationStream, CancellationToken cancellationToken = default(CancellationToken)) {
await CopyFreshOutputsToAsync(destinationStream, _uri + "/stdout", cancellationToken);
}
/// <summary>
/// Copies the fresh new standard output of the task instance since the last call to the given stream.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="instanceId">(optional) Id of the instance of which we want the fresh stdout. Will retrieve the fresh stdout of all instances if null</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task CopyFreshStdoutToAsync(Stream destinationStream, uint? instanceId, CancellationToken cancellationToken = default(CancellationToken)) {
var endpoint = string.Format("{0}/stdout/{1}", _uri, instanceId);
await CopyFreshOutputsToAsync(destinationStream, endpoint, cancellationToken);
}
/// <summary>
/// Copies the fresh new standard error of the task instance since the last call to the given stream.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task CopyFreshStderrToAsync(Stream destinationStream, CancellationToken cancellationToken = default(CancellationToken)) {
await CopyFreshOutputsToAsync(destinationStream, _uri + "/stderr", cancellationToken);
}
/// <summary>
/// Copies the fresh new standard error of the task instance since the last call to the given stream.
/// </summary>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="instanceId">(optional) Id of the instance of which we want the fresh stderr. Will retrieve the fresh stderr of all instances if null</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns></returns>
public virtual async Task CopyFreshStderrToAsync(Stream destinationStream, uint? instanceId, CancellationToken cancellationToken = default(CancellationToken)) {
var endpoint = string.Format("{0}/stderr/{1}", _uri, instanceId);
await CopyFreshOutputsToAsync(destinationStream, endpoint, cancellationToken);
}
/// <summary>
/// Returns the full standard output.
/// Note: the standard output will rotate if it's too large.
/// </summary>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns>The task standard output.</returns>
public virtual async Task<string> StdoutAsync(CancellationToken cancellationToken = default(CancellationToken)) {
using (MemoryStream ms = new MemoryStream()) {
await CopyStdoutToAsync(ms, cancellationToken);
ms.Position = 0;
using (var reader = new StreamReader(ms)) {
return await reader.ReadToEndAsync();
}
}
}
/// <summary>
/// Returns the full standard output of the task instance.
/// Note: the standard output will rotate if it's too large.
/// </summary>
/// <param name="instanceId">(optional) Id of the instance of which we want the stdout. Will retrieve the stdout of all instances if null.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns>The instance standard output.</returns>
public virtual async Task<string> StdoutAsync(uint? instanceId, CancellationToken cancellationToken = default(CancellationToken)) {
using (MemoryStream ms = new MemoryStream()) {
await CopyStdoutToAsync(ms, instanceId, cancellationToken);
ms.Position = 0;
using (var reader = new StreamReader(ms)) {
return await reader.ReadToEndAsync();
}
}
}
/// <summary>
/// Return the full standard error.
/// Note: the standard error will rotate if it's too large.
/// </summary>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns>The task standard error.</returns>
public virtual async Task<string> StderrAsync(CancellationToken cancellationToken = default(CancellationToken)) {
using (MemoryStream ms = new MemoryStream()) {
await CopyStderrToAsync(ms, cancellationToken);
ms.Position = 0;
using (var reader = new StreamReader(ms)) {
return await reader.ReadToEndAsync();
}
}
}
/// <summary>
/// Return the full standard error of the task instance.
/// Note: the standard error will rotate if it's too large.
/// </summary>
/// <param name="instanceId">(optional) Id of the instance of which we want the stderr. Will retrieve the stderr of all instances if null.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns>The instance standard error.</returns>
public virtual async Task<string> StderrAsync(uint? instanceId, CancellationToken cancellationToken = default(CancellationToken)) {
using (MemoryStream ms = new MemoryStream()) {
await CopyStderrToAsync(ms, instanceId, cancellationToken);
ms.Position = 0;
using (var reader = new StreamReader(ms)) {
return await reader.ReadToEndAsync();
}
}
}
/// <summary>
/// Returns the fresh new standard output since the last call.
/// </summary>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns>The task fresh standard output.</returns>
public virtual async Task<string> FreshStdoutAsync(CancellationToken cancellationToken = default(CancellationToken)) {
using (MemoryStream ms = new MemoryStream()) {
await CopyFreshStdoutToAsync(ms, cancellationToken);
ms.Position = 0;
using (var reader = new StreamReader(ms)) {
return await reader.ReadToEndAsync();
}
}
}
/// <summary>
/// Returns the fresh new standard output of the task instance since the last call.
/// </summary>
/// <param name="instanceId">(optional) Id of the instance of which we want the fresh stdout. Will retrieve the fresh stdout of all instances if null.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns>The instance fresh standard output.</returns>
public virtual async Task<string> FreshStdoutAsync(uint? instanceId, CancellationToken cancellationToken = default(CancellationToken)) {
using (MemoryStream ms = new MemoryStream()) {
await CopyFreshStdoutToAsync(ms, instanceId, cancellationToken);
ms.Position = 0;
using (var reader = new StreamReader(ms)) {
return await reader.ReadToEndAsync();
}
}
}
/// <summary>
/// Returns the fresh new standard error since the last call.
/// </summary>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns>The task fresh standard error.</returns>
public virtual async Task<string> FreshStderrAsync(CancellationToken cancellationToken = default(CancellationToken)) {
using (MemoryStream ms = new MemoryStream()) {
await CopyFreshStderrToAsync(ms, cancellationToken);
ms.Position = 0;
using (var reader = new StreamReader(ms)) {
return await reader.ReadToEndAsync();
}
}
}
/// <summary>
/// Returns the fresh new standard error of the task instance since the last call.
/// </summary>
/// <param name="instanceId">(optional) Id of the instance of which we want the fresh stderr. Will retrieve the fresh stderr of all instances if null.</param>
/// <param name="cancellationToken">Optional token to cancel the request.</param>
/// <returns>The instance fresh standard error.</returns>
public virtual async Task<string> FreshStderrAsync(uint? instanceId, CancellationToken cancellationToken = default(CancellationToken)) {
using (MemoryStream ms = new MemoryStream()) {
await CopyFreshStderrToAsync(ms, instanceId, cancellationToken);
ms.Position = 0;
using (var reader = new StreamReader(ms)) {
return await reader.ReadToEndAsync();
}
}
}
#endregion
}
}