mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Allowing tasks to take parameters
This commit is contained in:
parent
f01b25b827
commit
f00d1ec4e9
@ -2,7 +2,7 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
public interface ITaskManager
|
||||
{
|
||||
bool StartTask(string taskSlug);
|
||||
bool StartTask(string taskSlug, string arguments = null);
|
||||
|
||||
void ReloadTask();
|
||||
}
|
||||
|
@ -12,6 +12,6 @@ namespace Kyoo.Models
|
||||
public string HelpMessage { get; }
|
||||
public bool RunOnStartup { get; }
|
||||
public int Priority { get; }
|
||||
public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken);
|
||||
public Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string arguments = null);
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ namespace Kyoo.Controllers
|
||||
|
||||
private List<ITask> _tasks = new List<ITask>();
|
||||
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)
|
||||
{
|
||||
@ -29,9 +29,12 @@ namespace Kyoo.Controllers
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
if (_queuedTasks.Any())
|
||||
await _queuedTasks.Dequeue().Run(_serviceProvider, _taskToken.Token);
|
||||
{
|
||||
(ITask task, string arguments) = _queuedTasks.Dequeue();
|
||||
await task.Run(_serviceProvider, _taskToken.Token, arguments);
|
||||
}
|
||||
else
|
||||
await Task.Delay(10);
|
||||
await Task.Delay(1000, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +42,7 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
ReloadTask();
|
||||
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);
|
||||
}
|
||||
|
||||
@ -49,12 +52,12 @@ namespace Kyoo.Controllers
|
||||
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);
|
||||
if (task == null)
|
||||
return false;
|
||||
_queuedTasks.Enqueue(task);
|
||||
_queuedTasks.Enqueue((task, arguments));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ namespace Kyoo.Controllers
|
||||
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();
|
||||
_libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
|
||||
@ -40,6 +40,9 @@ namespace Kyoo.Controllers
|
||||
IEnumerable<Episode> episodes = _libraryManager.GetAllEpisodes();
|
||||
IEnumerable<Library> libraries = _libraryManager.GetLibraries();
|
||||
|
||||
if (argument != null)
|
||||
libraries = libraries.Where(x => x.Slug == argument);
|
||||
|
||||
foreach (Episode episode in episodes)
|
||||
{
|
||||
if (!File.Exists(episode.Path))
|
||||
|
@ -20,7 +20,7 @@ namespace Kyoo.Tasks
|
||||
public bool RunOnStartup => true;
|
||||
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();
|
||||
DatabaseContext databaseContext = serviceScope.ServiceProvider.GetService<DatabaseContext>();
|
||||
|
@ -15,7 +15,7 @@ namespace Kyoo.Tasks
|
||||
public string HelpMessage => null;
|
||||
public bool RunOnStartup => true;
|
||||
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();
|
||||
IPluginManager pluginManager = serviceScope.ServiceProvider.GetService<IPluginManager>();
|
||||
|
@ -16,11 +16,11 @@ namespace Kyoo.Api
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("{taskSlug}")]
|
||||
[Authorize(Policy="Admin")]
|
||||
public IActionResult RunTask(string taskSlug)
|
||||
[HttpGet("{taskSlug}/{args?}")]
|
||||
// [Authorize(Policy="Admin")]
|
||||
public IActionResult RunTask(string taskSlug, string args = null)
|
||||
{
|
||||
if (_taskManager.StartTask(taskSlug))
|
||||
if (_taskManager.StartTask(taskSlug, args))
|
||||
return Ok();
|
||||
return NotFound();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user