initialize plugin scheduled tasks when installed

This commit is contained in:
Gary Sharp
2023-04-26 18:16:46 +10:00
parent 937508c440
commit b55b77de9f
3 changed files with 46 additions and 3 deletions
@@ -4,10 +4,12 @@ using Disco.Services.Interop.DiscoServices;
using Disco.Services.Tasks; using Disco.Services.Tasks;
using Quartz; using Quartz;
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Reflection;
namespace Disco.Services.Plugins namespace Disco.Services.Plugins
{ {
@@ -147,6 +149,9 @@ namespace Disco.Services.Plugins
// Add Plugin Manifest to Host Environment // Add Plugin Manifest to Host Environment
Plugins.AddPlugin(packageManifest); Plugins.AddPlugin(packageManifest);
// Initialize Scheduled Tasks
ScheduledTasks.InitializeScheduledTasks(database, new List<Assembly>() { packageManifest.PluginAssembly });
PluginsLog.LogInstalled(packageManifest); PluginsLog.LogInstalled(packageManifest);
Status.SetFinishedUrl(string.Format("/Config/Plugins/{0}", System.Web.HttpUtility.UrlEncode(packageManifest.Id))); Status.SetFinishedUrl(string.Format("/Config/Plugins/{0}", System.Web.HttpUtility.UrlEncode(packageManifest.Id)));
Status.UpdateStatus(100, "Plugin Installation Completed"); Status.UpdateStatus(100, "Plugin Installation Completed");
+39 -1
View File
@@ -4,6 +4,7 @@ using System.Linq;
using Quartz; using Quartz;
using Quartz.Impl; using Quartz.Impl;
using Disco.Data.Repository; using Disco.Data.Repository;
using System.Reflection;
namespace Disco.Services.Tasks namespace Disco.Services.Tasks
{ {
@@ -15,7 +16,7 @@ namespace Disco.Services.Tasks
private static object _RunningTasksLock = new object(); private static object _RunningTasksLock = new object();
private static List<ScheduledTaskStatus> _RunningTasks = new List<ScheduledTaskStatus>(); private static List<ScheduledTaskStatus> _RunningTasks = new List<ScheduledTaskStatus>();
public static void InitalizeScheduledTasks(DiscoDataContext database, ISchedulerFactory SchedulerFactory, bool InitiallySchedule) public static void InitializeScheduledTasks(DiscoDataContext database, ISchedulerFactory SchedulerFactory, bool InitiallySchedule)
{ {
ScheduledTasksLog.LogInitializingScheduledTasks(); ScheduledTasksLog.LogInitializingScheduledTasks();
@@ -61,6 +62,43 @@ namespace Disco.Services.Tasks
} }
public static void InitializeScheduledTasks(DiscoDataContext database, List<Assembly> 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) public static ScheduledTaskStatus GetTaskStatus(string TaskSessionId)
{ {
return _RunningTasks.Where(t => t.SessionId == TaskSessionId).FirstOrDefault(); return _RunningTasks.Where(t => t.SessionId == TaskSessionId).FirstOrDefault();
+2 -2
View File
@@ -69,7 +69,7 @@ namespace Disco.Web
Disco.Services.Plugins.Plugins.InitalizePlugins(Database); Disco.Services.Plugins.Plugins.InitalizePlugins(Database);
// Initialize Scheduled Tasks // 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) // Schedule Immediate Check for Update (if never updated, or last updated over 2 days ago)
if (Database.DiscoConfiguration.UpdateLastCheckResponse == null || if (Database.DiscoConfiguration.UpdateLastCheckResponse == null ||
@@ -91,7 +91,7 @@ namespace Disco.Web
InitalizeCoreEnvironment(Database); InitalizeCoreEnvironment(Database);
// Initialize Scheduled Tasks // 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 // Import MAC Address Migration
if (PreviousVersion != null && PreviousVersion < new Version(1, 2, 910, 0)) if (PreviousVersion != null && PreviousVersion < new Version(1, 2, 910, 0))