diff --git a/Tools/AutoClient/Modes/FolderStore/FileWorker.cs b/Tools/AutoClient/Modes/FolderStore/FileWorker.cs index 1a818d0..5dfa83a 100644 --- a/Tools/AutoClient/Modes/FolderStore/FileWorker.cs +++ b/Tools/AutoClient/Modes/FolderStore/FileWorker.cs @@ -29,11 +29,22 @@ namespace AutoClient.Modes.FolderStore public int FailureCounter => State.FailureCounter; + protected override void OnNewState(WorkerStatus newState) + { + newState.LastUpdate = DateTime.MinValue; + } + public async Task Update() { try { Log($"Updating for '{sourceFilename}'..."); + if (IsCurrentlyRunning() && UpdatedRecently()) + { + Log("Is running, was recently checked. Skip."); + return; + } + var cid = await EnsureCid(); await EnsureRecentPurchase(cid); SaveState(); @@ -48,6 +59,12 @@ namespace AutoClient.Modes.FolderStore } } + private bool UpdatedRecently() + { + var now = DateTime.UtcNow; + return State.LastUpdate + TimeSpan.FromHours(1) > now; + } + private async Task EnsureCid() { Log($"Checking CID..."); @@ -186,6 +203,7 @@ namespace AutoClient.Modes.FolderStore if (!recent.Started.HasValue) recent.Started = now; if (!recent.Finish.HasValue) recent.Finish = now; } + State.LastUpdate = now; SaveState(); } @@ -249,6 +267,7 @@ namespace AutoClient.Modes.FolderStore [Serializable] public class WorkerStatus { + public DateTime LastUpdate { get; set; } public string Cid { get; set; } = string.Empty; public string EncodedCid { get; set; } = string.Empty; public int FailureCounter { get; set; } = 0; diff --git a/Tools/AutoClient/Modes/FolderStore/JsonBacked.cs b/Tools/AutoClient/Modes/FolderStore/JsonBacked.cs index d00581a..17b3dc7 100644 --- a/Tools/AutoClient/Modes/FolderStore/JsonBacked.cs +++ b/Tools/AutoClient/Modes/FolderStore/JsonBacked.cs @@ -21,6 +21,7 @@ namespace AutoClient.Modes.FolderStore if (!File.Exists(FilePath)) { State = new T(); + OnNewState(State); SaveState(); } var text = File.ReadAllText(FilePath); @@ -37,6 +38,10 @@ namespace AutoClient.Modes.FolderStore protected string FilePath { get; } protected T State { get; private set; } = default!; + protected virtual void OnNewState(T newState) + { + } + protected void SaveState() { try