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 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<Assembly>() { packageManifest.PluginAssembly });
PluginsLog.LogInstalled(packageManifest);
Status.SetFinishedUrl(string.Format("/Config/Plugins/{0}", System.Web.HttpUtility.UrlEncode(packageManifest.Id)));
Status.UpdateStatus(100, "Plugin Installation Completed");
+39 -1
View File
@@ -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<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();
@@ -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)
{
return _RunningTasks.Where(t => t.SessionId == TaskSessionId).FirstOrDefault();