146 lines
4.2 KiB
C#
Raw Normal View History

2025-01-16 15:13:16 +01:00
using Logging;
2024-04-01 20:40:03 +02:00
namespace AutoClient
{
public class AutomaticPurchaser
2024-04-01 20:40:03 +02:00
{
2025-01-16 15:13:16 +01:00
private readonly App app;
2024-04-01 20:40:03 +02:00
private readonly ILog log;
2025-01-16 15:13:16 +01:00
private readonly CodexWrapper node;
2024-09-12 14:38:15 +02:00
private Task workerTask = Task.CompletedTask;
2024-04-01 20:40:03 +02:00
2025-01-16 15:13:16 +01:00
public AutomaticPurchaser(App app, ILog log, CodexWrapper node)
2024-04-01 20:40:03 +02:00
{
2025-01-16 15:13:16 +01:00
this.app = app;
2024-04-01 20:40:03 +02:00
this.log = log;
2025-01-16 15:13:16 +01:00
this.node = node;
2024-04-01 20:40:03 +02:00
}
2024-06-28 08:47:09 +02:00
public void Start()
{
2024-09-12 14:38:15 +02:00
workerTask = Task.Run(Worker);
}
public void Stop()
{
workerTask.Wait();
2024-06-28 08:47:09 +02:00
}
private async Task Worker()
2024-04-01 20:40:03 +02:00
{
2024-09-12 14:38:15 +02:00
log.Log("Worker started.");
while (!app.Cts.Token.IsCancellationRequested)
2024-04-01 20:40:03 +02:00
{
2024-09-12 14:38:15 +02:00
try
{
var pid = await StartNewPurchase();
await WaitTillFinished(pid);
}
catch (Exception ex)
{
log.Error("Worker failed with: " + ex);
await Task.Delay(TimeSpan.FromHours(6));
}
}
}
2024-06-28 08:47:09 +02:00
private async Task<string> StartNewPurchase()
2024-04-01 20:40:03 +02:00
{
var file = await CreateFile();
2024-10-21 11:10:39 +02:00
try
{
2025-01-16 15:13:16 +01:00
var cid = node.UploadFile(file);
var response = node.RequestStorage(cid);
2024-11-27 08:48:12 +01:00
return response.PurchaseId;
2024-10-21 11:10:39 +02:00
}
finally
{
DeleteFile(file);
}
2024-04-01 20:40:03 +02:00
}
private async Task<string> CreateFile()
2024-04-01 20:40:03 +02:00
{
2024-09-12 14:38:15 +02:00
return await app.Generator.Generate();
2024-04-01 20:40:03 +02:00
}
2024-10-21 11:10:39 +02:00
private void DeleteFile(string file)
{
try
{
File.Delete(file);
}
catch (Exception exc)
{
2025-01-16 15:13:16 +01:00
log.Error($"Failed to delete file '{file}': {exc}");
2024-10-21 11:10:39 +02:00
}
}
2024-06-28 08:47:09 +02:00
private async Task WaitTillFinished(string pid)
2024-04-01 20:40:03 +02:00
{
try
{
var emptyResponseTolerance = 10;
2024-09-12 14:38:15 +02:00
while (!app.Cts.Token.IsCancellationRequested)
2024-04-01 20:40:03 +02:00
{
2025-01-16 15:13:16 +01:00
var purchase = node.GetStoragePurchase(pid);
2024-09-12 14:38:15 +02:00
if (purchase == null)
2024-04-01 20:40:03 +02:00
{
2024-09-12 14:38:15 +02:00
await FixedShortDelay();
emptyResponseTolerance--;
if (emptyResponseTolerance == 0)
{
2024-06-28 08:47:09 +02:00
log.Log("Received 10 empty responses. Stop tracking this purchase.");
await ExpiryTimeDelay();
return;
}
2024-09-12 14:38:15 +02:00
continue;
}
2024-10-30 11:09:13 +01:00
if (purchase.IsCancelled)
{
2024-09-12 14:38:15 +02:00
app.Performance.StorageContractCancelled();
return;
}
2024-10-30 11:09:13 +01:00
if (purchase.IsError)
2024-09-12 14:38:15 +02:00
{
app.Performance.StorageContractErrored(purchase.Error);
return;
}
2024-10-30 11:09:13 +01:00
if (purchase.IsFinished)
2024-09-12 14:38:15 +02:00
{
app.Performance.StorageContractFinished();
return;
}
2024-10-30 11:09:13 +01:00
if (purchase.IsStarted)
2024-09-12 14:38:15 +02:00
{
app.Performance.StorageContractStarted();
await FixedDurationDelay();
2024-04-01 20:40:03 +02:00
}
2024-06-28 08:47:09 +02:00
await FixedShortDelay();
2024-04-01 20:40:03 +02:00
}
}
catch (Exception ex)
{
log.Log($"Wait failed with exception: {ex}. Assume contract will expire: Wait expiry time.");
await ExpiryTimeDelay();
2024-04-01 20:40:03 +02:00
}
}
private async Task FixedDurationDelay()
{
2024-09-12 14:38:15 +02:00
await Task.Delay(app.Config.ContractDurationMinutes * 60 * 1000, app.Cts.Token);
}
private async Task ExpiryTimeDelay()
2024-04-01 20:40:03 +02:00
{
2024-09-12 14:38:15 +02:00
await Task.Delay(app.Config.ContractExpiryMinutes * 60 * 1000, app.Cts.Token);
2024-04-01 20:40:03 +02:00
}
private async Task FixedShortDelay()
2024-04-01 20:40:03 +02:00
{
2024-09-12 14:38:15 +02:00
await Task.Delay(15 * 1000, app.Cts.Token);
2024-04-01 20:40:03 +02:00
}
}
}