2021-09-27 12:42:46 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.RateLimiter;
|
|
|
|
|
|
|
|
public class Job<T> : IJob, IDisposable
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
public ulong ID { get; internal init; }
|
|
|
|
public string Description { get; internal init; }
|
|
|
|
public bool Started { get; internal set; }
|
|
|
|
public IResource<T> Resource { get; init; }
|
|
|
|
|
2021-10-23 18:36:35 +00:00
|
|
|
private bool _isFinished = false;
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2021-10-23 22:27:59 +00:00
|
|
|
if (_isFinished) return;
|
2021-10-23 18:36:35 +00:00
|
|
|
_isFinished = true;
|
2021-10-23 16:51:17 +00:00
|
|
|
Resource.Finish(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long Current { get; internal set; }
|
|
|
|
public long? Size { get; set; }
|
|
|
|
|
2023-05-07 20:32:18 +00:00
|
|
|
public async ValueTask Report(long processedSize, CancellationToken token)
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2023-05-07 20:32:18 +00:00
|
|
|
await Resource.Report(this, (int)Math.Min(processedSize, int.MaxValue), token);
|
2021-10-23 16:51:17 +00:00
|
|
|
Current += processedSize;
|
2021-12-27 15:37:20 +00:00
|
|
|
OnUpdate?.Invoke(this, (Percent.FactoryPutInRange(Current, Size ?? 1), Current));
|
2021-10-23 16:51:17 +00:00
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public void ReportNoWait(int processedSize)
|
|
|
|
{
|
|
|
|
Resource.ReportNoWait(this, processedSize);
|
2021-12-27 15:37:20 +00:00
|
|
|
OnUpdate?.Invoke(this, (Percent.FactoryPutInRange(Current, Size ?? 1), Current));
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
2021-12-27 15:37:20 +00:00
|
|
|
|
2023-05-07 20:32:18 +00:00
|
|
|
public void ResetProgress()
|
|
|
|
{
|
|
|
|
Current = 0;
|
|
|
|
OnUpdate?.Invoke(this, (Percent.FactoryPutInRange(Current, Size ?? 1), Current));
|
|
|
|
}
|
|
|
|
|
2021-12-27 15:37:20 +00:00
|
|
|
public event EventHandler<(Percent Progress, long Processed)> OnUpdate;
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|