mirror of
https://github.com/C9Glax/tranga.git
synced 2025-07-03 17:34:17 +02:00
Added Jobs and ProgressToken
This commit is contained in:
73
Tranga/Jobs/Job.cs
Normal file
73
Tranga/Jobs/Job.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using Tranga.MangaConnectors;
|
||||
|
||||
namespace Tranga.Jobs;
|
||||
|
||||
public abstract class Job
|
||||
{
|
||||
public MangaConnector mangaConnector { get; init; }
|
||||
public ProgressToken progressToken { get; private set; }
|
||||
public bool recurring { get; init; }
|
||||
public TimeSpan? recurrenceTime { get; set; }
|
||||
public DateTime? lastExecution { get; private set; }
|
||||
public DateTime nextExecution => NextExecution();
|
||||
|
||||
public Job(MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null)
|
||||
{
|
||||
this.mangaConnector = connector;
|
||||
this.progressToken = new ProgressToken(0);
|
||||
this.recurring = recurring;
|
||||
if (recurring && recurrenceTime is null)
|
||||
throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided.");
|
||||
this.recurrenceTime = recurrenceTime;
|
||||
}
|
||||
|
||||
public Job(MangaConnector connector, ProgressToken progressToken, bool recurring = false, TimeSpan? recurrenceTime = null)
|
||||
{
|
||||
this.mangaConnector = connector;
|
||||
this.progressToken = progressToken;
|
||||
this.recurring = recurring;
|
||||
if (recurring && recurrenceTime is null)
|
||||
throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided.");
|
||||
this.recurrenceTime = recurrenceTime;
|
||||
}
|
||||
|
||||
public Job(MangaConnector connector, int taskIncrements, bool recurring = false, TimeSpan? recurrenceTime = null)
|
||||
{
|
||||
this.mangaConnector = connector;
|
||||
this.progressToken = new ProgressToken(taskIncrements);
|
||||
this.recurring = recurring;
|
||||
if (recurring && recurrenceTime is null)
|
||||
throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided.");
|
||||
this.recurrenceTime = recurrenceTime;
|
||||
}
|
||||
|
||||
private DateTime NextExecution()
|
||||
{
|
||||
if(recurring && recurrenceTime.HasValue && lastExecution.HasValue)
|
||||
return lastExecution.Value.Add(recurrenceTime.Value);
|
||||
if(recurring && recurrenceTime.HasValue && !lastExecution.HasValue)
|
||||
return DateTime.Now;
|
||||
return DateTime.MaxValue;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
this.progressToken = new ProgressToken(this.progressToken.increments);
|
||||
}
|
||||
|
||||
public void Cancel()
|
||||
{
|
||||
this.progressToken.cancellationRequested = true;
|
||||
this.progressToken.Complete();
|
||||
}
|
||||
|
||||
public IEnumerable<Job> ExecuteReturnSubTasks()
|
||||
{
|
||||
progressToken.Start();
|
||||
IEnumerable<Job> ret = ExecuteReturnSubTasksInternal();
|
||||
lastExecution = DateTime.Now;
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected abstract IEnumerable<Job> ExecuteReturnSubTasksInternal();
|
||||
}
|
Reference in New Issue
Block a user