mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Implementing scheduled tasks
This commit is contained in:
parent
4a41dd6f88
commit
221b5afb8b
@ -5,6 +5,7 @@ using System.Threading;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Models;
|
using Kyoo.Models;
|
||||||
using Kyoo.Tasks;
|
using Kyoo.Tasks;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Kyoo.Controllers
|
namespace Kyoo.Controllers
|
||||||
@ -13,16 +14,18 @@ namespace Kyoo.Controllers
|
|||||||
{
|
{
|
||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
private readonly IPluginManager _pluginManager;
|
private readonly IPluginManager _pluginManager;
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
private List<ITask> _tasks = new List<ITask>();
|
private List<(ITask task, DateTime scheduledDate)> _tasks = new List<(ITask, DateTime)>();
|
||||||
private CancellationTokenSource _taskToken = new CancellationTokenSource();
|
private CancellationTokenSource _taskToken = new CancellationTokenSource();
|
||||||
private ITask _runningTask;
|
private ITask _runningTask;
|
||||||
private Queue<(ITask, string)> _queuedTasks = new Queue<(ITask, string)>();
|
private Queue<(ITask, string)> _queuedTasks = new Queue<(ITask, string)>();
|
||||||
|
|
||||||
public TaskManager(IServiceProvider serviceProvider, IPluginManager pluginManager)
|
public TaskManager(IServiceProvider serviceProvider, IPluginManager pluginManager, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_serviceProvider = serviceProvider;
|
_serviceProvider = serviceProvider;
|
||||||
_pluginManager = pluginManager;
|
_pluginManager = pluginManager;
|
||||||
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||||
@ -37,16 +40,28 @@ namespace Kyoo.Controllers
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
await Task.Delay(10, cancellationToken);
|
||||||
|
QueueScheduledTasks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await Task.Delay(1000_000, cancellationToken);
|
private void QueueScheduledTasks()
|
||||||
}
|
{
|
||||||
}
|
List<string> tasksToQueue = _tasks.Where(x => x.scheduledDate <= DateTime.Now)
|
||||||
|
.Select(x => x.task.Slug).ToList();
|
||||||
|
foreach (string task in tasksToQueue)
|
||||||
|
StartTask(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task StartAsync(CancellationToken cancellationToken)
|
public override Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
ReloadTask();
|
ReloadTask();
|
||||||
foreach (ITask task in _tasks.Where(x => x.RunOnStartup && x.Priority != Int32.MaxValue).OrderByDescending(x => x.Priority))
|
|
||||||
|
IEnumerable<ITask> startupTasks = _tasks.Select(x => x.task)
|
||||||
|
.Where(x => x.RunOnStartup && x.Priority != Int32.MaxValue)
|
||||||
|
.OrderByDescending(x => x.Priority);
|
||||||
|
foreach (ITask task in startupTasks)
|
||||||
_queuedTasks.Enqueue((task, null));
|
_queuedTasks.Enqueue((task, null));
|
||||||
return base.StartAsync(cancellationToken);
|
return base.StartAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
@ -59,13 +74,22 @@ namespace Kyoo.Controllers
|
|||||||
|
|
||||||
public bool StartTask(string taskSlug, string arguments = null)
|
public bool StartTask(string taskSlug, string arguments = null)
|
||||||
{
|
{
|
||||||
ITask task = _tasks.FirstOrDefault(x => x.Slug == taskSlug);
|
int index = _tasks.FindIndex(x => x.task.Slug == taskSlug);
|
||||||
if (task == null)
|
if (index == -1)
|
||||||
return false;
|
return false;
|
||||||
_queuedTasks.Enqueue((task, arguments));
|
_queuedTasks.Enqueue((_tasks[index].task, arguments));
|
||||||
|
_tasks[index] = (_tasks[index].task, DateTime.Now + GetTaskDelay(taskSlug));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TimeSpan GetTaskDelay(string taskSlug)
|
||||||
|
{
|
||||||
|
TimeSpan delay = _configuration.GetSection("scheduledTasks").GetValue<TimeSpan>(taskSlug);
|
||||||
|
if (delay == default)
|
||||||
|
delay = TimeSpan.FromDays(365);
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
|
||||||
public ITask GetRunningTask()
|
public ITask GetRunningTask()
|
||||||
{
|
{
|
||||||
return _runningTask;
|
return _runningTask;
|
||||||
@ -74,16 +98,20 @@ namespace Kyoo.Controllers
|
|||||||
public void ReloadTask()
|
public void ReloadTask()
|
||||||
{
|
{
|
||||||
_tasks.Clear();
|
_tasks.Clear();
|
||||||
_tasks.AddRange(CoreTaskHolder.Tasks);
|
_tasks.AddRange(CoreTaskHolder.Tasks.Select(x => (x, DateTime.Now + GetTaskDelay(x.Slug))));
|
||||||
foreach (ITask task in _tasks.Where(x => x.RunOnStartup && x.Priority == Int32.MaxValue))
|
|
||||||
|
IEnumerable<ITask> prerunTasks = _tasks.Select(x => x.task)
|
||||||
|
.Where(x => x.RunOnStartup && x.Priority == Int32.MaxValue);
|
||||||
|
|
||||||
|
foreach (ITask task in prerunTasks)
|
||||||
task.Run(_serviceProvider, _taskToken.Token);
|
task.Run(_serviceProvider, _taskToken.Token);
|
||||||
foreach (IPlugin plugin in _pluginManager.GetAllPlugins())
|
foreach (IPlugin plugin in _pluginManager.GetAllPlugins())
|
||||||
_tasks.AddRange(plugin.Tasks);
|
_tasks.AddRange(plugin.Tasks.Select(x => (x, DateTime.Now + GetTaskDelay(x.Slug))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<ITask> GetAllTasks()
|
public IEnumerable<ITask> GetAllTasks()
|
||||||
{
|
{
|
||||||
return _tasks;
|
return _tasks.Select(x => x.task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,6 +14,10 @@
|
|||||||
"Database": "Data Source=kyoo.db"
|
"Database": "Data Source=kyoo.db"
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"scheduledTasks": {
|
||||||
|
"scan": "24:00:00"
|
||||||
|
},
|
||||||
|
|
||||||
"certificatePassword": "passphrase",
|
"certificatePassword": "passphrase",
|
||||||
|
|
||||||
"transmuxTempPath": "cached/kyoo/transmux",
|
"transmuxTempPath": "cached/kyoo/transmux",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user