Tranga-Website/Tranga/TrangaTask.cs

61 lines
2.0 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
namespace Tranga;
2023-05-18 21:08:09 +02:00
/// <summary>
/// Stores information on Task
/// </summary>
2023-05-19 16:27:56 +02:00
public class TrangaTask
{
// ReSharper disable once CommentTypo ...Tell me why!
2023-05-20 01:06:12 +02:00
// ReSharper disable once MemberCanBePrivate.Global I want it thaaat way
public TimeSpan reoccurrence { get; }
public DateTime lastExecuted { get; set; }
public string? connectorName { get; }
public Task task { get; }
public Publication? publication { get; }
public string language { get; }
[JsonIgnore]public ExecutionState state { get; set; }
public enum ExecutionState
{
Waiting,
Enqueued,
Running
};
public TrangaTask(Task task, string? connectorName, Publication? publication, TimeSpan reoccurrence, string language = "")
{
if(task != Task.UpdateKomgaLibrary && connectorName is null)
throw new ArgumentException($"connectorName can not be null for task {task}");
if (publication is null && task != Task.UpdatePublications && task != Task.UpdateKomgaLibrary)
throw new ArgumentException($"Publication can not be null for task {task}");
2023-05-19 16:27:56 +02:00
this.publication = publication;
2023-05-18 21:08:09 +02:00
this.reoccurrence = reoccurrence;
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
2023-05-19 16:27:56 +02:00
this.connectorName = connectorName;
this.task = task;
this.language = language;
2023-05-18 21:08:09 +02:00
}
/// <returns>True if elapsed time since last execution is greater than set interval</returns>
2023-05-19 16:27:56 +02:00
public bool ShouldExecute()
{
return DateTime.Now.Subtract(this.lastExecuted) > reoccurrence && state is ExecutionState.Waiting;
}
public enum Task
{
UpdatePublications,
UpdateChapters,
DownloadNewChapters,
UpdateKomgaLibrary
}
2023-05-20 00:37:31 +02:00
public override string ToString()
{
return $"{task}, {lastExecuted}, {reoccurrence}, {state} {(connectorName is not null ? $", {connectorName}" : "" )} {(publication is not null ? $", {publication?.sortName}": "")}";
2023-05-20 00:37:31 +02:00
}
}