From d48caa44d6dcb1a53170a0d14863d65ab5a3d9f5 Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 11 Nov 2024 13:41:02 +0100 Subject: [PATCH] stops autoclient after 5 failures --- Tools/AutoClient/Modes/FolderStoreMode.cs | 30 ++++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Tools/AutoClient/Modes/FolderStoreMode.cs b/Tools/AutoClient/Modes/FolderStoreMode.cs index 696d35f..0ea6918 100644 --- a/Tools/AutoClient/Modes/FolderStoreMode.cs +++ b/Tools/AutoClient/Modes/FolderStoreMode.cs @@ -40,10 +40,14 @@ namespace AutoClient.Modes var i = 0; while (!cts.IsCancellationRequested) { - if (app.FolderWorkDispatcher.Revisiting) Thread.Sleep(500); - else Thread.Sleep(2000); + if (app.FolderWorkDispatcher.Revisiting) Thread.Sleep(2000); + else Thread.Sleep(5000); - await ProcessWorkItem(instance); + var worker = await ProcessWorkItem(instance); + if (worker.FailureCounter > 5) + { + throw new Exception("Worker has failure count > 5. Stopping AutoClient..."); + } i++; if (i > 5) @@ -55,7 +59,7 @@ namespace AutoClient.Modes } } - private async Task ProcessWorkItem(ICodexInstance instance) + private async Task ProcessWorkItem(ICodexInstance instance) { var file = app.FolderWorkDispatcher.GetFileToCheck(); var worker = new FileWorker(app, purchaseInfo, folder, file); @@ -63,6 +67,8 @@ namespace AutoClient.Modes { app.FolderWorkDispatcher.RevisitSoon(file); }); + + return worker; } public void Stop() @@ -92,6 +98,8 @@ namespace AutoClient.Modes sourceFilename = filename; } + public int FailureCounter => State.FailureCounter; + public async Task Update(ICodexInstance instance, Action shouldRevisitSoon) { try @@ -122,9 +130,18 @@ namespace AutoClient.Modes await UpdatePurchase(recent, instance, codex); - if (recent.Expiry.HasValue || recent.Finish.HasValue) + if (recent.Expiry.HasValue) { - app.Log.Log($"Purchase for '{sourceFilename}' has expired or finished."); + app.Log.Log($"Purchase for '{sourceFilename}' has failed or expired."); + await MakeNewPurchase(instance, codex); + shouldRevisitSoon(); + State.FailureCounter++; + return; + } + + if (recent.Finish.HasValue) + { + app.Log.Log($"Purchase for '{sourceFilename}' has finished."); await MakeNewPurchase(instance, codex); shouldRevisitSoon(); return; @@ -299,6 +316,7 @@ namespace AutoClient.Modes public class WorkerStatus { public string Cid { get; set; } = string.Empty; + public int FailureCounter { get; set; } = 0; public WorkerPurchase[] Purchases { get; set; } = Array.Empty(); }