Skip to content

Commit

Permalink
Use Task.Run for more eager starting of parallel tasks (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst authored Nov 30, 2024
1 parent 27f7e4e commit 1b4aa8c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/ModularPipelines.Build/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Use Task.Run for more eager starting of parallel tasks
12 changes: 6 additions & 6 deletions src/ModularPipelines/Engine/ModuleExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task<IEnumerable<ModuleBase>> ExecuteAsync(IReadOnlyList<ModuleBase
await ProcessKeyedNonParallelModules(keyedNonParallelModules.ToList());

var parallelModuleTasks = modules.Except(nonParallelModules)
.Select(x => StartModule(x, false))
.Select(x => Task.Run(() => StartModule(x, false)))
.ToArray();

if (_pipelineOptions.Value.ExecutionMode == ExecutionMode.StopOnFirstException)
Expand Down Expand Up @@ -119,18 +119,18 @@ private async Task ProcessKeyedNonParallelModules(List<ModuleBase> keyedNonParal
{
await keyedNonParallelModules
.OrderBy(x => x.GetType().GetCustomAttribute<NotInParallelAttribute>()!.ConstraintKeys.Length)
.ForEachAsync(async module =>
.ForEachAsync(module => Task.Run(async () =>
{
var keys = module.GetType()
.GetCustomAttribute<NotInParallelAttribute>()!
.ConstraintKeys
.OrderBy(x => x)
.ToArray();

_logger.LogDebug("Grabbing not in parallel locks for keys {Keys}", string.Join(", ", keys));

var locks = keys.Select(GetLockForKey).ToArray();

while (!WaitHandle.WaitAll(locks, TimeSpan.FromMilliseconds(100), false))
{
await Task.Delay(TimeSpan.FromMilliseconds(500));
Expand All @@ -147,7 +147,7 @@ await keyedNonParallelModules
semaphore.Release();
}
}
})
}))
.ProcessInParallel();
}

Expand Down

0 comments on commit 1b4aa8c

Please sign in to comment.