Limits number of concurrently starting contracts

This commit is contained in:
benbierens 2024-11-27 10:43:16 +01:00
parent 025c85c1aa
commit 9853a0b7db
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
4 changed files with 34 additions and 3 deletions

View File

@ -23,7 +23,7 @@ namespace AutoClient
if (!string.IsNullOrEmpty(config.FolderToStore))
{
FolderWorkDispatcher = new FolderWorkDispatcher(config.FolderToStore);
FolderWorkDispatcher = new FolderWorkDispatcher(Log, config.FolderToStore);
}
else
{

View File

@ -12,6 +12,19 @@ namespace AutoClient.Modes.FolderStore
this.purchaseInfo = purchaseInfo;
}
public bool IsBusy()
{
if (!State.Purchases.Any()) return false;
return State.Purchases.Any(p =>
p.Submitted.HasValue &&
!p.Started.HasValue &&
!p.Expiry.HasValue &&
!p.Finish.HasValue &&
p.Created > DateTime.UtcNow - purchaseInfo.PurchaseDurationTotal
);
}
public bool IsCurrentlyRunning()
{
if (!State.Purchases.Any()) return false;

View File

@ -1,11 +1,15 @@
namespace AutoClient.Modes.FolderStore
using Logging;
namespace AutoClient.Modes.FolderStore
{
public class FolderWorkDispatcher
{
private readonly string[] files = Array.Empty<string>();
private readonly ILog log;
private int index = 0;
private int busyCount = 0;
public FolderWorkDispatcher(string folder)
public FolderWorkDispatcher(ILog log, string folder)
{
var fs = Directory.GetFiles(folder);
var result = new List<string>();
@ -21,10 +25,17 @@
}
}
files = result.ToArray();
this.log = log;
}
public FileIndex GetFileToCheck()
{
if (busyCount > 1)
{
log.Log("Max number of busy workers reached. Waiting until contracts are started before creating any more.");
ResetIndex();
}
var file = new FileIndex(files[index], index);
index = (index + 1) % files.Length;
return file;
@ -33,6 +44,12 @@
public void ResetIndex()
{
index = 0;
busyCount = 0;
}
public void WorkerIsBusy()
{
busyCount++;
}
}

View File

@ -64,6 +64,7 @@ namespace AutoClient.Modes
var file = app.FolderWorkDispatcher.GetFileToCheck();
var worker = new FileWorker(app, instance, purchaseInfo, folder, file, OnFileUploaded, OnNewPurchase);
await worker.Update();
if (worker.IsBusy()) app.FolderWorkDispatcher.WorkerIsBusy();
return worker;
}