mirror of
https://github.com/C9Glax/tranga.git
synced 2025-07-04 01:44:17 +02:00
Added Jobs and ProgressToken
This commit is contained in:
70
Tranga/Jobs/JobBoss.cs
Normal file
70
Tranga/Jobs/JobBoss.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using Tranga.MangaConnectors;
|
||||
|
||||
namespace Tranga.Jobs;
|
||||
|
||||
public class JobBoss : GlobalBase
|
||||
{
|
||||
private HashSet<Job> jobs { get; init; }
|
||||
private Dictionary<MangaConnector, Queue<Job>> mangaConnectorJobQueue { get; init; }
|
||||
|
||||
public JobBoss(GlobalBase clone) : base(clone)
|
||||
{
|
||||
this.jobs = new();
|
||||
this.mangaConnectorJobQueue = new();
|
||||
}
|
||||
|
||||
public void MonitorJobs()
|
||||
{
|
||||
foreach (Job job in jobs.Where(job => job.nextExecution < DateTime.Now && !QueueContainsJob(job)).OrderBy(job => job.nextExecution))
|
||||
AddJobToQueue(job);
|
||||
CheckJobQueue();
|
||||
}
|
||||
|
||||
public void AddJob(Job job)
|
||||
{
|
||||
this.jobs.Add(job);
|
||||
}
|
||||
|
||||
public void RemoveJob(Job job)
|
||||
{
|
||||
job.Cancel();
|
||||
this.jobs.Remove(job);
|
||||
}
|
||||
|
||||
private bool QueueContainsJob(Job job)
|
||||
{
|
||||
mangaConnectorJobQueue.TryAdd(job.mangaConnector, new Queue<Job>());
|
||||
return mangaConnectorJobQueue[job.mangaConnector].Contains(job);
|
||||
}
|
||||
|
||||
private void AddJobToQueue(Job job)
|
||||
{
|
||||
Log($"Adding Job to Queue. {job}");
|
||||
mangaConnectorJobQueue.TryAdd(job.mangaConnector, new Queue<Job>());
|
||||
Queue<Job> connectorJobQueue = mangaConnectorJobQueue[job.mangaConnector];
|
||||
if(!connectorJobQueue.Contains(job))
|
||||
connectorJobQueue.Enqueue(job);
|
||||
}
|
||||
|
||||
public void AddJobsToQueue(IEnumerable<Job> jobs)
|
||||
{
|
||||
foreach(Job job in jobs)
|
||||
AddJobToQueue(job);
|
||||
}
|
||||
|
||||
private void CheckJobQueue()
|
||||
{
|
||||
foreach (Queue<Job> jobQueue in mangaConnectorJobQueue.Values)
|
||||
{
|
||||
Job queueHead = jobQueue.Peek();
|
||||
if (queueHead.progressToken.state == ProgressToken.State.Complete)
|
||||
{
|
||||
if(queueHead.recurring)
|
||||
queueHead.Reset();
|
||||
jobQueue.Dequeue();
|
||||
AddJobsToQueue(jobQueue.Peek().ExecuteReturnSubTasks());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user