Skip to content

Commit ba2408e

Browse files
committed
Flesh out execution life cycle actions
1 parent 150d68d commit ba2408e

File tree

14 files changed

+266
-12
lines changed

14 files changed

+266
-12
lines changed

src/InEngine.Core/AbstractCommand.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
using System;
22
using System.Linq;
3+
using InEngine.Core.Exceptions;
34
using InEngine.Core.IO;
45
using InEngine.Core.Scheduling;
56
using Konsole;
67
using Quartz;
8+
using RestSharp;
79

810
namespace InEngine.Core
911
{
1012
abstract public class AbstractCommand : ICommand, IFailed, IJob, IWrite
1113
{
12-
public LifecycleActions LifecycleActions { get; set; }
14+
public ExecutionLifeCycle ExecutionLifeCycle { get; set; }
1315
public Write Write { get; set; }
1416
public ProgressBar ProgressBar { get; internal set; }
1517
public string Name { get; set; }
@@ -22,7 +24,7 @@ protected AbstractCommand()
2224
Name = GetType().FullName;
2325
SchedulerGroup = GetType().AssemblyQualifiedName;
2426
Write = new Write();
25-
LifecycleActions = new LifecycleActions();
27+
ExecutionLifeCycle = new ExecutionLifeCycle();
2628
}
2729

2830
public virtual void Run()
@@ -57,9 +59,9 @@ public void Execute(IJobExecutionContext context)
5759

5860
try
5961
{
60-
LifecycleActions.BeforeAction?.Invoke(this);
62+
ExecutionLifeCycle.FirePreActions(this);
6163
Run();
62-
LifecycleActions.AfterAction?.Invoke(this);
64+
ExecutionLifeCycle.FirePostActions(this);
6365
}
6466
catch (Exception exception)
6567
{
@@ -124,6 +126,16 @@ public IWrite Newline(int count = 1)
124126
{
125127
return Write.Newline(count);
126128
}
129+
130+
public string FlushBuffer()
131+
{
132+
return Write.FlushBuffer();
133+
}
134+
135+
public void ToFile(string path, string text, bool shouldAppend = false)
136+
{
137+
Write.ToFile(path, text, shouldAppend);
138+
}
127139
#endregion
128140
}
129141
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace InEngine.Core.Exceptions
4+
{
5+
public class LifecycleActionFailedException : Exception
6+
{
7+
public LifecycleActionFailedException(string message, Exception exception) : base(message, exception)
8+
{}
9+
}
10+
}

src/InEngine.Core/IO/Http.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using RestSharp;
2+
3+
namespace InEngine.Core.IO
4+
{
5+
public class Http
6+
{
7+
public string Get(string url)
8+
{
9+
return new RestClient(url).Execute(new RestRequest(string.Empty, Method.GET)).Content;
10+
}
11+
}
12+
}

src/InEngine.Core/IO/IWrite.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ public interface IWrite
1616
IWrite ErrorText(string val);
1717
IWrite Text(string val);
1818
IWrite ColoredText(string val, ConsoleColor consoleColor);
19+
20+
string FlushBuffer();
21+
void ToFile(string path, string text, bool shouldAppend = false);
1922
}
2023
}

src/InEngine.Core/IO/Mail.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Net.Mail;
3+
4+
namespace InEngine.Core.IO
5+
{
6+
public class Mail
7+
{
8+
public string Host { get; set; }
9+
public int Port { get; set; }
10+
11+
public void Send(string fromAddress, string toAddress, string subject, string body)
12+
{
13+
new SmtpClient() {
14+
Host = Host,
15+
Port = Port
16+
}.Send(new MailMessage(fromAddress, toAddress, subject, body));
17+
}
18+
}
19+
}

src/InEngine.Core/IO/Write.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading;
26

37
namespace InEngine.Core.IO
48
{
59
public class Write : IWrite
610
{
11+
static Mutex mutexLock = new Mutex();
12+
713
public ConsoleColor InfoColor { get; set; } = ConsoleColor.Green;
814
public ConsoleColor WarningColor { get; set; } = ConsoleColor.Yellow;
915
public ConsoleColor ErrorColor { get; set; } = ConsoleColor.Red;
1016
public ConsoleColor LineColor { get; set; } = ConsoleColor.White;
17+
public List<string> Buffer { get; set; } = new List<string>();
1118

1219
public IWrite Newline(int count = 1)
1320
{
@@ -38,9 +45,12 @@ public IWrite Line(string val)
3845

3946
public IWrite ColoredLine(string val, ConsoleColor consoleColor)
4047
{
48+
mutexLock.WaitOne();
4149
Console.ForegroundColor = consoleColor;
4250
Console.WriteLine(val);
4351
Console.ResetColor();
52+
mutexLock.ReleaseMutex();
53+
Buffer.Add(val);
4454
return this;
4555
}
4656

@@ -66,10 +76,30 @@ public IWrite Text(string val)
6676

6777
public IWrite ColoredText(string val, ConsoleColor consoleColor)
6878
{
79+
mutexLock.WaitOne();
6980
Console.ForegroundColor = consoleColor;
7081
Console.Write(val);
7182
Console.ResetColor();
83+
mutexLock.ReleaseMutex();
84+
Buffer.Add(val);
7285
return this;
7386
}
87+
88+
public string FlushBuffer()
89+
{
90+
var str = string.Join("\n", Buffer);
91+
Buffer.Clear();
92+
return str;
93+
}
94+
95+
public void ToFile(string path, string text, bool shouldAppend = false)
96+
{
97+
if (!File.Exists(path))
98+
File.Create(path);
99+
if (shouldAppend)
100+
File.AppendAllText(path, text);
101+
else
102+
File.WriteAllText(path, text);
103+
}
74104
}
75105
}

src/InEngine.Core/InEngine.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<PackageReference Include="StackExchange.Redis" Version="1.2.4" />
2828
<PackageReference Include="Goblinfactory.Konsole" Version="3.1.0" />
2929
<PackageReference Include="EntityFramework" Version="6.2.0" />
30+
<PackageReference Include="RestSharp" Version="106.1.0" />
3031
</ItemGroup>
3132
<ItemGroup>
3233
<Folder Include="Scheduling\" />

src/InEngine.Core/InEngineSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class InEngineSettings
1111
public static string ConfigurationFile { get; set; } = "appsettings.json";
1212
public List<string> Plugins { get; set; } = new List<string>();
1313
public QueueSettings Queue { get; set; }
14+
public MailSettings Mail { get; set; }
1415

1516
public static InEngineSettings Make()
1617
{

src/InEngine.Core/MailSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
namespace InEngine.Core
3+
{
4+
public class MailSettings
5+
{
6+
public string Host { get; set; }
7+
public int Port { get; set; }
8+
public string From { get; set; }
9+
}
10+
}

src/InEngine.Core/Queuing/Commands/Consume.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public class Consume : AbstractCommand
1313

1414
public override void Run()
1515
{
16-
var broker = Queue.Make(UseSecondaryQueue);
16+
var queue = Queue.Make(UseSecondaryQueue);
1717
var shouldConsume = true;
1818
while (shouldConsume)
19-
shouldConsume = broker.Consume() && ShouldConsumeAll;
19+
shouldConsume = queue.Consume() && ShouldConsumeAll;
2020
}
2121

2222
public override void Failed(Exception exception)

0 commit comments

Comments
 (0)