-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseExportService.cs
More file actions
106 lines (87 loc) · 2.81 KB
/
BaseExportService.cs
File metadata and controls
106 lines (87 loc) · 2.81 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
using System.Diagnostics;
using Relativity.Export.Samples.RelConsole.Helpers;
using Relativity.Export.V1.Model;
using Relativity.Services.ServiceProxy;
using Spectre.Console;
namespace Relativity.Export.Samples.RelConsole.SampleCollection;
public partial class BaseExportService
{
private readonly Logger _logger;
private readonly IServiceFactory _serviceFactory;
private readonly string _host;
private readonly string _username;
private readonly string _password;
public BaseExportService(string host, string username, string password, string[] args = default!)
{
this._host = host;
this._username = username;
this._password = password;
this._serviceFactory = this.GetServiceFactory();
_logger = new Logger(args);
}
protected IServiceFactory GetServiceFactory()
{
Uri relativityRestUri = new Uri($"{this._host}relativity.rest/api");
Credentials credentials = new UsernamePasswordCredentials(this._username, this._password);
ServiceFactorySettings settings = new ServiceFactorySettings(relativityRestUri, credentials);
return new ServiceFactory(settings);
}
private async Task<ValueResponse<ExportJob>> WaitForJobToBeCompletedAsync(Func<Task<ValueResponse<ExportJob>>> job, bool updateStatus = true, int frequency = 5_000)
{
ValueResponse<ExportJob>? jobStatus = null;
CancellationTokenSource tokenSource = new();
Stopwatch watch = Stopwatch.StartNew();
int retries = 3;
if (updateStatus)
{
// request time measurement
Task watchUpdater = Task.Run(async () =>
{
while (!tokenSource.IsCancellationRequested)
{
OutputHelper.UpdateStatus($"Fetching updates ({watch.ElapsedMilliseconds} ms)");
await Task.Delay(100);
}
}, tokenSource.Token);
}
do
{
try
{
jobStatus = await job();
if (jobStatus is null)
throw new Exception("The response was incorrect");
string logData =
$"Export job ID: {jobStatus.ExportJobID}\n"
+ $"Job status: [aquamarine1]{jobStatus.Value.JobStatus}[/]";
_logger.LogInformation(logData);
await Task.Delay(frequency);
retries = 3;
}
catch (Exception) when (retries > 0)
{
retries--;
_logger.LogWarning($"Retrying job status fetching ({retries} retries left)");
await Task.Delay(3000);
}
catch (Exception ex)
{
AnsiConsole.WriteException(ex);
throw;
}
finally
{
watch.Restart();
}
} while (jobStatus?.Value.JobStatus is not ExportStatus.Completed
and not ExportStatus.CompletedWithErrors
and not ExportStatus.Failed
and not ExportStatus.Cancelled);
tokenSource.Cancel();
if (jobStatus.Value.JobStatus == ExportStatus.Failed)
{
_logger.LogError($"{jobStatus.Value.ErrorCode} - {jobStatus.Value.ErrorMessage}");
}
return jobStatus;
}
}