|
1 | | -using System.Diagnostics; |
2 | | -using Relativity.Export.Samples.RelConsole.Helpers; |
3 | | -using Relativity.Export.V1.Model; |
4 | | -using Relativity.Services.ServiceProxy; |
5 | | -using Spectre.Console; |
6 | | - |
7 | | -namespace Relativity.Export.Samples.RelConsole.SampleCollection; |
8 | | - |
9 | | -public partial class BaseExportService |
10 | | -{ |
11 | | - private readonly Logger _logger; |
12 | | - private readonly IServiceFactory _serviceFactory; |
13 | | - private readonly string _host; |
14 | | - private readonly string _username; |
15 | | - private readonly string _password; |
16 | | - |
17 | | - public BaseExportService(string host, string username, string password, string[] args = default!) |
18 | | - { |
19 | | - this._host = host; |
20 | | - this._username = username; |
21 | | - this._password = password; |
22 | | - |
23 | | - this._serviceFactory = this.GetServiceFactory(); |
24 | | - |
25 | | - _logger = new Logger(args); |
26 | | - } |
27 | | - |
28 | | - protected IServiceFactory GetServiceFactory() |
29 | | - { |
30 | | - Uri relativityRestUri = new Uri($"{this._host}relativity.rest/api"); |
31 | | - Credentials credentials = new UsernamePasswordCredentials(this._username, this._password); |
32 | | - |
33 | | - ServiceFactorySettings settings = new ServiceFactorySettings(relativityRestUri, credentials); |
34 | | - |
35 | | - return new ServiceFactory(settings); |
36 | | - } |
37 | | - |
38 | | - private async Task<ValueResponse<ExportJob>> WaitForJobToBeCompletedAsync(Func<Task<ValueResponse<ExportJob>>> job, bool updateStatus = true, int frequency = 5_000) |
39 | | - { |
40 | | - ValueResponse<ExportJob>? jobStatus = null; |
41 | | - CancellationTokenSource tokenSource = new(); |
42 | | - Stopwatch watch = Stopwatch.StartNew(); |
43 | | - int retries = 3; |
44 | | - |
45 | | - if (updateStatus) |
46 | | - { |
47 | | - // request time measurement |
48 | | - Task watchUpdater = Task.Run(async () => |
49 | | - { |
50 | | - while (!tokenSource.IsCancellationRequested) |
51 | | - { |
52 | | - OutputHelper.UpdateStatus($"Fetching updates ({watch.ElapsedMilliseconds} ms)"); |
53 | | - await Task.Delay(100); |
54 | | - } |
55 | | - }, tokenSource.Token); |
56 | | - } |
57 | | - |
58 | | - do |
59 | | - { |
60 | | - try |
61 | | - { |
62 | | - jobStatus = await job(); |
63 | | - |
64 | | - if (jobStatus is null) |
65 | | - throw new Exception("The response was incorrect"); |
66 | | - |
67 | | - string logData = |
68 | | - $"Export job ID: {jobStatus.ExportJobID}\n" |
69 | | - + $"Job status: [aquamarine1]{jobStatus.Value.JobStatus}[/]"; |
70 | | - |
71 | | - _logger.LogInformation(logData); |
72 | | - |
73 | | - await Task.Delay(frequency); |
74 | | - retries = 3; |
75 | | - } |
76 | | - catch (Exception) when (retries > 0) |
77 | | - { |
78 | | - retries--; |
79 | | - _logger.LogWarning($"Retrying job status fetching ({retries} retries left)"); |
80 | | - await Task.Delay(3000); |
81 | | - |
82 | | - } |
83 | | - catch (Exception ex) |
84 | | - { |
85 | | - AnsiConsole.WriteException(ex); |
86 | | - throw; |
87 | | - } |
88 | | - finally |
89 | | - { |
90 | | - watch.Restart(); |
91 | | - } |
92 | | - } while (jobStatus?.Value.JobStatus is not ExportStatus.Completed |
93 | | - and not ExportStatus.CompletedWithErrors |
94 | | - and not ExportStatus.Failed |
95 | | - and not ExportStatus.Cancelled); |
96 | | - |
97 | | - tokenSource.Cancel(); |
98 | | - |
99 | | - if (jobStatus.Value.JobStatus == ExportStatus.Failed) |
100 | | - { |
101 | | - _logger.LogError($"{jobStatus.Value.ErrorCode} - {jobStatus.Value.ErrorMessage}"); |
102 | | - } |
103 | | - |
104 | | - return jobStatus; |
105 | | - } |
106 | | -} |
| 1 | +using System.Diagnostics; |
| 2 | +using Relativity.Export.Samples.RelConsole.Helpers; |
| 3 | +using Relativity.Export.V1.Model; |
| 4 | +using Relativity.Services.ServiceProxy; |
| 5 | +using Spectre.Console; |
| 6 | + |
| 7 | +namespace Relativity.Export.Samples.RelConsole.SampleCollection; |
| 8 | + |
| 9 | +public partial class BaseExportService |
| 10 | +{ |
| 11 | + private readonly Logger _logger; |
| 12 | + private readonly IServiceFactory _serviceFactory; |
| 13 | + private readonly string _host; |
| 14 | + private readonly string _username; |
| 15 | + private readonly string _password; |
| 16 | + |
| 17 | + public BaseExportService(string host, string username, string password, string[] args = default!) |
| 18 | + { |
| 19 | + this._host = host; |
| 20 | + this._username = username; |
| 21 | + this._password = password; |
| 22 | + |
| 23 | + this._serviceFactory = this.GetServiceFactory(); |
| 24 | + |
| 25 | + _logger = new Logger(args); |
| 26 | + } |
| 27 | + |
| 28 | + protected IServiceFactory GetServiceFactory() |
| 29 | + { |
| 30 | + Uri relativityRestUri = new Uri($"{this._host}relativity.rest/api"); |
| 31 | + Credentials credentials = new UsernamePasswordCredentials(this._username, this._password); |
| 32 | + |
| 33 | + ServiceFactorySettings settings = new ServiceFactorySettings(relativityRestUri, credentials); |
| 34 | + |
| 35 | + return new ServiceFactory(settings); |
| 36 | + } |
| 37 | + |
| 38 | + private async Task<ValueResponse<ExportJob>> WaitForJobToBeCompletedAsync(Func<Task<ValueResponse<ExportJob>>> job, bool updateStatus = true, int frequency = 5_000) |
| 39 | + { |
| 40 | + ValueResponse<ExportJob>? jobStatus = null; |
| 41 | + CancellationTokenSource tokenSource = new(); |
| 42 | + Stopwatch watch = Stopwatch.StartNew(); |
| 43 | + int retries = 3; |
| 44 | + |
| 45 | + if (updateStatus) |
| 46 | + { |
| 47 | + // request time measurement |
| 48 | + Task watchUpdater = Task.Run(async () => |
| 49 | + { |
| 50 | + while (!tokenSource.IsCancellationRequested) |
| 51 | + { |
| 52 | + OutputHelper.UpdateStatus($"Fetching updates ({watch.ElapsedMilliseconds} ms)"); |
| 53 | + await Task.Delay(100); |
| 54 | + } |
| 55 | + }, tokenSource.Token); |
| 56 | + } |
| 57 | + |
| 58 | + do |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + jobStatus = await job(); |
| 63 | + |
| 64 | + if (jobStatus is null) |
| 65 | + throw new Exception("The response was incorrect"); |
| 66 | + |
| 67 | + string logData = |
| 68 | + $"Export job ID: {jobStatus.ExportJobID}\n" |
| 69 | + + $"Job status: [aquamarine1]{jobStatus.Value.JobStatus}[/]"; |
| 70 | + |
| 71 | + _logger.LogInformation(logData); |
| 72 | + |
| 73 | + await Task.Delay(frequency); |
| 74 | + retries = 3; |
| 75 | + } |
| 76 | + catch (Exception) when (retries > 0) |
| 77 | + { |
| 78 | + retries--; |
| 79 | + _logger.LogWarning($"Retrying job status fetching ({retries} retries left)"); |
| 80 | + await Task.Delay(3000); |
| 81 | + |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + AnsiConsole.WriteException(ex); |
| 86 | + throw; |
| 87 | + } |
| 88 | + finally |
| 89 | + { |
| 90 | + watch.Restart(); |
| 91 | + } |
| 92 | + } while (jobStatus?.Value.JobStatus is not ExportStatus.Completed |
| 93 | + and not ExportStatus.CompletedWithErrors |
| 94 | + and not ExportStatus.Failed |
| 95 | + and not ExportStatus.Cancelled); |
| 96 | + |
| 97 | + tokenSource.Cancel(); |
| 98 | + |
| 99 | + if (jobStatus.Value.JobStatus == ExportStatus.Failed) |
| 100 | + { |
| 101 | + _logger.LogError($"{jobStatus.Value.ErrorCode} - {jobStatus.Value.ErrorMessage}"); |
| 102 | + } |
| 103 | + |
| 104 | + return jobStatus; |
| 105 | + } |
| 106 | +} |
0 commit comments