Skip to content

Commit de20eeb

Browse files
SF-3503 Update MongoDB Driver to 3.4 (#3366)
Co-authored-by: Nathaniel Paulus <[email protected]>
1 parent aff8cc3 commit de20eeb

19 files changed

+376
-506
lines changed

src/SIL.XForge.Scripture/Services/MachineApiService.cs

Lines changed: 92 additions & 33 deletions
Large diffs are not rendered by default.

src/SIL.XForge.Scripture/Services/MachineProjectService.cs

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ IRepository<UserSecret> userSecrets
7373
public async Task<string> AddSmtProjectAsync(string sfProjectId, CancellationToken cancellationToken)
7474
{
7575
// Load the project from the realtime service
76-
Attempt<SFProject> attempt = await realtimeService.TryGetSnapshotAsync<SFProject>(sfProjectId);
76+
Attempt<SFProject> attempt = await realtimeService.TryGetSnapshotAsync<SFProject>(
77+
sfProjectId,
78+
cancellationToken
79+
);
7780
if (!attempt.TryResult(out SFProject project))
7881
{
7982
throw new DataNotFoundException("The project does not exist.");
@@ -134,7 +137,8 @@ await projectSecrets.UpdateAsync(
134137
{
135138
u.Unset(p => p.ServalData.TranslationQueuedAt);
136139
}
137-
}
140+
},
141+
cancellationToken: cancellationToken
138142
);
139143
}
140144
catch (ServalApiException e) when (e.StatusCode == 409)
@@ -154,7 +158,8 @@ await projectSecrets.UpdateAsync(
154158
u.Unset(p => p.ServalData.TranslationJobId);
155159
u.Unset(p => p.ServalData.TranslationQueuedAt);
156160
}
157-
}
161+
},
162+
cancellationToken: cancellationToken
158163
);
159164
}
160165
catch (DataNotFoundException e)
@@ -191,7 +196,8 @@ await projectSecrets.UpdateAsync(
191196
u.Unset(p => p.ServalData.TranslationJobId);
192197
u.Unset(p => p.ServalData.TranslationQueuedAt);
193198
}
194-
}
199+
},
200+
cancellationToken: cancellationToken
195201
);
196202
}
197203
}
@@ -213,7 +219,10 @@ CancellationToken cancellationToken
213219
)
214220
{
215221
// Load the project from the realtime service
216-
Attempt<SFProject> attempt = await realtimeService.TryGetSnapshotAsync<SFProject>(sfProjectId);
222+
Attempt<SFProject> attempt = await realtimeService.TryGetSnapshotAsync<SFProject>(
223+
sfProjectId,
224+
cancellationToken
225+
);
217226
if (!attempt.TryResult(out SFProject project))
218227
{
219228
throw new DataNotFoundException("The project does not exist.");
@@ -244,7 +253,11 @@ CancellationToken cancellationToken
244253
)
245254
{
246255
// Load the target project secrets, so we can get the translation engine ID
247-
if (!(await projectSecrets.TryGetAsync(sfProjectId)).TryResult(out SFProjectSecret projectSecret))
256+
if (
257+
!(await projectSecrets.TryGetAsync(sfProjectId, cancellationToken)).TryResult(
258+
out SFProjectSecret projectSecret
259+
)
260+
)
248261
{
249262
throw new DataNotFoundException("The project secret cannot be found.");
250263
}
@@ -364,7 +377,8 @@ await projectSecrets.UpdateAsync(
364377

365378
// Remove all corpora that were deleted
366379
u.RemoveAll(p => p.ServalData.CorpusFiles, p => corpusIdsToRemove.Contains(p.CorpusId));
367-
}
380+
},
381+
cancellationToken: cancellationToken
368382
);
369383
}
370384

@@ -519,7 +533,11 @@ CancellationToken cancellationToken
519533
)
520534
{
521535
// Load the target project secrets, so we can get the translation engine ID
522-
if (!(await projectSecrets.TryGetAsync(buildConfig.ProjectId)).TryResult(out SFProjectSecret projectSecret))
536+
if (
537+
!(await projectSecrets.TryGetAsync(buildConfig.ProjectId, cancellationToken)).TryResult(
538+
out SFProjectSecret projectSecret
539+
)
540+
)
523541
{
524542
throw new DataNotFoundException("The project secret cannot be found.");
525543
}
@@ -564,7 +582,7 @@ await RecreateOrUpdateTranslationEngineIfRequiredAsync(
564582
);
565583

566584
// Get the updated project secret
567-
projectSecret = await projectSecrets.GetAsync(buildConfig.ProjectId);
585+
projectSecret = await projectSecrets.GetAsync(buildConfig.ProjectId, cancellationToken: cancellationToken);
568586

569587
// Ensure we have the ServalData
570588
if (projectSecret.ServalData is null)
@@ -622,7 +640,8 @@ await projectSecrets.UpdateAsync(
622640
u.Unset(p => p.ServalData.TranslationJobId);
623641
u.Unset(p => p.ServalData.TranslationQueuedAt);
624642
}
625-
}
643+
},
644+
cancellationToken: cancellationToken
626645
);
627646

