Skip to content

Commit 6a82468

Browse files
committed
Rework retry, add consume output, remove database driver
1 parent 6f3db3e commit 6a82468

17 files changed

+136
-351
lines changed

src/InEngine.Core/AbstractCommand.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Threading.Tasks;
4+
using InEngine.Core.Exceptions;
45
using InEngine.Core.IO;
56
using Konsole;
67
using Quartz;
@@ -32,6 +33,28 @@ public virtual void Run()
3233
throw new NotImplementedException();
3334
}
3435

36+
public virtual void RunWithLifeCycle()
37+
{
38+
try
39+
{
40+
CommandLifeCycle.FirePreActions(this);
41+
if (SecondsBeforeTimeout <= 0)
42+
Run();
43+
else
44+
{
45+
var task = Task.Run(() => Run());
46+
if (!task.Wait(TimeSpan.FromSeconds(SecondsBeforeTimeout)))
47+
throw new Exception($"Scheduled command timed out after {SecondsBeforeTimeout} second(s).");
48+
}
49+
CommandLifeCycle.FirePostActions(this);
50+
}
51+
catch (Exception exception)
52+
{
53+
Failed(exception);
54+
throw new CommandFailedException("Command failed. See inner exception for details.", exception);
55+
}
56+
}
57+
3558
public virtual void Failed(Exception exception)
3659
{}
3760

@@ -50,31 +73,17 @@ public void UpdateProgress(int tick)
5073
#region Scheduling
5174
public virtual void Execute(IJobExecutionContext context)
5275
{
53-
if (context != null) {
76+
if (context != null)
77+
{
5478
var properties = GetType().GetProperties();
5579
context.MergedJobDataMap.ToList().ForEach(x => {
5680
var property = properties.FirstOrDefault(y => y.Name == x.Key);
5781
if (property != null)
58-
property.SetValue(this, x.Value);
82+
property.SetValue(this, x.Value);
5983
});
6084
}
6185

62-
try
63-
{
64-
CommandLifeCycle.FirePreActions(this);
65-
if (SecondsBeforeTimeout <= 0)
66-
Run();
67-
else {
68-
var task = Task.Run(() => Run());
69-
if (!task.Wait(TimeSpan.FromSeconds(SecondsBeforeTimeout)))
70-
throw new Exception($"Scheduled command timed out after {SecondsBeforeTimeout} second(s).");
71-
}
72-
CommandLifeCycle.FirePostActions(this);
73-
}
74-
catch (Exception exception)
75-
{
76-
Failed(exception);
77-
}
86+
RunWithLifeCycle();
7887
}
7988
#endregion
8089

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
namespace InEngine.Core
3+
{
4+
public static class CommandExtensions
5+
{
6+
public static void WriteSummaryToConsole(this AbstractCommand command)
7+
{
8+
Console.WriteLine($"- {command.Name} : {command.ScheduleId} | Try #{command.CommandLifeCycle.CurrentTry}");
9+
}
10+
}
11+
}

src/InEngine.Core/CommandLifeCycle.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,33 @@ public class CommandLifeCycle
1111

1212
public bool ShouldPingBefore { get; set; }
1313
public string PingBeforeUrl { get; set; }
14+
1415
public bool ShouldPingAfter { get; set; }
1516
public string PingAfterUrl { get; set; }
1617

17-
1818
public bool ShouldWriteOutputToFile { get; set; }
1919
public string WriteOutputToFilePath { get; set; }
20+
2021
public bool ShouldAppendOutputToFile { get; set; }
2122
public string AppendOutputToFilePath { get; set; }
23+
2224
public bool ShouldEmailOutput { get; set; }
2325
public string EmailOutputToAddress { get; set; }
2426

27+
public int MaximumRetries { get; set; }
28+
public int CurrentTry { get; set; }
29+
30+
public CommandLifeCycle IncrementRetry()
31+
{
32+
CurrentTry++;
33+
return this;
34+
}
35+
36+
public bool ShouldRetry()
37+
{
38+
return CurrentTry <= MaximumRetries;
39+
}
40+
2541
public void FirePreActions(AbstractCommand command)
2642
{
2743
try

src/InEngine.Core/InEngine.Core.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
<ItemGroup>
3535
<Folder Include="Scheduling\" />
3636
<Folder Include="Queuing\Clients\" />
37-
<Folder Include="Queuing\Clients\Database\" />
3837
<Folder Include="Scheduling\Commands\" />
3938
</ItemGroup>
4039
<ItemGroup>

src/InEngine.Core/Queuing/Clients/Database/CommandEnvelopeModel.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/InEngine.Core/Queuing/Clients/Database/CommandEnvelopeStatus.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/InEngine.Core/Queuing/Clients/Database/QueueDbContext.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)