Skip to content

Commit 325368c

Browse files
committed
WIP Upgrade quartz and convert tasks to async
1 parent f269aff commit 325368c

File tree

24 files changed

+152
-94
lines changed

24 files changed

+152
-94
lines changed

src/InEngine.Commands/AlwaysFail.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using InEngine.Core;
34
using InEngine.Core.Exceptions;
45

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

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

34
namespace InEngine.Commands;
45

@@ -7,5 +8,8 @@ namespace InEngine.Commands;
78
/// </summary>
89
public class AlwaysSucceed : AbstractCommand
910
{
10-
public override void Run() => Info("This command always succeeds.");
11+
public override async Task Run()
12+
{
13+
await Task.Run(() => Info("This command always succeeds."));
14+
}
1115
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using InEngine.Core;
1+
using System.Threading.Tasks;
2+
using InEngine.Core;
23

34
namespace InEngine.Commands.Sample;
45

6+
using System;
7+
58
/*
69
* At a minimum, a command must implement the AbstractCommand interface.
710
*
@@ -11,7 +14,8 @@ namespace InEngine.Commands.Sample;
1114
*/
1215
public class Minimal : AbstractCommand
1316
{
14-
public override void Run()
17+
public override async Task Run()
1518
{
19+
await Task.Run(() => Console.WriteLine("This is an example of a minimal command."));
1620
}
17-
}
21+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Threading;
22
using InEngine.Core;
3+
using System.Threading.Tasks;
34

45
namespace InEngine.Commands.Sample;
56

67
public class Pause : AbstractCommand
78
{
8-
public override void Run()
9+
public override async Task Run()
910
{
10-
Thread.Sleep(3000);
11+
await Task.Run(() => Thread.Sleep(3000));
1112
}
1213
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using InEngine.Core;
22
using System;
3+
using System.Threading.Tasks;
34

45
namespace InEngine.Commands.Sample;
56

67
public class SayHello : AbstractCommand
78
{
8-
public override void Run()
9+
public override async Task Run()
910
{
10-
Console.WriteLine("hello");
11+
await Task.Run(() => Console.WriteLine("hello"));
1112
}
1213
}

src/InEngine.Commands/Sample/ShowProgress.cs

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

34
namespace InEngine.Commands.Sample;
45

@@ -12,7 +13,7 @@ public class ShowProgress : AbstractCommand
1213
* Note that the override keyword is necessary in the Run method
1314
* signature as the base class method is virtual.
1415
*/
15-
public override void Run()
16+
public override async Task Run()
1617
{
1718
// Define the ticks (aka steps) for the command...
1819
var maxTicks = 100000;
@@ -22,7 +23,7 @@ public override void Run()
2223
for (var i = 0; i <= maxTicks;i++)
2324
{
2425
// Update the command's progress
25-
UpdateProgress(i);
26+
await Task.Run(() => UpdateProgress(i));
2627
}
2728
}
2829
}

src/InEngine.Core.Test/InEngine.Core.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<PackageReference Include="NUnit" Version="3.9.0" />
1919
<PackageReference Include="NUnit.Console" Version="3.7.0" />
2020
<PackageReference Include="NUnit.ConsoleRunner" Version="3.7.0" />
21-
<PackageReference Include="Quartz" Version="2.6.1" />
21+
<PackageReference Include="Quartz" Version="3.4.0" />
2222
</ItemGroup>
2323
<ItemGroup>
2424
<ProjectReference Include="..\InEngine.Core\InEngine.Core.csproj" />

src/InEngine.Core/AbstractCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ protected AbstractCommand()
3838
SchedulerGroup = GetType().AssemblyQualifiedName;
3939
}
4040

41-
public virtual void Run() => throw new NotImplementedException();
41+
public abstract Task Run();
4242

43-
public virtual void RunWithLifeCycle()
43+
public virtual async Task RunWithLifeCycle()
4444
{
4545
try
4646
{
4747
CommandLifeCycle.FirePreActions(this);
4848
if (SecondsBeforeTimeout <= 0)
49-
Run();
49+
await Run();
5050
else
5151
{
5252
var task = Task.Run(Run);
@@ -75,7 +75,7 @@ public virtual void Failed(Exception exception)
7575
#endregion
7676

7777
#region Scheduling
78-
public virtual void Execute(IJobExecutionContext context)
78+
public virtual async Task Execute(IJobExecutionContext context)
7979
{
8080
if (context != null)
8181
{
@@ -86,7 +86,7 @@ public virtual void Execute(IJobExecutionContext context)
8686
property.SetValue(this, x.Value);
8787
});
8888
}
89-
RunWithLifeCycle();
89+
await RunWithLifeCycle();
9090
}
9191
#endregion
9292

src/InEngine.Core/Commands/Chain.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
using System.Linq;
44
using InEngine.Core.Exceptions;
55
using Microsoft.Extensions.Logging;
6+
using System.Threading.Tasks;
67

78
namespace InEngine.Core.Commands;
89

910
public class Chain : AbstractCommand
1011
{
1112
public IList<AbstractCommand> Commands { get; set; } = new List<AbstractCommand>();
1213

13-
public override void Run()
14+
public override async Task Run()
1415
{
15-
Commands.ToList().ForEach(x =>
16+
foreach (var x in Commands.ToList())
1617
{
1718
try
1819
{
1920
x.WriteSummaryToConsole();
20-
x.Run();
21+
await x.Run();
2122
}
2223
catch (Exception exception)
2324
{
2425
Log.LogError(exception, "Chain command failed");
2526
x.Failed(exception);
2627
throw new CommandChainFailedException(x.Name, exception);
2728
}
28-
});
29+
}
2930
}
3031
}

src/InEngine.Core/Commands/Echo.cs

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

34
namespace InEngine.Core.Commands;
45

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

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

0 commit comments

Comments
 (0)