628647
return translationBuild.Id;
@@ -727,7 +746,7 @@ CancellationToken cancellationToken
727746
)
728747
{
729748
// Get the existing project secret, so we can see how to create the engine and update the Serval data
730-
SFProjectSecret projectSecret = await projectSecrets.GetAsync(sfProject.Id);
749+
SFProjectSecret projectSecret = await projectSecrets.GetAsync(sfProject.Id, cancellationToken);
731750
string translationEngineId = GetTranslationEngineId(projectSecret, preTranslate);
732751
if (string.IsNullOrWhiteSpace(translationEngineId))
733752
{
@@ -757,15 +776,17 @@ CancellationToken cancellationToken
757776
// Store the Pre-Translation Engine ID
758777
await projectSecrets.UpdateAsync(
759778
sfProject.Id,
760-
u => u.Set(p => p.ServalData.PreTranslationEngineId, translationEngineId)
779+
u => u.Set(p => p.ServalData.PreTranslationEngineId, translationEngineId),
780+
cancellationToken: cancellationToken
761781
);
762782
}
763783
else if (projectSecret.ServalData is not null)
764784
{
765785
// Store the Translation Engine ID
766786
await projectSecrets.UpdateAsync(
767787
sfProject.Id,
768-
u => u.Set(p => p.ServalData.TranslationEngineId, translationEngineId)
788+
u => u.Set(p => p.ServalData.TranslationEngineId, translationEngineId),
789+
cancellationToken: cancellationToken
769790
);
770791
}
771792
else if (preTranslate)
@@ -777,7 +798,8 @@ await projectSecrets.UpdateAsync(
777798
u.Set(
778799
p => p.ServalData,
779800
new ServalData { PreTranslationEngineId = translationEngineId, CorpusFiles = [] }
780-
)
801+
),
802+
cancellationToken: cancellationToken
781803
);
782804
}
783805
else
@@ -789,7 +811,8 @@ await projectSecrets.UpdateAsync(
789811
u.Set(
790812
p => p.ServalData,
791813
new ServalData { TranslationEngineId = translationEngineId, CorpusFiles = [] }
792-
)
814+
),
815+
cancellationToken: cancellationToken
793816
);
794817
}
795818
}
@@ -913,7 +936,7 @@ CancellationToken cancellationToken
913936
)
914937
{
915938
// Get the user secret
916-
Attempt<UserSecret> userSecretAttempt = await userSecrets.TryGetAsync(curUserId);
939+
Attempt<UserSecret> userSecretAttempt = await userSecrets.TryGetAsync(curUserId, cancellationToken);
917940
if (!userSecretAttempt.TryResult(out UserSecret userSecret))
918941
{
919942
throw new DataNotFoundException("The user does not exist.");
@@ -987,7 +1010,8 @@ await projectSecrets.UpdateAsync(
9871010
{
9881011
u.Unset(p => p.ServalData.TranslationEngineId);
9891012
}
990-
}
1013+
},
1014+
cancellationToken: cancellationToken
9911015
);
9921016

9931017
// Create the Serval project, and get the translation engine id
@@ -1333,7 +1357,8 @@ await projectSecrets.UpdateAsync(
13331357
{
13341358
u.Unset(p => p.ServalData.TranslationEngineId);
13351359
}
1336-
}
1360+
},
1361+
cancellationToken: cancellationToken
13371362
);
13381363

