Skip to content

Commit

Permalink
Updated parallel usage
Browse files Browse the repository at this point in the history
  • Loading branch information
larfeq committed Dec 5, 2024
1 parent fe4a9dd commit a326caa
Showing 1 changed file with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task RunAsync(
MaxDegreeOfParallelism = threadCount
};

Parallel.ForEach(departments, parallelOptions, async orgUnit =>
await Parallel.ForEachAsync(departments, new ParallelOptions { MaxDegreeOfParallelism = threadCount }, async (orgUnit, cancellationToken) =>
{
var resourceOwners = orgUnit.Management.Persons
.Select(p => Guid.Parse(p.AzureUniqueId))
Expand Down Expand Up @@ -131,39 +131,34 @@ public async Task RunAsync(

logger.LogInformation("Syncing departments {Departments}", JsonConvert.SerializeObject(enqueueTimeForDepartmentMapping, Formatting.Indented));

await Task.Run(() =>
await Parallel.ForEachAsync(apiDepartments, new ParallelOptions { MaxDegreeOfParallelism = threadCount }, async (department, cancellationToken) =>
{
Parallel.ForEach(apiDepartments, parallelOptions, department =>
try
{
Task.Run(async () =>
{
try
{
// TODO: Do one batch update instead of individual updates
// Update the database
await summaryApiClient.PutDepartmentAsync(department, cancellationToken);
}
catch (Exception e)
{
logger.LogCritical(e, "Failed to PUT department {Department}",
JsonConvert.SerializeObject(department, Formatting.Indented));
return;
}

try
{
// Send queue message
await SendDepartmentToQueue(sender, department, enqueueTimeForDepartmentMapping[department]);
}
catch (Exception e)
{
logger.LogCritical(e, "Failed to send department to queue {Department}",
JsonConvert.SerializeObject(department, Formatting.Indented));
}
}).Wait();
});
// TODO: Do one batch update instead of individual updates
// Update the database
await summaryApiClient.PutDepartmentAsync(department, cancellationToken);
}
catch (Exception e)
{
logger.LogCritical(e, "Failed to PUT department {Department}",
JsonConvert.SerializeObject(department, Formatting.Indented));
return;
}

try
{
// Send queue message
await SendDepartmentToQueue(sender, department, enqueueTimeForDepartmentMapping[department]);
}
catch (Exception e)
{
logger.LogCritical(e, "Failed to send department to queue {Department}",
JsonConvert.SerializeObject(department, Formatting.Indented));
}
});


logger.LogInformation("weekly-department-recipients-sync completed");
}

Expand Down

0 comments on commit a326caa

Please sign in to comment.