File tree Expand file tree Collapse file tree 13 files changed +3221
-34
lines changed Expand file tree Collapse file tree 13 files changed +3221
-34
lines changed Original file line number Diff line number Diff line change 1+ using Quartz ;
2+
3+ namespace InEngine . Core
4+ {
5+ public interface IJobs : IPluginType
6+ {
7+ void Schedule ( IScheduler scheduler ) ;
8+ }
9+ }
Original file line number Diff line number Diff line change 11namespace InEngine . Core
22{
3- public interface IOptions
3+ public interface IOptions : IPluginType
44 {
55 string GetUsage ( string verb ) ;
66 }
Original file line number Diff line number Diff line change 1+ namespace InEngine . Core
2+ {
3+ public interface IPluginType
4+ { }
5+ }
Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff line change 11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using System . Reflection ;
6+ using NLog ;
57
68namespace 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}
Original file line number Diff line number Diff line change 44
55namespace 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
Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff line change 11using System ;
2- using System . Collections . Generic ;
3- using System . IO ;
42using System . Linq ;
53using System . Reflection ;
64using 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
Original file line number Diff line number Diff line change 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" />
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" >
Original file line number Diff line number Diff line change 1- using InEngine . Commands . Sample ;
2- using QueueJobs = InEngine . Core . Queue . Jobs ;
1+ using System ;
2+ using InEngine . Core ;
33using Quartz ;
4+ using NLog ;
5+
46
57namespace 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}
You can’t perform that action at this time.
0 commit comments