13391364
// Create the new translation engine id
@@ -1358,7 +1383,11 @@ CancellationToken cancellationToken
13581383
)
13591384
{
13601385
// Load the target project secrets, so we can get the translation engine ID
1361-
if (!(await projectSecrets.TryGetAsync(sfProjectId)).TryResult(out SFProjectSecret projectSecret))
1386+
if (
1387+
!(await projectSecrets.TryGetAsync(sfProjectId, cancellationToken)).TryResult(
1388+
out SFProjectSecret projectSecret
1389+
)
1390+
)
13621391
{
13631392
throw new DataNotFoundException("The project secret cannot be found.");
13641393
}
@@ -1410,13 +1439,21 @@ await translationEnginesClient.DeleteCorpusAsync(
14101439
}
14111440

14121441
// Remove our record of the corpus
1413-
await projectSecrets.UpdateAsync(sfProjectId, u => u.Unset(p => p.ServalData.Corpora[corpusId]));
1442+
await projectSecrets.UpdateAsync(
1443+
sfProjectId,
1444+
u => u.Unset(p => p.ServalData.Corpora[corpusId]),
1445+
cancellationToken: cancellationToken
1446+
);
14141447
}
14151448

14161449
// Remove the corpora property if it is empty
14171450
if (projectSecret.ServalData?.Corpora?.Any(c => c.Value.PreTranslate != preTranslate) == false)
14181451
{
1419-
await projectSecrets.UpdateAsync(sfProjectId, u => u.Unset(p => p.ServalData.Corpora));
1452+
await projectSecrets.UpdateAsync(
1453+
sfProjectId,
1454+
u => u.Unset(p => p.ServalData.Corpora),
1455+
cancellationToken: cancellationToken
1456+
);
14201457
}
14211458
}
14221459

@@ -1567,14 +1604,21 @@ CancellationToken cancellationToken
15671604
)
15681605
{
15691606
// Load the project from the realtime service
1570-
Attempt<SFProject> attempt = await realtimeService.TryGetSnapshotAsync<SFProject>(buildConfig.ProjectId);
1607+
Attempt<SFProject> attempt = await realtimeService.TryGetSnapshotAsync<SFProject>(
1608+
buildConfig.ProjectId,
1609+
cancellationToken
1610+
);
15711611
if (!attempt.TryResult(out SFProject project))
15721612
{
15731613
throw new DataNotFoundException("The project does not exist.");
15741614
}
15751615

15761616
// Load the project secrets, so we can get the corpus files
1577-
if (!(await projectSecrets.TryGetAsync(project.Id)).TryResult(out SFProjectSecret projectSecret))
1617+
if (
1618+
!(await projectSecrets.TryGetAsync(project.Id, cancellationToken)).TryResult(
1619+
out SFProjectSecret projectSecret
1620+
)
1621+
)
15781622
{
15791623
throw new DataNotFoundException("The project secret cannot be found.");
15801624
}
@@ -1840,7 +1884,8 @@ await projectSecrets.UpdateAsync(
18401884
{
18411885
u.Set(p => p.ServalData.ParallelCorpusIdForSmt, translationParallelCorpusId);
18421886
}
1843-
}
1887+
},
1888+
cancellationToken: cancellationToken
18441889
);
18451890

18461891
return corporaSyncInfo;

src/SIL.XForge.Scripture/Services/ParatextService.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ you will end up here */
764764
}
765765
else
766766
{
767-
projectUserSecret = await _userSecretRepository.GetAsync(user.Id);
767+
projectUserSecret = await _userSecretRepository.GetAsync(user.Id, token);
768768
}
769769

