2023-05-31 22:32:37 +02:00
using Logging ;
using Newtonsoft.Json ;
2023-05-31 21:43:07 +02:00
using Newtonsoft.Json.Linq ;
using Tranga.TrangaTasks ;
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-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-05-20 14:07:38 +02:00
public string? connectorName { get ; }
2023-05-19 16:23:37 +02:00
public Task task { get ; }
public Publication ? publication { get ; }
2023-05-31 21:15:32 +02:00
public string? language { get ; }
2023-05-20 14:50:48 +02:00
[JsonIgnore] public ExecutionState state { get ; set ; }
2023-06-01 22:05:48 +02:00
[JsonIgnore] public float progress = > ( tasksFinished ! = 0f ? tasksFinished / tasksCount : 0f ) ;
[JsonIgnore] public float tasksCount { get ; set ; }
[JsonIgnore] public float tasksFinished { get ; set ; }
2023-05-20 14:50:48 +02:00
public enum ExecutionState
{
Waiting ,
Enqueued ,
Running
} ;
2023-05-19 14:00:30 +02:00
2023-05-31 21:15:32 +02:00
protected TrangaTask ( Task task , string? connectorName , Publication ? publication , TimeSpan reoccurrence , string? language = null )
2023-05-17 23:23:01 +02:00
{
2023-05-19 16:27:56 +02:00
this . publication = publication ;
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 ) ;
2023-05-19 16:27:56 +02:00
this . connectorName = connectorName ;
2023-05-19 14:00:30 +02:00
this . task = task ;
this . language = language ;
2023-06-01 22:05:48 +02:00
this . tasksCount = 1 ;
this . tasksFinished = 0 ;
2023-05-18 21:08:09 +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-01 10:35:23 +02:00
protected abstract void ExecuteTask ( TaskManager taskManager , Logger ? logger ) ;
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>
public void Execute ( TaskManager taskManager , Logger ? logger )
2023-05-31 22:32:37 +02:00
{
logger ? . WriteLine ( this . GetType ( ) . ToString ( ) , $"Executing Task {this}" ) ;
this . state = ExecutionState . Running ;
2023-06-01 10:35:23 +02:00
ExecuteTask ( taskManager , logger ) ;
this . lastExecuted = DateTime . Now ;
this . state = ExecutionState . Waiting ;
logger ? . WriteLine ( this . GetType ( ) . ToString ( ) , $"Finished Executing Task {this}" ) ;
2023-05-31 22:32:37 +02:00
}
2023-06-01 10:35:23 +02:00
/// <returns>True if elapsed time since last execution is greater than set interval</returns>
public bool ShouldExecute ( )
2023-05-31 22:32:37 +02:00
{
2023-06-01 10:35:23 +02:00
return DateTime . Now . Subtract ( this . lastExecuted ) > reoccurrence & & state is ExecutionState . Waiting ;
2023-05-31 22:32:37 +02:00
}
2023-05-31 22:26:53 +02:00
public enum Task : byte
2023-05-17 23:23:01 +02:00
{
2023-05-31 22:26:53 +02:00
DownloadNewChapters = 2 ,
UpdateKomgaLibrary = 3
2023-05-17 23:23:01 +02:00
}
2023-05-20 00:37:31 +02:00
public override string ToString ( )
{
2023-06-01 22:27:37 +02:00
return $"{task}, {lastExecuted}, {reoccurrence}, {state} {(connectorName is not null ? $" , { connectorName } " : " " )} {(publication is not null ? $" , { progress : 00.00 } % " : " ")} {(publication is not null ? $" , { publication ? . sortName } " : " ")}" ;
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 )
{
return ( objectType = = typeof ( TrangaTask ) ) ;
}
public override object ReadJson ( JsonReader reader , Type objectType , object? existingValue , JsonSerializer serializer )
{
JObject jo = JObject . Load ( reader ) ;
if ( jo [ "task" ] ! . Value < Int64 > ( ) = = ( Int64 ) Task . DownloadNewChapters )
return jo . ToObject < DownloadNewChaptersTask > ( serializer ) ! ;
if ( jo [ "task" ] ! . Value < Int64 > ( ) = = ( Int64 ) Task . UpdateKomgaLibrary )
return jo . ToObject < UpdateKomgaLibraryTask > ( serializer ) ! ;
throw new Exception ( ) ;
}
public override bool CanWrite = > false ;
2023-05-31 21:44:16 +02:00
/// <summary>
/// Don't call this
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
/// <exception cref="Exception"></exception>
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
}