Skip to content

Commit 32fe921

Browse files
committed
Auto register jobs
1 parent 9903db2 commit 32fe921

13 files changed

+3221
-34
lines changed

src/InEngine.Core/IJobs.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Quartz;
2+
3+
namespace InEngine.Core
4+
{
5+
public interface IJobs : IPluginType
6+
{
7+
void Schedule(IScheduler scheduler);
8+
}
9+
}

src/InEngine.Core/IOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace InEngine.Core
22
{
3-
public interface IOptions
3+
public interface IOptions : IPluginType
44
{
55
string GetUsage(string verb);
66
}

src/InEngine.Core/IPluginType.cs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace InEngine.Core
2+
{
3+
public interface IPluginType
4+
{}
5+
}

src/InEngine.Core/InEngine.Core.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
<Compile Include="IOptions.cs" />
9191
<Compile Include="Queue\Options.cs" />
9292
<Compile Include="Plugin.cs" />
93+
<Compile Include="IJobs.cs" />
94+
<Compile Include="IPluginType.cs" />
9395
</ItemGroup>
9496
<ItemGroup>
9597
<None Include="packages.config" />

src/InEngine.Core/Plugin.cs

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Reflection;
6+
using NLog;
57

68
namespace InEngine.Core
79
{
810
public class Plugin
911
{
10-
public Assembly Assembly { get; set; }
11-
public string Name { get { return Assembly.GetName().Name; } }
12+
public Assembly Assembly { get; set; }
13+
public string Name { get { return Assembly.GetName().Name; } }
1214

1315
public Plugin(Assembly assembly)
1416
{
1517
Assembly = assembly;
1618
}
1719

18-
public List<IOptions> MakeOptions()
20+
public List<T> Make<T>() where T : class, IPluginType
1921
{
2022
return Assembly
2123
.GetTypes()
22-
.Where(x => x.IsClass && typeof(IOptions).IsAssignableFrom(x))
23-
.Select(x => Assembly.CreateInstance(x.FullName) as IOptions)
24+
.Where(x => x.IsClass && typeof(T).IsAssignableFrom(x))
25+
.Select(x => Assembly.CreateInstance(x.FullName) as T)
2426
.ToList();
2527
}
28+
29+
public static List<Plugin> Discover<T>() where T : IPluginType
30+
{
31+
var discoveredAssemblies = Directory
32+
.GetFiles(".", "*.dll")
33+
.Select(x => Assembly.LoadFrom(x));
34+
var pluginList = new List<Plugin>();
35+
foreach (var assembly in discoveredAssemblies)
36+
{
37+
try
38+
{
39+
if (assembly.GetTypes().Any(y => y.IsClass && typeof(T).IsAssignableFrom(y)))
40+
pluginList.Add(new Plugin(assembly));
41+
}
42+
catch (Exception exception)
43+
{
44+
LogManager.GetCurrentClassLogger().Error(exception, "Error discovering plugins");
45+
}
46+
}
47+
return pluginList;
48+
}
2649
}
2750
}

src/InEngine.Core/Queue/Jobs.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace InEngine.Core.Queue
66
{
7-
public static class Jobs
7+
public class Jobs : IJobs
88
{
9-
public static void Schedule(IScheduler scheduler)
9+
public void Schedule(IScheduler scheduler)
1010
{
1111
var consume = new Consume();
1212
var jobDetails = JobBuilder

src/InEngine.Core/packages.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<package id="Common.Logging" version="3.4.1" targetFramework="net462" />
55
<package id="Common.Logging.Core" version="3.4.1" targetFramework="net462" />
66
<package id="Goblinfactory.Konsole.ProgressBar.Core" version="1.0.2" targetFramework="net462" />
7-
<package id="Microsoft.CSharp" version="4.0.1" targetFramework="net462" />
7+
<package id="Microsoft.CSharp" version="4.4.0" targetFramework="net462" />
88
<package id="Microsoft.Diagnostics.Tracing.EventSource.Redist" version="1.1.28" targetFramework="net462" />
99
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net462" />
1010
<package id="NLog" version="4.4.12" targetFramework="net462" />

src/InEngineCli/ArgumentInterpreter.cs

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
42
using System.Linq;
53
using System.Reflection;
64
using CommandLine;
@@ -21,7 +19,7 @@ public ArgumentInterpreter()
2119

2220
public void Interpret(string[] args)
2321
{
24-
var plugins = FindPlugins();
22+
var plugins = Plugin.Discover<IOptions>();
2523
if (!args.Any())
2624
{
2725
Console.WriteLine("Available plugins... ");
@@ -41,7 +39,7 @@ public void Interpret(string[] args)
4139
if (plugin == null)
4240
ExitWithFailure("Plugin does not exist: " + options.PluginName);
4341

44-
var pluginOptionList = plugin.MakeOptions();
42+
var pluginOptionList = plugin.Make<IOptions>();
4543

4644
var pluginArgs = args.Skip(1).ToArray();
4745
if (!pluginArgs.ToList().Any()) {
@@ -90,16 +88,6 @@ protected string MakeErrorMessage(string message = null)
9088
return $"✘ {message}";
9189
}
9290

93-
public List<Plugin> FindPlugins()
94-
{
95-
return Directory
96-
.GetFiles(".", "*.dll")
97-
.Select(x => Assembly.LoadFrom(x))
98-
.Where(x => x.GetTypes().Any(y => y.IsClass && typeof(IOptions).IsAssignableFrom(y)))
99-
.Select(x => new Plugin(x))
100-
.ToList();
101-
}
102-
10391
public void InterpretPluginArguments(string[] pluginArgs, IOptions pluginOptions)
10492
{
10593
var isSuccessful = Parser

src/InEngineScheduler/InEngineScheduler.csproj

+12-6
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,22 @@
2828
</PropertyGroup>
2929
<ItemGroup>
3030
<Reference Include="System" />
31-
<Reference Include="Common.Logging.Core">
32-
<HintPath>..\packages\Common.Logging.Core.3.3.1\lib\net40\Common.Logging.Core.dll</HintPath>
33-
</Reference>
34-
<Reference Include="Common.Logging">
35-
<HintPath>..\packages\Common.Logging.3.3.1\lib\net40\Common.Logging.dll</HintPath>
36-
</Reference>
3731
<Reference Include="Quartz">
3832
<HintPath>..\packages\Quartz.2.6.1\lib\net40\Quartz.dll</HintPath>
3933
</Reference>
4034
<Reference Include="System.Configuration" />
4135
<Reference Include="System.ServiceProcess" />
4236
<Reference Include="System.Configuration.Install" />
37+
<Reference Include="Microsoft.CSharp" />
38+
<Reference Include="Common.Logging.Core">
39+
<HintPath>..\packages\Common.Logging.Core.3.4.1\lib\net40\Common.Logging.Core.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Common.Logging">
42+
<HintPath>..\packages\Common.Logging.3.4.1\lib\net40\Common.Logging.dll</HintPath>
43+
</Reference>
44+
<Reference Include="NLog">
45+
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
46+
</Reference>
4347
</ItemGroup>
4448
<ItemGroup>
4549
<Compile Include="Program.cs" />
@@ -51,6 +55,8 @@
5155
<ItemGroup>
5256
<None Include="packages.config" />
5357
<None Include="job_scheduling_data_2_0.xsd" />
58+
<None Include="NLog.xsd" />
59+
<None Include="NLog.config" />
5460
</ItemGroup>
5561
<ItemGroup>
5662
<ProjectReference Include="..\InEngine.Core\InEngine.Core.csproj">

src/InEngineScheduler/Jobs.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
using InEngine.Commands.Sample;
2-
using QueueJobs = InEngine.Core.Queue.Jobs;
1+
using System;
2+
using InEngine.Core;
33
using Quartz;
4+
using NLog;
5+
46

57
namespace InEngineScheduler
68
{
79
public static class Jobs
810
{
911
public static void Schedule(IScheduler scheduler)
1012
{
11-
QueueJobs.Schedule(scheduler);
13+
var logger = LogManager.GetCurrentClassLogger();
14+
Plugin.Discover<IJobs>().ForEach(x => {
15+
logger.Info($"Registering jobs from plugin: {x.Name}");
16+
x.Make<IJobs>().ForEach(y => y.Schedule(scheduler));
17+
});
1218
}
1319
}
1420
}

src/InEngineScheduler/NLog.config

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
5+
autoReload="true"
6+
throwExceptions="false"
7+
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
8+
9+
<!-- optional, add some variables
10+
https://github.com/nlog/NLog/wiki/Configuration-file#variables
11+
-->
12+
<variable name="myvar" value="myvalue"/>
13+
14+
<!--
15+
See https://github.com/nlog/nlog/wiki/Configuration-file
16+
for information on customizing logging rules and outputs.
17+
-->
18+
<targets>
19+
20+
<!--
21+
add your targets here
22+
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
23+
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
24+
-->
25+
26+
<!--
27+
Write events to a file with the date in the filename.
28+
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
29+
layout="${longdate} ${uppercase:${level}} ${message}" />
30+
-->
31+
</targets>
32+
33+
<rules>
34+
<!-- add your logging rules here -->
35+
36+
<!--
37+
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
38+
<logger name="*" minlevel="Debug" writeTo="f" />
39+
-->
40+
</rules>
41+
</nlog>

0 commit comments

Comments
 (0)