Allowing tasks to take parameters

This commit is contained in:
Zoe Roux 2020-04-08 02:27:22 +02:00
parent f01b25b827
commit f00d1ec4e9
7 changed files with 21 additions and 15 deletions

View File

@ -2,7 +2,7 @@ namespace Kyoo.Controllers
{ {
public interface ITaskManager public interface ITaskManager
{ {
bool StartTask(string taskSlug); bool StartTask(string taskSlug, string arguments = null);
void ReloadTask(); void ReloadTask();
} }

View File

@ -12,6 +12,6 @@ namespace Kyoo.Models
public string HelpMessage { get; } public string HelpMessage { get; }
public bool RunOnStartup { get; } public bool RunOnStartup { get; }
public int Priority { get; } public int Priority { get; }
public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken); public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string arguments = null);
} }
} }

View File

@ -16,7 +16,7 @@ namespace Kyoo.Controllers
private List<ITask> _tasks = new List<ITask>(); private List<ITask> _tasks = new List<ITask>();
private CancellationTokenSource _taskToken = new CancellationTokenSource(); private CancellationTokenSource _taskToken = new CancellationTokenSource();
private Queue<ITask> _queuedTasks = new Queue<ITask>(); private Queue<(ITask, string)> _queuedTasks = new Queue<(ITask, string)>();
public TaskManager(IServiceProvider serviceProvider, IPluginManager pluginManager) public TaskManager(IServiceProvider serviceProvider, IPluginManager pluginManager)
{ {
@ -29,9 +29,12 @@ namespace Kyoo.Controllers
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
if (_queuedTasks.Any()) if (_queuedTasks.Any())
await _queuedTasks.Dequeue().Run(_serviceProvider, _taskToken.Token); {
(ITask task, string arguments) = _queuedTasks.Dequeue();
await task.Run(_serviceProvider, _taskToken.Token, arguments);
}
else else
await Task.Delay(10); await Task.Delay(1000, cancellationToken);
} }
} }
@ -39,7 +42,7 @@ namespace Kyoo.Controllers
{ {
ReloadTask(); ReloadTask();
foreach (ITask task in _tasks.Where(x => x.RunOnStartup && x.Priority != Int32.MaxValue).OrderByDescending(x => x.Priority)) foreach (ITask task in _tasks.Where(x => x.RunOnStartup && x.Priority != Int32.MaxValue).OrderByDescending(x => x.Priority))
_queuedTasks.Enqueue(task); _queuedTasks.Enqueue((task, null));
return base.StartAsync(cancellationToken); return base.StartAsync(cancellationToken);
} }
@ -49,12 +52,12 @@ namespace Kyoo.Controllers
return base.StopAsync(cancellationToken); return base.StopAsync(cancellationToken);
} }
public bool StartTask(string taskSlug) public bool StartTask(string taskSlug, string arguments = null)
{ {
ITask task = _tasks.FirstOrDefault(x => x.Slug == taskSlug); ITask task = _tasks.FirstOrDefault(x => x.Slug == taskSlug);
if (task == null) if (task == null)
return false; return false;
_queuedTasks.Enqueue(task); _queuedTasks.Enqueue((task, arguments));
return true; return true;
} }

View File

@ -27,7 +27,7 @@ namespace Kyoo.Controllers
private IConfiguration _config; private IConfiguration _config;
public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken) public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string argument = null)
{ {
using IServiceScope serviceScope = serviceProvider.CreateScope(); using IServiceScope serviceScope = serviceProvider.CreateScope();
_libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>(); _libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
@ -40,6 +40,9 @@ namespace Kyoo.Controllers
IEnumerable<Episode> episodes = _libraryManager.GetAllEpisodes(); IEnumerable<Episode> episodes = _libraryManager.GetAllEpisodes();
IEnumerable<Library> libraries = _libraryManager.GetLibraries(); IEnumerable<Library> libraries = _libraryManager.GetLibraries();
if (argument != null)
libraries = libraries.Where(x => x.Slug == argument);
foreach (Episode episode in episodes) foreach (Episode episode in episodes)
{ {
if (!File.Exists(episode.Path)) if (!File.Exists(episode.Path))

View File

@ -20,7 +20,7 @@ namespace Kyoo.Tasks
public bool RunOnStartup => true; public bool RunOnStartup => true;
public int Priority => Int32.MaxValue; public int Priority => Int32.MaxValue;
public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken) public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string arguments = null)
{ {
using IServiceScope serviceScope = serviceProvider.CreateScope(); using IServiceScope serviceScope = serviceProvider.CreateScope();
DatabaseContext databaseContext = serviceScope.ServiceProvider.GetService<DatabaseContext>(); DatabaseContext databaseContext = serviceScope.ServiceProvider.GetService<DatabaseContext>();

View File

@ -15,7 +15,7 @@ namespace Kyoo.Tasks
public string HelpMessage => null; public string HelpMessage => null;
public bool RunOnStartup => true; public bool RunOnStartup => true;
public int Priority => Int32.MaxValue; public int Priority => Int32.MaxValue;
public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken) public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string arguments = null)
{ {
using IServiceScope serviceScope = serviceProvider.CreateScope(); using IServiceScope serviceScope = serviceProvider.CreateScope();
IPluginManager pluginManager = serviceScope.ServiceProvider.GetService<IPluginManager>(); IPluginManager pluginManager = serviceScope.ServiceProvider.GetService<IPluginManager>();

View File

@ -16,11 +16,11 @@ namespace Kyoo.Api
} }
[HttpGet("{taskSlug}")] [HttpGet("{taskSlug}/{args?}")]
[Authorize(Policy="Admin")] // [Authorize(Policy="Admin")]
public IActionResult RunTask(string taskSlug) public IActionResult RunTask(string taskSlug, string args = null)
{ {
if (_taskManager.StartTask(taskSlug)) if (_taskManager.StartTask(taskSlug, args))
return Ok(); return Ok();
return NotFound(); return NotFound();
} }