Skip to content

Commit 3cd5243

Browse files
committed
Support sync and async commands
1 parent 8f94658 commit 3cd5243

File tree

23 files changed

+58
-73
lines changed

23 files changed

+58
-73
lines changed

src/InEngine.Commands/AlwaysFail.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Threading.Tasks;
32
using InEngine.Core;
43
using InEngine.Core.Exceptions;
54

@@ -10,10 +9,7 @@ namespace InEngine.Commands;
109
/// </summary>
1110
public class AlwaysFail : AbstractCommand
1211
{
13-
public override async Task Run()
14-
{
15-
await Task.Run(() => throw new CommandFailedException("This command always fails."));
16-
}
12+
public override void Run() => throw new CommandFailedException("This command always fails.");
1713

1814
public override void Failed(Exception exception)
1915
{
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Threading.Tasks;
2-
using InEngine.Core;
1+
using InEngine.Core;
32

43
namespace InEngine.Commands;
54

@@ -8,8 +7,5 @@ namespace InEngine.Commands;
87
/// </summary>
98
public class AlwaysSucceed : AbstractCommand
109
{
11-
public override async Task Run()
12-
{
13-
await Task.Run(() => Info("This command always succeeds."));
14-
}
10+
public override void Run() => Info("This command always succeeds.");
1511
}

src/InEngine.Commands/Sample/Minimal.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,5 @@ namespace InEngine.Commands.Sample;
1414
*/
1515
public class Minimal : AbstractCommand
1616
{
17-
public override async Task Run()
18-
{
19-
await Task.Run(() => Console.WriteLine("This is an example of a minimal command."));
20-
}
17+
public override void Run() => Line("This is an example of a minimal command.");
2118
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using System.Threading;
22
using InEngine.Core;
3-
using System.Threading.Tasks;
43

54
namespace InEngine.Commands.Sample;
65

76
public class Pause : AbstractCommand
87
{
9-
public override async Task Run()
10-
{
11-
await Task.Run(() => Thread.Sleep(3000));
12-
}
8+
public override void Run() => Thread.Sleep(3000);
139
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
using InEngine.Core;
2-
using System;
3-
using System.Threading.Tasks;
42

53
namespace InEngine.Commands.Sample;
64

75
public class SayHello : AbstractCommand
86
{
9-
public override async Task Run()
10-
{
11-
await Task.Run(() => Console.WriteLine("hello"));
12-
}
7+
public override void Run() => Line("hello");
138
}

src/InEngine.Commands/Sample/ShowProgress.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public class ShowProgress : AbstractCommand
1313
* Note that the override keyword is necessary in the Run method
1414
* signature as the base class method is virtual.
1515
*/
16-
public override async Task Run()
16+
public override async Task RunAsync()
1717
{
1818
// Define the ticks (aka steps) for the command...
19-
var maxTicks = 100000;
19+
const int maxTicks = 100000;
2020
SetProgressBarMaxTicks(maxTicks);
2121

2222
// Do some work...

src/InEngine.Core.Test/Commands/ChainTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public void ShouldRunChainOfCommands()
2727
};
2828
Subject.Commands = commands;
2929

30-
Subject.Run();
30+
Subject.RunAsync();
3131

32-
mockCommand1.Verify(x => x.Run(), Times.Once());
33-
mockCommand2.Verify(x => x.Run(), Times.Once());
32+
mockCommand1.Verify(x => x.RunAsync(), Times.Once());
33+
mockCommand2.Verify(x => x.RunAsync(), Times.Once());
3434
}
3535

3636
[Test]
@@ -47,10 +47,10 @@ public void ShouldRunChainOfCommandsAndFail()
4747
};
4848
Subject.Commands = commands;
4949

50-
Assert.That(Subject.Run, Throws.TypeOf<CommandChainFailedException>());
50+
Assert.That(Subject.RunAsync, Throws.TypeOf<CommandChainFailedException>());
5151

52-
mockCommand1.Verify(x => x.Run(), Times.Once());
53-
mockCommand2.Verify(x => x.Run(), Times.Never());
52+
mockCommand1.Verify(x => x.RunAsync(), Times.Once());
53+
mockCommand2.Verify(x => x.RunAsync(), Times.Never());
5454
}
5555

5656
[Test]
@@ -62,7 +62,7 @@ public void ShouldRunChainOfDifferentCommands()
6262
new Echo() { VerbatimText = "Hello, world!" },
6363
};
6464

