mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Core - Refactored (unused) queued actions system
This commit is contained in:
parent
0ed367530f
commit
a6f52ce4a0
@ -135,11 +135,16 @@ namespace Artemis.Core.Services
|
|||||||
DeviceProvider GetDeviceProviderByDevice(IRGBDevice device);
|
DeviceProvider GetDeviceProviderByDevice(IRGBDevice device);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Queues an action for the provided plugin for the next time Artemis starts, before plugins are loaded
|
/// Queues the provided plugin to be deleted the next time Artemis starts, before plugins are loaded
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="plugin">The plugin to queue the action for</param>
|
/// <param name="plugin">The plugin to delete</param>
|
||||||
/// <param name="pluginAction">The action to take</param>
|
void QueuePluginDeletion(Plugin plugin);
|
||||||
void QueuePluginAction(Plugin plugin, PluginManagementAction pluginAction);
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the provided plugin for the deletion queue it was added to via <see cref="QueuePluginDeletion" />
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">The plugin to dequeue</param>
|
||||||
|
void DequeuePluginDeletion(Plugin plugin);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs when built-in plugins are being loaded
|
/// Occurs when built-in plugins are being loaded
|
||||||
|
|||||||
@ -7,6 +7,7 @@ using System.Linq;
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Artemis.Core.DeviceProviders;
|
using Artemis.Core.DeviceProviders;
|
||||||
using Artemis.Core.Ninject;
|
using Artemis.Core.Ninject;
|
||||||
|
using Artemis.Storage.Entities.General;
|
||||||
using Artemis.Storage.Entities.Plugins;
|
using Artemis.Storage.Entities.Plugins;
|
||||||
using Artemis.Storage.Repositories.Interfaces;
|
using Artemis.Storage.Repositories.Interfaces;
|
||||||
using McMaster.NETCore.Plugins;
|
using McMaster.NETCore.Plugins;
|
||||||
@ -26,17 +27,19 @@ namespace Artemis.Core.Services
|
|||||||
private readonly IKernel _kernel;
|
private readonly IKernel _kernel;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IPluginRepository _pluginRepository;
|
private readonly IPluginRepository _pluginRepository;
|
||||||
|
private readonly IQueuedActionRepository _queuedActionRepository;
|
||||||
private readonly List<Plugin> _plugins;
|
private readonly List<Plugin> _plugins;
|
||||||
private bool _isElevated;
|
private bool _isElevated;
|
||||||
|
|
||||||
public PluginManagementService(IKernel kernel, ILogger logger, IPluginRepository pluginRepository)
|
public PluginManagementService(IKernel kernel, ILogger logger, IPluginRepository pluginRepository, IQueuedActionRepository queuedActionRepository)
|
||||||
{
|
{
|
||||||
_kernel = kernel;
|
_kernel = kernel;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_pluginRepository = pluginRepository;
|
_pluginRepository = pluginRepository;
|
||||||
|
_queuedActionRepository = queuedActionRepository;
|
||||||
_plugins = new List<Plugin>();
|
_plugins = new List<Plugin>();
|
||||||
|
|
||||||
ProcessQueuedActions();
|
ProcessPluginDeletionQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CopyBuiltInPlugin(ZipArchive zipArchive, string targetDirectory)
|
private void CopyBuiltInPlugin(ZipArchive zipArchive, string targetDirectory)
|
||||||
@ -664,27 +667,51 @@ namespace Artemis.Core.Services
|
|||||||
|
|
||||||
#region Queued actions
|
#region Queued actions
|
||||||
|
|
||||||
public void QueuePluginAction(Plugin plugin, PluginManagementAction pluginAction)
|
public void QueuePluginDeletion(Plugin plugin)
|
||||||
{
|
{
|
||||||
List<PluginQueuedActionEntity> existing = _pluginRepository.GetQueuedActions(plugin.Guid);
|
_queuedActionRepository.Add(new QueuedActionEntity
|
||||||
if (existing.Any(e => pluginAction == PluginManagementAction.Delete && e is PluginQueuedDeleteEntity))
|
{
|
||||||
return;
|
Type = "DeletePlugin",
|
||||||
|
CreatedAt = DateTimeOffset.Now,
|
||||||
if (pluginAction == PluginManagementAction.Delete)
|
Parameters = new Dictionary<string, object>()
|
||||||
_pluginRepository.AddQueuedAction(new PluginQueuedDeleteEntity {PluginGuid = plugin.Guid, Directory = plugin.Directory.FullName});
|
{
|
||||||
|
{"pluginGuid", plugin.Guid.ToString()},
|
||||||
|
{"plugin", plugin.ToString()},
|
||||||
|
{"directory", plugin.Directory.FullName}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessQueuedActions()
|
public void DequeuePluginDeletion(Plugin plugin)
|
||||||
{
|
{
|
||||||
foreach (PluginQueuedActionEntity pluginQueuedActionEntity in _pluginRepository.GetQueuedActions())
|
QueuedActionEntity? queuedActionEntity = _queuedActionRepository.GetByType("DeletePlugin").FirstOrDefault(q => q.Parameters["pluginGuid"].Equals(plugin.Guid.ToString()));
|
||||||
{
|
if (queuedActionEntity != null)
|
||||||
if (pluginQueuedActionEntity is PluginQueuedDeleteEntity deleteAction)
|
_queuedActionRepository.Remove(queuedActionEntity);
|
||||||
{
|
}
|
||||||
if (Directory.Exists(deleteAction.Directory))
|
|
||||||
Directory.Delete(deleteAction.Directory, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
_pluginRepository.RemoveQueuedAction(pluginQueuedActionEntity);
|
private void ProcessPluginDeletionQueue()
|
||||||
|
{
|
||||||
|
foreach (QueuedActionEntity queuedActionEntity in _queuedActionRepository.GetByType("DeletePlugin"))
|
||||||
|
{
|
||||||
|
string? directory = queuedActionEntity.Parameters["directory"].ToString();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (Directory.Exists(directory))
|
||||||
|
{
|
||||||
|
_logger.Information("Queued plugin deletion - deleting folder - {plugin}", queuedActionEntity.Parameters["plugin"]);
|
||||||
|
Directory.Delete(directory!, true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.Information("Queued plugin deletion - folder already deleted - {plugin}", queuedActionEntity.Parameters["plugin"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
_queuedActionRepository.Remove(queuedActionEntity);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.Warning(e, "Queued plugin deletion failed - {plugin}", queuedActionEntity.Parameters["plugin"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
src/Artemis.Storage/Entities/General/QueuedActionEntity.cs
Normal file
19
src/Artemis.Storage/Entities/General/QueuedActionEntity.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Artemis.Storage.Entities.General
|
||||||
|
{
|
||||||
|
public class QueuedActionEntity
|
||||||
|
{
|
||||||
|
public QueuedActionEntity()
|
||||||
|
{
|
||||||
|
Parameters = new Dictionary<string, object>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public string Type { get; set; }
|
||||||
|
public DateTimeOffset CreatedAt { get; set; }
|
||||||
|
|
||||||
|
public Dictionary<string, object> Parameters { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -13,21 +13,4 @@ namespace Artemis.Storage.Entities.Plugins
|
|||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Value { get; set; }
|
public string Value { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a queued action for a plugin
|
|
||||||
/// </summary>
|
|
||||||
public abstract class PluginQueuedActionEntity
|
|
||||||
{
|
|
||||||
public Guid Id { get; set; }
|
|
||||||
public Guid PluginGuid { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a queued delete action for a plugin
|
|
||||||
/// </summary>
|
|
||||||
public class PluginQueuedDeleteEntity : PluginQueuedActionEntity
|
|
||||||
{
|
|
||||||
public string Directory { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using Artemis.Storage.Entities.Plugins;
|
using Artemis.Storage.Entities.Plugins;
|
||||||
|
|
||||||
namespace Artemis.Storage.Repositories.Interfaces
|
namespace Artemis.Storage.Repositories.Interfaces
|
||||||
@ -9,16 +8,11 @@ namespace Artemis.Storage.Repositories.Interfaces
|
|||||||
void AddPlugin(PluginEntity pluginEntity);
|
void AddPlugin(PluginEntity pluginEntity);
|
||||||
PluginEntity GetPluginByGuid(Guid pluginGuid);
|
PluginEntity GetPluginByGuid(Guid pluginGuid);
|
||||||
void SavePlugin(PluginEntity pluginEntity);
|
void SavePlugin(PluginEntity pluginEntity);
|
||||||
|
|
||||||
void AddSetting(PluginSettingEntity pluginSettingEntity);
|
void AddSetting(PluginSettingEntity pluginSettingEntity);
|
||||||
PluginSettingEntity GetSettingByGuid(Guid pluginGuid);
|
PluginSettingEntity GetSettingByGuid(Guid pluginGuid);
|
||||||
PluginSettingEntity GetSettingByNameAndGuid(string name, Guid pluginGuid);
|
PluginSettingEntity GetSettingByNameAndGuid(string name, Guid pluginGuid);
|
||||||
void SaveSetting(PluginSettingEntity pluginSettingEntity);
|
void SaveSetting(PluginSettingEntity pluginSettingEntity);
|
||||||
void RemoveSettings(Guid pluginGuid);
|
void RemoveSettings(Guid pluginGuid);
|
||||||
|
|
||||||
void AddQueuedAction(PluginQueuedActionEntity pluginQueuedActionEntity);
|
|
||||||
List<PluginQueuedActionEntity> GetQueuedActions();
|
|
||||||
List<PluginQueuedActionEntity> GetQueuedActions(Guid pluginGuid);
|
|
||||||
void RemoveQueuedAction(PluginQueuedActionEntity pluginQueuedActionEntity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Artemis.Storage.Entities.General;
|
||||||
|
|
||||||
|
namespace Artemis.Storage.Repositories.Interfaces
|
||||||
|
{
|
||||||
|
public interface IQueuedActionRepository : IRepository
|
||||||
|
{
|
||||||
|
void Add(QueuedActionEntity queuedActionEntity);
|
||||||
|
void Remove(QueuedActionEntity queuedActionEntity);
|
||||||
|
List<QueuedActionEntity> GetAll();
|
||||||
|
List<QueuedActionEntity> GetByType(string type);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using Artemis.Storage.Entities.Module;
|
using Artemis.Storage.Entities.Module;
|
||||||
using Artemis.Storage.Repositories.Interfaces;
|
using Artemis.Storage.Repositories.Interfaces;
|
||||||
using LiteDB;
|
using LiteDB;
|
||||||
|
|||||||
@ -15,7 +15,6 @@ namespace Artemis.Storage.Repositories
|
|||||||
_repository = repository;
|
_repository = repository;
|
||||||
|
|
||||||
_repository.Database.GetCollection<PluginSettingEntity>().EnsureIndex(s => new {s.Name, s.PluginGuid}, true);
|
_repository.Database.GetCollection<PluginSettingEntity>().EnsureIndex(s => new {s.Name, s.PluginGuid}, true);
|
||||||
_repository.Database.GetCollection<PluginQueuedActionEntity>().EnsureIndex(s => s.PluginGuid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddPlugin(PluginEntity pluginEntity)
|
public void AddPlugin(PluginEntity pluginEntity)
|
||||||
@ -59,24 +58,5 @@ namespace Artemis.Storage.Repositories
|
|||||||
{
|
{
|
||||||
_repository.DeleteMany<PluginSettingEntity>(s => s.PluginGuid == pluginGuid);
|
_repository.DeleteMany<PluginSettingEntity>(s => s.PluginGuid == pluginGuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PluginQueuedActionEntity> GetQueuedActions()
|
|
||||||
{
|
|
||||||
return _repository.Query<PluginQueuedActionEntity>().ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<PluginQueuedActionEntity> GetQueuedActions(Guid pluginGuid)
|
|
||||||
{
|
|
||||||
return _repository.Query<PluginQueuedActionEntity>().Where(q => q.PluginGuid == pluginGuid).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddQueuedAction(PluginQueuedActionEntity pluginQueuedActionEntity)
|
|
||||||
{
|
|
||||||
_repository.Upsert(pluginQueuedActionEntity);
|
|
||||||
}
|
|
||||||
public void RemoveQueuedAction(PluginQueuedActionEntity pluginQueuedActionEntity)
|
|
||||||
{
|
|
||||||
_repository.Delete<PluginQueuedActionEntity>(pluginQueuedActionEntity.Id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
46
src/Artemis.Storage/Repositories/QueuedActionRepository.cs
Normal file
46
src/Artemis.Storage/Repositories/QueuedActionRepository.cs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Artemis.Storage.Entities.General;
|
||||||
|
using Artemis.Storage.Repositories.Interfaces;
|
||||||
|
using LiteDB;
|
||||||
|
|
||||||
|
namespace Artemis.Storage.Repositories
|
||||||
|
{
|
||||||
|
public class QueuedActionRepository : IQueuedActionRepository
|
||||||
|
{
|
||||||
|
private readonly LiteRepository _repository;
|
||||||
|
|
||||||
|
public QueuedActionRepository(LiteRepository repository)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
_repository.Database.GetCollection<QueuedActionEntity>().EnsureIndex(s => s.Type);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Implementation of IQueuedActionRepository
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Add(QueuedActionEntity queuedActionEntity)
|
||||||
|
{
|
||||||
|
_repository.Insert(queuedActionEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Remove(QueuedActionEntity queuedActionEntity)
|
||||||
|
{
|
||||||
|
_repository.Delete<QueuedActionEntity>(queuedActionEntity.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public List<QueuedActionEntity> GetAll()
|
||||||
|
{
|
||||||
|
return _repository.Query<QueuedActionEntity>().ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public List<QueuedActionEntity> GetByType(string type)
|
||||||
|
{
|
||||||
|
return _repository.Query<QueuedActionEntity>().Where(q => q.Type == type).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user