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);
|
||||
|
||||
/// <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>
|
||||
/// <param name="plugin">The plugin to queue the action for</param>
|
||||
/// <param name="pluginAction">The action to take</param>
|
||||
void QueuePluginAction(Plugin plugin, PluginManagementAction pluginAction);
|
||||
/// <param name="plugin">The plugin to delete</param>
|
||||
void QueuePluginDeletion(Plugin plugin);
|
||||
|
||||
/// <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>
|
||||
/// Occurs when built-in plugins are being loaded
|
||||
|
||||
@ -7,6 +7,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.DeviceProviders;
|
||||
using Artemis.Core.Ninject;
|
||||
using Artemis.Storage.Entities.General;
|
||||
using Artemis.Storage.Entities.Plugins;
|
||||
using Artemis.Storage.Repositories.Interfaces;
|
||||
using McMaster.NETCore.Plugins;
|
||||
@ -26,17 +27,19 @@ namespace Artemis.Core.Services
|
||||
private readonly IKernel _kernel;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IPluginRepository _pluginRepository;
|
||||
private readonly IQueuedActionRepository _queuedActionRepository;
|
||||
private readonly List<Plugin> _plugins;
|
||||
private bool _isElevated;
|
||||
|
||||
public PluginManagementService(IKernel kernel, ILogger logger, IPluginRepository pluginRepository)
|
||||
public PluginManagementService(IKernel kernel, ILogger logger, IPluginRepository pluginRepository, IQueuedActionRepository queuedActionRepository)
|
||||
{
|
||||
_kernel = kernel;
|
||||
_logger = logger;
|
||||
_pluginRepository = pluginRepository;
|
||||
_queuedActionRepository = queuedActionRepository;
|
||||
_plugins = new List<Plugin>();
|
||||
|
||||
ProcessQueuedActions();
|
||||
ProcessPluginDeletionQueue();
|
||||
}
|
||||
|
||||
private void CopyBuiltInPlugin(ZipArchive zipArchive, string targetDirectory)
|
||||
@ -664,27 +667,51 @@ namespace Artemis.Core.Services
|
||||
|
||||
#region Queued actions
|
||||
|
||||
public void QueuePluginAction(Plugin plugin, PluginManagementAction pluginAction)
|
||||
public void QueuePluginDeletion(Plugin plugin)
|
||||
{
|
||||
List<PluginQueuedActionEntity> existing = _pluginRepository.GetQueuedActions(plugin.Guid);
|
||||
if (existing.Any(e => pluginAction == PluginManagementAction.Delete && e is PluginQueuedDeleteEntity))
|
||||
return;
|
||||
|
||||
if (pluginAction == PluginManagementAction.Delete)
|
||||
_pluginRepository.AddQueuedAction(new PluginQueuedDeleteEntity {PluginGuid = plugin.Guid, Directory = plugin.Directory.FullName});
|
||||
_queuedActionRepository.Add(new QueuedActionEntity
|
||||
{
|
||||
Type = "DeletePlugin",
|
||||
CreatedAt = DateTimeOffset.Now,
|
||||
Parameters = new Dictionary<string, object>()
|
||||
{
|
||||
{"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())
|
||||
{
|
||||
if (pluginQueuedActionEntity is PluginQueuedDeleteEntity deleteAction)
|
||||
{
|
||||
if (Directory.Exists(deleteAction.Directory))
|
||||
Directory.Delete(deleteAction.Directory, true);
|
||||
QueuedActionEntity? queuedActionEntity = _queuedActionRepository.GetByType("DeletePlugin").FirstOrDefault(q => q.Parameters["pluginGuid"].Equals(plugin.Guid.ToString()));
|
||||
if (queuedActionEntity != null)
|
||||
_queuedActionRepository.Remove(queuedActionEntity);
|
||||
}
|
||||
|
||||
_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 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.Collections.Generic;
|
||||
using Artemis.Storage.Entities.Plugins;
|
||||
|
||||
namespace Artemis.Storage.Repositories.Interfaces
|
||||
@ -15,10 +14,5 @@ namespace Artemis.Storage.Repositories.Interfaces
|
||||
PluginSettingEntity GetSettingByNameAndGuid(string name, Guid pluginGuid);
|
||||
void SaveSetting(PluginSettingEntity pluginSettingEntity);
|
||||
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.Repositories.Interfaces;
|
||||
using LiteDB;
|
||||
|
||||
@ -15,7 +15,6 @@ namespace Artemis.Storage.Repositories
|
||||
_repository = repository;
|
||||
|
||||
_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)
|
||||
@ -59,24 +58,5 @@ namespace Artemis.Storage.Repositories
|
||||
{
|
||||
_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