65-
Subject.Run();
65+
Subject.RunAsync();
6666
}
6767

6868
[Test]
@@ -74,6 +74,6 @@ public void ShouldRunChainOfDifferentCommandsAsAbstractCommand()
7474
new Echo(verbatimText: "Hello, world!"),
7575
};
7676

77-
Subject.Run();
77+
Subject.RunAsync();
7878
}
7979
}

src/InEngine.Core/AbstractCommand.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ public abstract class AbstractCommand : IJob, IWrite, IHasCommandLifeCycle, IHas
2424
public int SecondsBeforeTimeout { get; set; } = 300;
2525

2626
private MailSettings mailSettings;
27+
2728
public MailSettings MailSettings
2829
{
2930
get => mailSettings;
3031
set => CommandLifeCycle.MailSettings = mailSettings = value;
31-
}
32+
}
3233

3334
protected AbstractCommand()
3435
{
@@ -38,21 +39,26 @@ protected AbstractCommand()
3839
SchedulerGroup = GetType().AssemblyQualifiedName;
3940
}
4041

41-
public abstract Task Run();
42+
public virtual void Run()
43+
{
44+
}
45+
46+
public virtual async Task RunAsync() => await Task.Run(Run).ConfigureAwait(false);
4247

4348
public virtual async Task RunWithLifeCycle()
4449
{
4550
try
4651
{
4752
CommandLifeCycle.FirePreActions(this);
4853
if (SecondsBeforeTimeout <= 0)
49-
await Run();
54+
await RunAsync();
5055
else
5156
{
52-
var task = Task.Run(Run);
57+
var task = Task.Run(RunAsync);
5358
if (!task.Wait(TimeSpan.FromSeconds(SecondsBeforeTimeout)))
5459
throw new Exception($"Scheduled command timed out after {SecondsBeforeTimeout} second(s).");
5560
}
61+
5662
CommandLifeCycle.FirePostActions(this);
5763
}
5864
catch (Exception exception)
@@ -65,32 +71,39 @@ public virtual async Task RunWithLifeCycle()
6571
}
6672

6773
public virtual void Failed(Exception exception)
68-
{}
74+
{
75+
}
6976

7077
#region ProgressBar
78+
7179
public void SetProgressBarMaxTicks(int maxTicks) => ProgressBar = new ProgressBar(maxTicks);
7280

7381
public void UpdateProgress(int tick) => ProgressBar.Refresh(tick, Name);
7482

7583
#endregion
7684

7785
#region Scheduling
86+
7887
public virtual async Task Execute(IJobExecutionContext context)
7988
{
8089
if (context != null)
8190
{
8291
var properties = GetType().GetProperties();
83-
context.MergedJobDataMap.ToList().ForEach(x => {
92+
context.MergedJobDataMap.ToList().ForEach(x =>
93+
{
8494
var property = properties.FirstOrDefault(y => y.Name == x.Key);
8595
if (property != null)
8696
property.SetValue(this, x.Value);
8797
});
8898
}
99+
89100
await RunWithLifeCycle();
90101
}
102+
91103
#endregion
92104

93105
#region Console output
106+
94107
public IWrite Info(object val) => Write.Info(val);
95108
public IWrite Warning(object val) => Write.Warning(val);
96109
public IWrite Error(object val) => Write.Error(val);

src/InEngine.Core/Commands/Chain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public class Chain : AbstractCommand
1111
{
1212
public IList<AbstractCommand> Commands { get; set; } = new List<AbstractCommand>();
1313

14-
public override async Task Run()
14+
public override async Task RunAsync()
1515
{
1616
foreach (var x in Commands.ToList())
1717
{
1818
try
1919
{
2020
x.WriteSummaryToConsole();
21-
await x.Run();
21+
await x.RunAsync();
2222
}
2323
catch (Exception exception)
2424
{

src/InEngine.Core/Commands/Echo.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Threading.Tasks;
2-
using CommandLine;
1+
using CommandLine;
32

43
namespace InEngine.Core.Commands;
54

@@ -17,5 +16,5 @@ public Echo()
1716
[Option("text", HelpText = "The text to echo.")]
1817
public string VerbatimText { get; init; }
1918

20-
public override async Task Run() => await Task.Run(() => Line(VerbatimText));
19+
public override void Run() => Line(VerbatimText);
2120
}

0 commit comments

Comments
 (0)