/// <summary> /// Instantiates Scheduler, including any configuration. /// </summary> /// <param name="action">A lambda that configures that sets the Scheduler configuration.</param> public static void Initialize(Action <IConfiguration> action = null) { if (null != _instance) { throw new Exception("Scheduler cannot be initialized after the Scheduler Instance has been created."); } var configuration = new Configuration(); if (null != action) { action(configuration); } Configuration = configuration; // Ensure container resolves persistance store based on configuration settings ObjectFactory.Configure(x => x.RegisterInterceptor(new PersistanceStoreInterceptor(Configuration))); // Initialise JobTypes modules var jobTypeStartups = ObjectFactory.GetAllInstances <IJobTypeStartup>(); foreach (var jobTypeStartup in jobTypeStartups) { jobTypeStartup.Initialise(Configuration); } if (configuration.AutoStart) { IScheduler sched = Instance(); sched.Start(); } }
/// <summary> /// Gets instance of Quartz Scheduler. /// Hosts WebApi endpoints and register AuditListeners. /// </summary> /// <returns></returns> public static IScheduler Instance() { if (null == _instance) { lock (SyncRoot) { if (null == _instance) { if (null == Configuration) { Configuration = new Configuration(); } ISchedulerFactory schedFact = new StdSchedulerFactory(GetProperties()); _instance = schedFact.GetScheduler(); if (Configuration.EnableAuditHistory) { _instance.ListenerManager.AddJobListener(new AuditJobListener(), GroupMatcher <JobKey> .AnyGroup()); _instance.ListenerManager.AddTriggerListener(new AuditTriggerListener(), GroupMatcher <TriggerKey> .AnyGroup()); } if (Configuration.EnableWebApiSelfHost) { IDisposable webApiHost = WebApp.Start <Startup>(url: Configuration.WebApiBaseAddress); } } } } return(_instance); }
/// <summary> /// Instantiates Scheduler, including any configuration. /// </summary> /// <param name="action">A lambda that configures that sets the Scheduler configuration.</param> /// <exception cref="ArgumentOutOfRangeException"></exception> public static void Initialize(Action <IConfiguration> action = null) { if (null != _instance) { throw new InvalidOperationException("Scheduler cannot be initialized after the Scheduler Instance has been created."); } var configuration = new Configuration(); if (null != action) { action(configuration); } Configuration = configuration; // Ensure container resolves persistence store based on configuration settings // Create Temp store, _persistenceStore = new InMemoryStore(); // Determine store type from configuration switch (Configuration.PersistenceStoreType) { case PersistenceStoreType.Postgre: _persistenceStore = new PostgreStore(Configuration.ConnectionString); break; case PersistenceStoreType.SqlServer: _persistenceStore = new SqlServerStore(Configuration.ConnectionString); break; case PersistenceStoreType.InMemory: _persistenceStore = new InMemoryStore(); break; } // Inject the persistence store after initialization SchedulerContainer.Container.Inject(_persistenceStore); // Initialise JobTypes modules var jobTypeStartups = SchedulerContainer.Container.GetAllInstances <IJobTypeStartup>(); foreach (var jobTypeStartup in jobTypeStartups) { jobTypeStartup.Initialise(Configuration); } if (configuration.AutoStart) { IScheduler sched = Instance(); sched.Start(); SchedulerContainer.Container.Inject(sched); } }
/// <summary> /// Gets instance of Quartz Scheduler. /// Hosts WebApi endpoints and register AuditListeners. /// </summary> /// <returns></returns> public static IScheduler Instance() { if (null == _instance) { lock (SyncRoot) { if (null == _instance) { if (null == Configuration) { Configuration = new Configuration(); } ISchedulerFactory schedFact = new StdSchedulerFactory(GetProperties()); _instance = schedFact.GetScheduler(); if (Configuration.EnableAuditHistory) { _instance.ListenerManager.AddJobListener(new AuditJobListener(), GroupMatcher <JobKey> .AnyGroup()); _instance.ListenerManager.AddTriggerListener(new AuditTriggerListener(), GroupMatcher <TriggerKey> .AnyGroup()); } // Custom listeners AddCustomTriggerListeners(); AddCustomJobListeners(); AddCustomSchedulerListeners(); // Custom Authorization AddCustomAuthorization(); if (Configuration.EnableWebApiSelfHost) { // If custom WebApp Settings is defined in a custom assembly - load the assembly, get the relevant type and method, // make generic method and invoke it. if (!string.IsNullOrEmpty(Configuration.CustomWebAppSettingsAssemblyName)) { var asm = GetAssembly(Configuration.CustomWebAppSettingsAssemblyName); Type startupType = null; var allTypes = asm.GetTypes(); foreach (var type in allTypes) { var allMethods = type.GetMethods(); foreach (MethodInfo methodInfo in allMethods) { ParameterInfo pi = methodInfo.GetParameters().FirstOrDefault(q => q.ParameterType == typeof(IAppBuilder)); if (null != pi) { startupType = type; break; } } if (startupType != null) { break; } } MethodInfo miWebAppStart = typeof(Scheduler).GetMethod("WebAppStart", BindingFlags.NonPublic | BindingFlags.Static); MethodInfo genericMiWebAppStart = miWebAppStart.MakeGenericMethod(startupType); genericMiWebAppStart.Invoke(null, new object[] { Configuration.WebApiBaseAddress }); } else { // No custom WebApp Settings defined, use the built-in default. IDisposable webApiHost = WebApp.Start <Startup>(url: Configuration.WebApiBaseAddress); } } } } } return(_instance); }