Created
February 18, 2013 16:00
-
-
Save wernight/4978408 to your computer and use it in GitHub Desktop.
Custom NAnt task to execute multiple <exec> tasks in parallel and wait until they all completed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using NAnt.Core; | |
using NAnt.Core.Attributes; | |
using NAnt.Core.Tasks; | |
namespace MyTasks | |
{ | |
[TaskName("parallelexec")] | |
public class ParallelExecTask : TaskContainer | |
{ | |
[BuildElementArray("exec", Required = true, ElementType = typeof(ExecTask))] | |
public ExecTask[] ExecTasks { get; set; } | |
[TaskAttribute("threadcount")] | |
public int ThreadCount { get; set; } | |
protected override void ExecuteTask() | |
{ | |
var parallelOptions = new ParallelOptions(); | |
if (ThreadCount > 0) | |
{ | |
parallelOptions.MaxDegreeOfParallelism = ThreadCount; | |
Log(Level.Verbose, string.Format("Executing in parallel using at most {0} threads...", ThreadCount)); | |
} | |
else | |
{ | |
Log(Level.Verbose, string.Format("Executing in parallel using at most {0} threads...", ThreadCount)); | |
} | |
try | |
{ | |
Parallel.ForEach(ExecTasks, parallelOptions, Body); | |
} | |
catch (AggregateException e) | |
{ | |
foreach (Exception innerException in e.InnerExceptions) | |
{ | |
if (innerException is BuildException) | |
Log(Level.Error, innerException.Message); | |
else | |
throw innerException; | |
} | |
throw new BuildException("Parallel execution failed for " + e.InnerExceptions.Count + " of " + ExecTasks.Length + " commands executions (see the above log).", Location); | |
} | |
} | |
private void Body(ExecTask execTask) | |
{ | |
try | |
{ | |
execTask.Execute(); | |
} | |
catch (BuildException e) | |
{ | |
throw new BuildException("External Program Failed: " + execTask.ProgramFileName + " " + execTask.CommandLine + " (return code was " + execTask.ExitCode + ")", e); | |
} | |
} | |
} | |
} |
A similar plugin is developed here: https://github.com/NAntCrossCompile/NAnt.Parallel
How do I put wait ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any example on how to use this?
Thanks