Adding helper methods inside of tasks to allow users to use them freely

This commit is contained in:
Zoe Roux 2020-04-08 03:04:05 +02:00
parent f00d1ec4e9
commit 4a41dd6f88
7 changed files with 57 additions and 3 deletions

View File

@ -1,9 +1,13 @@
using System.Collections.Generic;
using Kyoo.Models;
namespace Kyoo.Controllers
{
public interface ITaskManager
{
bool StartTask(string taskSlug, string arguments = null);
ITask GetRunningTask();
void ReloadTask();
IEnumerable<ITask> GetAllTasks();
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@ -13,5 +14,7 @@ namespace Kyoo.Models
public bool RunOnStartup { get; }
public int Priority { get; }
public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string arguments = null);
public IEnumerable<string> GetPossibleParameters();
public int? Progress();
}
}

View File

@ -16,6 +16,7 @@ namespace Kyoo.Controllers
private List<ITask> _tasks = new List<ITask>();
private CancellationTokenSource _taskToken = new CancellationTokenSource();
private ITask _runningTask;
private Queue<(ITask, string)> _queuedTasks = new Queue<(ITask, string)>();
public TaskManager(IServiceProvider serviceProvider, IPluginManager pluginManager)
@ -31,10 +32,14 @@ namespace Kyoo.Controllers
if (_queuedTasks.Any())
{
(ITask task, string arguments) = _queuedTasks.Dequeue();
_runningTask = task;
await task.Run(_serviceProvider, _taskToken.Token, arguments);
}
else
await Task.Delay(1000, cancellationToken);
{
await Task.Delay(1000_000, cancellationToken);
}
}
}
@ -60,6 +65,11 @@ namespace Kyoo.Controllers
_queuedTasks.Enqueue((task, arguments));
return true;
}
public ITask GetRunningTask()
{
return _runningTask;
}
public void ReloadTask()
{
@ -70,5 +80,10 @@ namespace Kyoo.Controllers
foreach (IPlugin plugin in _pluginManager.GetAllPlugins())
_tasks.AddRange(plugin.Tasks);
}
public IEnumerable<ITask> GetAllTasks()
{
return _tasks;
}
}
}

View File

@ -26,6 +26,16 @@ namespace Kyoo.Controllers
private ITranscoder _transcoder;
private IConfiguration _config;
public IEnumerable<string> GetPossibleParameters()
{
return _libraryManager.GetLibraries().Select(x => x.Slug);
}
public int? Progress()
{
// TODO implement this later.
return null;
}
public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string argument = null)
{

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -49,5 +50,15 @@ namespace Kyoo.Tasks
}
return Task.CompletedTask;
}
public IEnumerable<string> GetPossibleParameters()
{
return null;
}
public int? Progress()
{
return null;
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Kyoo.Controllers;
@ -22,5 +23,15 @@ namespace Kyoo.Tasks
pluginManager.ReloadPlugins();
return Task.CompletedTask;
}
public IEnumerable<string> GetPossibleParameters()
{
return null;
}
public int? Progress()
{
return null;
}
}
}

View File

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