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 Kyoo.Models;
|
||||
using Kyoo.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Kyoo.Controllers
|
||||
@ -13,16 +14,18 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
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 ITask _runningTask;
|
||||
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;
|
||||
_pluginManager = pluginManager;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
@ -37,16 +40,28 @@ namespace Kyoo.Controllers
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
await Task.Delay(1000_000, cancellationToken);
|
||||
await Task.Delay(10, cancellationToken);
|
||||
QueueScheduledTasks();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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));
|
||||
return base.StartAsync(cancellationToken);
|
||||
}
|
||||
@ -59,12 +74,21 @@ namespace Kyoo.Controllers
|
||||
|
||||
public bool StartTask(string taskSlug, string arguments = null)
|
||||
{
|
||||
ITask task = _tasks.FirstOrDefault(x => x.Slug == taskSlug);
|
||||
if (task == null)
|
||||
int index = _tasks.FindIndex(x => x.task.Slug == taskSlug);
|
||||
if (index == -1)
|
||||
return false;
|
||||
_queuedTasks.Enqueue((task, arguments));
|
||||
_queuedTasks.Enqueue((_tasks[index].task, arguments));
|
||||
_tasks[index] = (_tasks[index].task, DateTime.Now + GetTaskDelay(taskSlug));
|
||||
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()
|
||||
{
|
||||
@ -74,16 +98,20 @@ namespace Kyoo.Controllers
|
||||
public void ReloadTask()
|
||||
{
|
||||
_tasks.Clear();
|
||||
_tasks.AddRange(CoreTaskHolder.Tasks);
|
||||
foreach (ITask task in _tasks.Where(x => x.RunOnStartup && x.Priority == Int32.MaxValue))
|
||||
_tasks.AddRange(CoreTaskHolder.Tasks.Select(x => (x, DateTime.Now + GetTaskDelay(x.Slug))));
|
||||
|
||||
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);
|
||||
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()
|
||||
{
|
||||
return _tasks;
|
||||
return _tasks.Select(x => x.task);
|
||||
}
|
||||
}
|
||||
}
|
@ -14,6 +14,10 @@
|
||||
"Database": "Data Source=kyoo.db"
|
||||
},
|
||||
|
||||
"scheduledTasks": {
|
||||
"scan": "24:00:00"
|
||||
},
|
||||
|
||||
"certificatePassword": "passphrase",
|
||||
|
||||
"transmuxTempPath": "cached/kyoo/transmux",
|
||||
|
Loading…
x
Reference in New Issue
Block a user