2023-06-20 15:46:54 +02:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
using Logging;
|
2023-05-31 22:32:37 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2023-05-31 21:43:07 +02:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Tranga.TrangaTasks;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
using JsonConverter = Newtonsoft.Json.JsonConverter;
|
2023-05-19 19:20:06 +02:00
|
|
|
|
|
|
|
|
|
namespace Tranga;
|
2023-05-18 21:08:09 +02:00
|
|
|
|
|
2023-05-19 20:03:17 +02:00
|
|
|
|
/// <summary>
|
2023-05-31 21:44:16 +02:00
|
|
|
|
/// Stores information on Task, when implementing new Tasks also update the serializer
|
2023-05-19 20:03:17 +02:00
|
|
|
|
/// </summary>
|
2023-06-20 14:57:44 +02:00
|
|
|
|
[JsonDerivedType(typeof(MonitorPublicationTask), 2)]
|
2023-06-05 00:35:57 +02:00
|
|
|
|
[JsonDerivedType(typeof(UpdateLibrariesTask), 3)]
|
|
|
|
|
[JsonDerivedType(typeof(DownloadChapterTask), 4)]
|
2023-05-31 21:15:32 +02:00
|
|
|
|
public abstract class TrangaTask
|
2023-05-19 14:00:30 +02:00
|
|
|
|
{
|
2023-05-20 12:53:54 +02:00
|
|
|
|
// 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
|
2023-05-19 16:23:37 +02:00
|
|
|
|
public TimeSpan reoccurrence { get; }
|
|
|
|
|
public DateTime lastExecuted { get; set; }
|
2023-06-11 18:24:26 +02:00
|
|
|
|
[Newtonsoft.Json.JsonIgnore] public ExecutionState state { get; set; }
|
2023-06-20 14:57:44 +02:00
|
|
|
|
public Task task { get; }
|
2023-06-21 17:29:48 +02:00
|
|
|
|
public string taskId { get; init; }
|
2023-06-11 18:24:26 +02:00
|
|
|
|
[Newtonsoft.Json.JsonIgnore] public TrangaTask? parentTask { get; set; }
|
|
|
|
|
public string? parentTaskId { get; set; }
|
2023-06-21 17:30:31 +02:00
|
|
|
|
[Newtonsoft.Json.JsonIgnore] internal HashSet<TrangaTask> childTasks { get; }
|
2023-06-20 23:15:56 +02:00
|
|
|
|
public double progress => GetProgress();
|
2023-06-07 00:27:53 +02:00
|
|
|
|
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; }
|
2023-06-21 17:30:31 +02:00
|
|
|
|
[Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; internal set; }
|
|
|
|
|
[Newtonsoft.Json.JsonIgnore]public DateTime executionApproximatelyFinished => lastChange.Add(GetRemainingTime());
|
2023-06-20 23:15:56 +02:00
|
|
|
|
public TimeSpan executionApproximatelyRemaining => executionApproximatelyFinished.Subtract(DateTime.Now);
|
2023-06-11 18:24:26 +02:00
|
|
|
|
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
|
2023-05-20 14:50:48 +02:00
|
|
|
|
|
2023-06-20 15:46:54 +02:00
|
|
|
|
public enum ExecutionState { Waiting, Enqueued, Running, Failed, Success }
|
2023-05-19 14:00:30 +02:00
|
|
|
|
|
2023-06-11 17:27:33 +02:00
|
|
|
|
protected TrangaTask(Task task, TimeSpan reoccurrence, TrangaTask? parentTask = null)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 21:08:09 +02:00
|
|
|
|
this.reoccurrence = reoccurrence;
|
2023-05-19 14:00:30 +02:00
|
|
|
|
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
|
|
|
|
|
this.task = task;
|
2023-06-20 14:57:44 +02:00
|
|
|
|
this.executionStarted = DateTime.UnixEpoch;
|
2023-06-09 23:42:54 +02:00
|
|
|
|
this.lastChange = DateTime.MaxValue;
|
2023-06-19 17:17:24 +02:00
|
|
|
|
this.taskId = Convert.ToBase64String(BitConverter.GetBytes(new Random().Next()));
|
2023-06-11 17:04:33 +02:00
|
|
|
|
this.childTasks = new();
|
2023-06-11 17:27:33 +02:00
|
|
|
|
this.parentTask = parentTask;
|
|
|
|
|
this.parentTaskId = parentTask?.taskId;
|
2023-06-09 23:38:28 +02:00
|
|
|
|
}
|
2023-06-01 10:35:23 +02:00
|
|
|
|
|
2023-05-31 21:40:00 +02:00
|
|
|
|
/// <summary>
|
2023-06-01 10:35:23 +02:00
|
|
|
|
/// BL for concrete Tasks
|
2023-05-31 21:40:00 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="taskManager"></param>
|
2023-05-31 22:32:37 +02:00
|
|
|
|
/// <param name="logger"></param>
|
2023-06-10 14:27:09 +02:00
|
|
|
|
/// <param name="cancellationToken"></param>
|
2023-06-20 15:46:54 +02:00
|
|
|
|
protected abstract HttpStatusCode ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null);
|
2023-06-20 14:57:44 +02:00
|
|
|
|
|
|
|
|
|
public abstract TrangaTask Clone();
|
|
|
|
|
|
|
|
|
|
protected abstract double GetProgress();
|
2023-05-17 23:23:01 +02:00
|
|
|
|
|
2023-06-01 10:35:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute the task
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="taskManager">Should be the parent taskManager</param>
|
|
|
|
|
/// <param name="logger"></param>
|
2023-06-10 14:27:09 +02:00
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
public void Execute(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
2023-05-31 22:32:37 +02:00
|
|
|
|
{
|
|
|
|
|
logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}");
|
|
|
|
|
this.state = ExecutionState.Running;
|
2023-06-06 21:19:30 +02:00
|
|
|
|
this.executionStarted = DateTime.Now;
|
2023-06-09 23:42:54 +02:00
|
|
|
|
this.lastChange = DateTime.Now;
|
2023-06-24 21:00:26 +02:00
|
|
|
|
if(parentTask is not null && parentTask.childTasks.All(ct => ct.state is ExecutionState.Waiting or ExecutionState.Failed))
|
2023-06-21 17:30:31 +02:00
|
|
|
|
parentTask.executionStarted = DateTime.Now;
|
|
|
|
|
|
2023-06-20 15:46:54 +02:00
|
|
|
|
HttpStatusCode statusCode = ExecuteTask(taskManager, logger, cancellationToken);
|
2023-06-21 17:30:31 +02:00
|
|
|
|
|
2023-06-20 15:46:54 +02:00
|
|
|
|
if ((int)statusCode >= 200 && (int)statusCode < 300)
|
2023-06-20 14:57:44 +02:00
|
|
|
|
{
|
|
|
|
|
this.lastExecuted = DateTime.Now;
|
2023-06-28 22:43:46 +02:00
|
|
|
|
this.state = ExecutionState.Success;
|
2023-06-20 14:57:44 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-06-27 23:55:13 +02:00
|
|
|
|
this.state = ExecutionState.Failed;
|
2023-06-20 14:57:44 +02:00
|
|
|
|
this.lastExecuted = DateTime.MaxValue;
|
|
|
|
|
}
|
2023-06-27 23:55:13 +02:00
|
|
|
|
|
2023-07-02 23:06:24 +02:00
|
|
|
|
if (this is DownloadChapterTask)
|
|
|
|
|
taskManager.DeleteTask(this);
|
2023-06-27 23:55:13 +02:00
|
|
|
|
|
2023-06-01 10:35:23 +02:00
|
|
|
|
logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}");
|
2023-05-31 22:32:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-11 18:24:26 +02:00
|
|
|
|
public void AddChildTask(TrangaTask childTask)
|
2023-06-11 17:04:33 +02:00
|
|
|
|
{
|
|
|
|
|
this.childTasks.Add(childTask);
|
|
|
|
|
}
|
2023-06-11 17:27:33 +02:00
|
|
|
|
|
2023-06-15 18:22:59 +02:00
|
|
|
|
public void RemoveChildTask(TrangaTask childTask)
|
|
|
|
|
{
|
|
|
|
|
this.childTasks.Remove(childTask);
|
2023-05-31 22:32:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-11 18:24:26 +02:00
|
|
|
|
private TimeSpan GetRemainingTime()
|
|
|
|
|
{
|
2023-06-21 17:30:31 +02:00
|
|
|
|
if(progress == 0 || state is ExecutionState.Enqueued or ExecutionState.Waiting or ExecutionState.Failed || lastChange == DateTime.MaxValue)
|
|
|
|
|
return DateTime.MaxValue.Subtract(lastChange).Subtract(TimeSpan.FromHours(1));
|
2023-06-11 18:24:26 +02:00
|
|
|
|
TimeSpan elapsed = lastChange.Subtract(executionStarted);
|
2023-06-21 17:30:31 +02:00
|
|
|
|
return elapsed.Divide(progress).Multiply(1 - progress);
|
2023-06-11 18:24:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 22:26:53 +02:00
|
|
|
|
public enum Task : byte
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-06-20 14:57:44 +02:00
|
|
|
|
MonitorPublication = 2,
|
2023-06-05 00:35:57 +02:00
|
|
|
|
UpdateLibraries = 3,
|
2023-06-20 14:57:44 +02:00
|
|
|
|
DownloadChapter = 4,
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
2023-05-20 00:37:31 +02:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2023-06-06 21:19:30 +02:00
|
|
|
|
return $"{task}, {lastExecuted}, {reoccurrence}, {state}, {progress:P2}, {executionApproximatelyFinished}, {executionApproximatelyRemaining}";
|
2023-05-20 00:37:31 +02:00
|
|
|
|
}
|
2023-05-31 21:43:07 +02:00
|
|
|
|
|
|
|
|
|
public class TrangaTaskJsonConverter : JsonConverter
|
|
|
|
|
{
|
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
|
|
|
{
|
2023-06-20 14:57:44 +02:00
|
|
|
|
return objectType == typeof(TrangaTask);
|
2023-05-31 21:43:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
JObject jo = JObject.Load(reader);
|
2023-06-20 14:57:44 +02:00
|
|
|
|
if (jo["task"]!.Value<Int64>() == (Int64)Task.MonitorPublication)
|
|
|
|
|
return jo.ToObject<MonitorPublicationTask>(serializer)!;
|
2023-05-31 21:43:07 +02:00
|
|
|
|
|
2023-06-03 15:17:08 +02:00
|
|
|
|
if (jo["task"]!.Value<Int64>() == (Int64)Task.UpdateLibraries)
|
|
|
|
|
return jo.ToObject<UpdateLibrariesTask>(serializer)!;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
|
|
|
|
|
if (jo["task"]!.Value<Int64>() == (Int64)Task.DownloadChapter)
|
|
|
|
|
return jo.ToObject<DownloadChapterTask>(serializer)!;
|
2023-05-31 21:43:07 +02:00
|
|
|
|
|
|
|
|
|
throw new Exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanWrite => false;
|
|
|
|
|
|
2023-05-31 21:44:16 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Don't call this
|
|
|
|
|
/// </summary>
|
2023-05-31 21:43:07 +02:00
|
|
|
|
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Dont call this");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|