770770
// Get the PT role
@@ -3570,7 +3570,7 @@ private async Task<UserSecret> GetUserSecretWithCurrentParatextTokens(string sfU
35703570
await semaphore.WaitAsync(token);
35713571
try
35723572
{
3573-
Attempt<UserSecret> attempt = await _userSecretRepository.TryGetAsync(sfUserId);
3573+
Attempt<UserSecret> attempt = await _userSecretRepository.TryGetAsync(sfUserId, token);
35743574
if (!attempt.TryResult(out UserSecret userSecret))
35753575
{
35763576
throw new DataNotFoundException("Could not find user secrets for SF user id " + sfUserId);
@@ -3598,7 +3598,7 @@ private async Task<UserSecret> GetUserSecretWithCurrentParatextTokens(string sfU
35983598

35993599
// Get the tokens from auth0, and make sure they are up-to-date
36003600
// If they cannot be refreshed, an exception will throw
3601-
Attempt<User> userAttempt = await _realtimeService.TryGetSnapshotAsync<User>(sfUserId);
3601+
Attempt<User> userAttempt = await _realtimeService.TryGetSnapshotAsync<User>(sfUserId, token);
36023602
if (!userAttempt.TryResult(out User user))
36033603
{
36043604
throw;
@@ -3620,7 +3620,8 @@ private async Task<UserSecret> GetUserSecretWithCurrentParatextTokens(string sfU
36203620

36213621
userSecret = await _userSecretRepository.UpdateAsync(
36223622
sfUserId,
3623-
b => b.Set(u => u.ParatextTokens, refreshedUserTokens)
3623+
b => b.Set(u => u.ParatextTokens, refreshedUserTokens),
3624+
cancellationToken: token
36243625
);
36253626
}
36263627

src/SIL.XForge.Scripture/Services/ParatextSyncRunner.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ CancellationToken token
985985
{
986986
await _hubContext.NotifySyncProgress(projectSFId, ProgressState.NotStarted);
987987
_logger.LogInformation($"Initializing sync for project {projectSFId} with sync metrics id {syncMetricsId}");
988-
if (!(await _syncMetricsRepository.TryGetAsync(syncMetricsId)).TryResult(out _syncMetrics))
988+
if (!(await _syncMetricsRepository.TryGetAsync(syncMetricsId, token)).TryResult(out _syncMetrics))
989989
{
990990
Log($"Could not find sync metrics.", syncMetricsId, userId);
991991
return false;
@@ -1010,7 +1010,7 @@ CancellationToken token
10101010
_syncMetrics.ProductVersion = Product.Version;
10111011
_syncMetrics.DateStarted = DateTime.UtcNow;
10121012
_syncMetrics.Status = SyncStatus.Running;
1013-
if (!await _syncMetricsRepository.ReplaceAsync(_syncMetrics, true))
1013+
if (!await _syncMetricsRepository.ReplaceAsync(_syncMetrics, true, token))
10141014
{
10151015
Log("The sync metrics could not be updated in MongoDB");
10161016
}
@@ -1030,7 +1030,7 @@ CancellationToken token
10301030

10311031
await NotifySyncProgress(SyncPhase.Phase1, 30.0);
10321032

1033-
if (!(await _projectSecrets.TryGetAsync(projectSFId)).TryResult(out _projectSecret))
1033+
if (!(await _projectSecrets.TryGetAsync(projectSFId, token)).TryResult(out _projectSecret))
10341034
{
10351035
Log($"Could not find project secret.", projectSFId, userId);
10361036
return false;
@@ -1040,7 +1040,7 @@ CancellationToken token
10401040
[.. _projectDoc.Data.UserRoles.Keys]
10411041
);
10421042

1043-
if (!(await _userSecrets.TryGetAsync(userId)).TryResult(out _userSecret))
1043+
if (!(await _userSecrets.TryGetAsync(userId, token)).TryResult(out _userSecret))
10441044
{
10451045
Log($"Could not find user secret.", projectSFId, userId);
10461046
return false;
@@ -1697,7 +1697,7 @@ private async Task CompleteSync(bool successful, bool canRollbackParatext, Cance
16971697
.QuerySnapshots<User>()
16981698
.Where(u => _projectDoc.Data.UserRoles.Keys.Contains(u.Id) && u.ParatextId != null)
16991699
.Select(u => new { UserId = u.Id, u.ParatextId })
1700-
.ToListAsync();
1700+
.ToListAsync(token);
17011701

17021702
bool dataInSync = true;
17031703
if (!successful)
@@ -1929,7 +1929,8 @@ await _projectSecrets.UpdateAsync(
19291929
{
19301930
u.Remove(p => p.SyncMetricsIds, _syncMetrics?.Id);
19311931
}
1932-
}
1932+
},
1933+
cancellationToken: token
19331934
);
19341935
}
19351936

@@ -2008,7 +2009,7 @@ await _projectDoc.SubmitJson0OpAsync(op =>
20082009
}
20092010

20102011
_syncMetrics.DateFinished = DateTime.UtcNow;
2011-
if (!await _syncMetricsRepository.ReplaceAsync(_syncMetrics, true))
2012+
if (!await _syncMetricsRepository.ReplaceAsync(_syncMetrics, true, token))
20122013
{
20132014
Log("The sync metrics could not be updated in MongoDB");
20142015
}

src/SIL.XForge.Scripture/Services/SFProjectService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ public async Task UpdatePermissionsAsync(
13941394
CancellationToken token = default
13951395
)
13961396
{
1397-
Attempt<UserSecret> userSecretAttempt = await _userSecrets.TryGetAsync(curUserId);
1397+
Attempt<UserSecret> userSecretAttempt = await _userSecrets.TryGetAsync(curUserId, token);
13981398
if (!userSecretAttempt.TryResult(out UserSecret userSecret))
13991399
{
14001400
throw new DataNotFoundException("No matching user secrets found.");

0 commit comments

Comments
 (0)