Skip to content

Commit 0f22d98

Browse files
committed
Refactor broker and add failed method
1 parent e16fc54 commit 0f22d98

File tree

23 files changed

+116
-106
lines changed

23 files changed

+116
-106
lines changed

docs-src/commands.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ namespace MyCommandPlugin
3636
{
3737
public class MyCommand : ICommand
3838
{
39-
public CommandResult Run()
39+
public void Run()
4040
{
4141
Console.WriteLine("Hello, world!");
42-
return new CommandResult(true);
42+
return new void(true);
4343
}
4444
}
4545
}
@@ -56,10 +56,10 @@ namespace MyCommandPlugin
5656
{
5757
public class MyCommand : AbstractCommand
5858
{
59-
public override CommandResult Run()
59+
public override void Run()
6060
{
6161
Console.WriteLine("Hello, world!");
62-
return new CommandResult(true);
62+
return new void(true);
6363
}
6464
}
6565
}
@@ -188,7 +188,7 @@ IWrite Line(string val);
188188
```
189189

190190
```c#
191-
public override CommandResult Run()
191+
public override void Run()
192192
{
193193
Info("Display some information");
194194
}
@@ -215,15 +215,15 @@ Line("This is a plain line.");
215215
The **InEngine.Core.AbstractCommand** class provides a Logger property. It implements the **NLog.ILogger** interface.
216216

217217
```c#
218-
public override CommandResult Run()
218+
public override void Run()
219219
{
220220
Logger.Trace("Sample trace message");
221221
Logger.Debug("Sample debug message");
222222
Logger.Info("Sample informational message");
223223
Logger.Warn("Sample warning message");
224224
Logger.Error("Sample error message");
225225
Logger.Fatal("Sample fatal error message");
226-
return new CommandResult(true);
226+
return new void(true);
227227
}
228228
```
229229

@@ -251,7 +251,7 @@ Setup an [NLog configuration](https://github.com/NLog/NLog/wiki/Tutorial#configu
251251
The **InEngine.Core.AbstractCommand** class provides a ProgressBar property. This is how it is used.
252252

253253
```c#
254-
public override CommandResult Run()
254+
public override void Run()
255255
{
256256
// Define the ticks (aka steps) for the command...
257257
var maxTicks = 100000;
@@ -264,7 +264,7 @@ public override CommandResult Run()
264264
UpdateProgress(i);
265265
}
266266

267-
return new CommandResult(true);
267+
return new void(true);
268268
}
269269
```
270270

src/InEngine.Commands/Sample/Minimal.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ namespace InEngine.Commands.Sample
1111
*/
1212
public class Minimal : ICommand
1313
{
14-
public CommandResult Run()
14+
public void Run()
1515
{
16-
return new CommandResult(true);
1716
}
1817
}
1918
}

src/InEngine.Commands/Sample/SayHello.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ namespace InEngine.Commands.Sample
55
{
66
public class SayHello : AbstractCommand
77
{
8-
public override CommandResult Run()
8+
public override void Run()
99
{
1010
Console.WriteLine("hello");
11-
return new CommandResult(true);
1211
}
1312
}
1413
}

src/InEngine.Commands/Sample/ShowProgress.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ShowProgress : AbstractCommand
1212
* Note that the override keyword is necessary in the Run method
1313
* signature as the base class method is virtual.
1414
*/
15-
public override CommandResult Run()
15+
public override void Run()
1616
{
1717
// Define the ticks (aka steps) for the command...
1818
var maxTicks = 100000;
@@ -24,8 +24,6 @@ public override CommandResult Run()
2424
// Update the command's progress
2525
UpdateProgress(i);
2626
}
27-
28-
return new CommandResult(true);
2927
}
3028
}
3129
}

src/InEngine.Core.Tests/Queue/Commands/ConsumeTest.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,37 @@ public void Setup()
2222
[Test]
2323
public void ShouldConsumePrimaryQueue()
2424
{
25-
var expectedMessage = "Consumed";
2625
new Publish() {
27-
Command = new Null()
26+
Command = new AlwaysSucceed()
2827
}.Run();
2928

30-
var result = Subject.Run();
31-
32-
Assert.IsTrue(result.IsSuccessful);
33-
Assert.AreEqual(expectedMessage, result.Message);
29+
Subject.Run();
3430
}
3531

3632
[Test]
3733
public void ShouldConsumeSecondaryQueue()
3834
{
3935
var expectedMessage = "Consumed";
4036
new Publish() {
41-
Command = new Null()
37+
Command = new AlwaysSucceed()
4238
}.Run();
4339
Subject.UseSecondaryQueue = true;
4440

45-
var result = Subject.Run();
46-
47-
Assert.IsTrue(result.IsSuccessful);
48-
Assert.AreEqual(expectedMessage, result.Message);
41+
Subject.Run();
4942
}
5043

