diff --git a/Disco.Services/Plugins/InstallPluginTask.cs b/Disco.Services/Plugins/InstallPluginTask.cs index 637c98a5..02ef0c60 100644 --- a/Disco.Services/Plugins/InstallPluginTask.cs +++ b/Disco.Services/Plugins/InstallPluginTask.cs @@ -4,10 +4,12 @@ using Disco.Services.Interop.DiscoServices; using Disco.Services.Tasks; using Quartz; using System; +using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Net.Http; +using System.Reflection; namespace Disco.Services.Plugins { @@ -147,6 +149,9 @@ namespace Disco.Services.Plugins // Add Plugin Manifest to Host Environment Plugins.AddPlugin(packageManifest); + // Initialize Scheduled Tasks + ScheduledTasks.InitializeScheduledTasks(database, new List() { packageManifest.PluginAssembly }); + PluginsLog.LogInstalled(packageManifest); Status.SetFinishedUrl(string.Format("/Config/Plugins/{0}", System.Web.HttpUtility.UrlEncode(packageManifest.Id))); Status.UpdateStatus(100, "Plugin Installation Completed"); diff --git a/Disco.Services/Tasks/ScheduledTasks.cs b/Disco.Services/Tasks/ScheduledTasks.cs index c13b702b..b8277e3b 100644 --- a/Disco.Services/Tasks/ScheduledTasks.cs +++ b/Disco.Services/Tasks/ScheduledTasks.cs @@ -4,6 +4,7 @@ using System.Linq; using Quartz; using Quartz.Impl; using Disco.Data.Repository; +using System.Reflection; namespace Disco.Services.Tasks { @@ -15,7 +16,7 @@ namespace Disco.Services.Tasks private static object _RunningTasksLock = new object(); private static List _RunningTasks = new List(); - public static void InitalizeScheduledTasks(DiscoDataContext database, ISchedulerFactory SchedulerFactory, bool InitiallySchedule) + public static void InitializeScheduledTasks(DiscoDataContext database, ISchedulerFactory SchedulerFactory, bool InitiallySchedule) { ScheduledTasksLog.LogInitializingScheduledTasks(); @@ -61,6 +62,43 @@ namespace Disco.Services.Tasks } + public static void InitializeScheduledTasks(DiscoDataContext database, List assemblies) + { + var scheduler = _TaskScheduler; + + if (scheduler == null) + throw new InvalidOperationException("Scheduled task assembly initialization can only be called after master initialization"); + + try + { + var servicesAssemblyName = typeof(ScheduledTask).Assembly.GetName().Name; + + var scheduledTaskTypes = (from a in assemblies + where !a.GlobalAssemblyCache && + !a.IsDynamic && + (a.GetName().Name == servicesAssemblyName || a.GetReferencedAssemblies().Any(ra => ra.Name == servicesAssemblyName)) + from type in a.GetTypes() + where typeof(ScheduledTask).IsAssignableFrom(type) && !type.IsAbstract + select type); + foreach (Type scheduledTaskType in scheduledTaskTypes) + { + ScheduledTask instance = (ScheduledTask)Activator.CreateInstance(scheduledTaskType); + try + { + instance.InitalizeScheduledTask(database); + } + catch (Exception ex) + { + ScheduledTasksLog.LogInitializeException(ex, scheduledTaskType); + } + } + } + catch (Exception ex) + { + ScheduledTasksLog.LogInitializeException(ex); + } + } + public static ScheduledTaskStatus GetTaskStatus(string TaskSessionId) { return _RunningTasks.Where(t => t.SessionId == TaskSessionId).FirstOrDefault(); diff --git a/Disco.Web/App_Start/AppConfig.cs b/Disco.Web/App_Start/AppConfig.cs index efc4c506..52460e4a 100644 --- a/Disco.Web/App_Start/AppConfig.cs +++ b/Disco.Web/App_Start/AppConfig.cs @@ -69,7 +69,7 @@ namespace Disco.Web Disco.Services.Plugins.Plugins.InitalizePlugins(Database); // Initialize Scheduled Tasks - Disco.Services.Tasks.ScheduledTasks.InitalizeScheduledTasks(Database, DiscoApplication.SchedulerFactory, true); + Disco.Services.Tasks.ScheduledTasks.InitializeScheduledTasks(Database, DiscoApplication.SchedulerFactory, true); // Schedule Immediate Check for Update (if never updated, or last updated over 2 days ago) if (Database.DiscoConfiguration.UpdateLastCheckResponse == null || @@ -91,7 +91,7 @@ namespace Disco.Web InitalizeCoreEnvironment(Database); // Initialize Scheduled Tasks - Disco.Services.Tasks.ScheduledTasks.InitalizeScheduledTasks(Database, DiscoApplication.SchedulerFactory, false); + Disco.Services.Tasks.ScheduledTasks.InitializeScheduledTasks(Database, DiscoApplication.SchedulerFactory, false); // Import MAC Address Migration if (PreviousVersion != null && PreviousVersion < new Version(1, 2, 910, 0))