5144
[Test]
5245
public void ShouldConsumeSecondaryQueueBasedOnJobContextData()
5346
{
54-
var expectedMessage = "Consumed";
5547
new Publish() {
56-
Command = new Null()
48+
Command = new AlwaysSucceed()
5749
}.Run();
5850
var jobExecutionConext = new Mock<IJobExecutionContext>();
5951
var jobDataMap = new JobDataMap { { "useSecondaryQueue", true } };
6052
jobExecutionConext.SetupGet(p => p.JobDetail.JobDataMap).Returns(jobDataMap);
6153
Subject.JobExecutionContext = jobExecutionConext.Object;
6254

63-
var result = Subject.Run();
64-
65-
Assert.IsTrue(result.IsSuccessful);
66-
Assert.AreEqual(expectedMessage, result.Message);
55+
Subject.Run();
6756
}
6857
}
6958
}

src/InEngine.Core.Tests/Queue/Commands/PublishTest.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,31 @@ public void Setup()
2121
[Test]
2222
public void ShouldPublishCommandObject()
2323
{
24-
var expectedMessage = "Published";
25-
Subject.Command = new Null();
24+
Subject.Command = new AlwaysSucceed();
2625

27-
var result = Subject.Run();
28-
29-
Assert.IsTrue(result.IsSuccessful);
30-
Assert.AreEqual(expectedMessage, result.Message);
26+
Subject.Run();
3127
}
3228

3329
[Test]
3430
public void ShouldPublishCommandByArgs()
3531
{
36-
var expectedMessage = "Published";
37-
var nullCommand = new Null();
32+
var nullCommand = new AlwaysSucceed();
3833
Subject.CommandAssembly = nullCommand.GetType().Assembly.GetName().Name + ".dll";
3934
Subject.CommandClass = nullCommand.GetType().FullName;
4035

41-
var result = Subject.Run();
42-
43-
Assert.IsTrue(result.IsSuccessful);
44-
Assert.AreEqual(expectedMessage, result.Message);
36+
Subject.Run();
4537
}
4638

4739
[Test]
4840
public void ShouldPublishManyCommands()
4941
{
50-
//var expectedMessage = "Published";
51-
Subject.Command = new Null();
52-
var results = new List<CommandResult>();
53-
54-
foreach(var i in Enumerable.Range(0, 200).ToList())
55-
results.Add(Subject.Run());
5642

57-
//Assert.IsTrue(result.IsSuccessful);
58-
//Assert.AreEqual(expectedMessage, result.Message);
43+
foreach (var i in Enumerable.Range(0, 200).ToList()) {
44+
Subject.Command = new Echo() {
45+
Text = $"test job: {i}"
46+
};
47+
Subject.Run();
48+
}
5949
}
6050

6151
[Test]

src/InEngine.Core/AbstractCommand.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace InEngine.Core
88
{
9-
abstract public class AbstractCommand : ICommand, IJob, IWrite
9+
abstract public class AbstractCommand : ICommand, IFailed, IJob, IWrite
1010
{
1111
public IJobExecutionContext JobExecutionContext { get; set; }
1212
public ILogger Logger { get; internal set; }
@@ -33,11 +33,14 @@ protected AbstractCommand()
3333
Write = new Write();;
3434
}
3535

36-
public virtual CommandResult Run()
36+
public virtual void Run()
3737
{
3838
throw new NotImplementedException();
3939
}
4040

41+
public virtual void Failed(Exception exception)
42+
{}
43+
4144
#region ProgressBar
4245
public void SetProgressBarMaxTicks(int maxTicks)
4346
{
@@ -54,7 +57,14 @@ public void UpdateProgress(int tick)
5457
public void Execute(IJobExecutionContext context)
5558
{
5659
JobExecutionContext = context;
57-
Run();
60+
try
61+
{
62+
Run();
63+
}
64+
catch (Exception exception)
65+
{
66+
Failed(exception);
67+
}
5868
}
5969

6070
public T GetJobContextData<T>(string key)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using InEngine.Core.Exceptions;
3+
4+
namespace InEngine.Core.Commands
5+
{
6+
/// <summary>
7+
/// Dummy command for testing and sample code.
8+
/// </summary>
9+
public class AlwaysFail : AbstractCommand
10+
{
11+
public override void Run()
12+
{
13+
throw new CommandFailedException("This command always fails.");
14+
}
15+
16+
public override void Failed(Exception exception)
17+
{
18+
Error(exception.Message);
19+
}
20+
}
21+
}

src/InEngine.Core/Commands/Null.cs renamed to src/InEngine.Core/Commands/AlwaysSucceed.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
/// <summary>
44
/// Dummy command for testing and sample code.
55
/// </summary>
6-
public class Null : AbstractCommand
6+
public class AlwaysSucceed : AbstractCommand
77
{
8-
public override CommandResult Run()
8+
public override void Run()
99
{
10-
return new CommandResult(true);
1110
}
1211
}
1312
}

src/InEngine.Core/Commands/Echo.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ public class Echo : AbstractCommand
1111
[Option("text", HelpText = "The text to echo.")]
1212
public string Text { get; set; }
1313

14-
public override CommandResult Run()
14+
public override void Run()
1515
{
16-
Console.WriteLine(Text);
17-
return new CommandResult(true);
16+
Write.Line(Text);
1817
}
1918
}
2019
}

0 commit comments

Comments
 (0)