Add bot worker
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers;
|
||||
|
||||
public abstract class BaseWorker<T> : BackgroundService where T : class
|
||||
{
|
||||
private readonly WorkerType _workerType;
|
||||
protected readonly ILogger<T> _logger;
|
||||
protected readonly TimeSpan _delay;
|
||||
private readonly IWorkerService _workerService;
|
||||
private int _executionCount;
|
||||
|
||||
protected BaseWorker(
|
||||
WorkerType workerType,
|
||||
ILogger<T> logger,
|
||||
TimeSpan timeSpan,
|
||||
IWorkerService workerService)
|
||||
{
|
||||
_workerType = workerType;
|
||||
_logger = logger;
|
||||
_delay = timeSpan == TimeSpan.Zero ? TimeSpan.FromMinutes(1) : timeSpan;
|
||||
_workerService = workerService;
|
||||
_executionCount = 0;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"[{_workerType}] Starting");
|
||||
var worker = await _workerService.GetWorker(_workerType);
|
||||
|
||||
if (worker == null)
|
||||
{
|
||||
await _workerService.InsertWorker(_workerType, _delay);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation($"[{_workerType}] Last run : {worker.LastRunTime} - Execution Count : {worker.ExecutionCount}");
|
||||
_executionCount = worker.ExecutionCount;
|
||||
}
|
||||
|
||||
cancellationToken.Register(() => _logger.LogInformation($"[{_workerType}] Stopping"));
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
worker = await _workerService.GetWorker(_workerType);
|
||||
|
||||
//if (true)
|
||||
if (worker.IsActive)
|
||||
{
|
||||
await Run(cancellationToken);
|
||||
_executionCount++;
|
||||
await _workerService.UpdateWorker(_workerType, _executionCount);
|
||||
_logger.LogInformation($"[{_workerType}] Run ok. Next run at : {DateTime.UtcNow.Add(_delay)}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation($"[{_workerType}] Worker not active. Next run at : {DateTime.UtcNow.Add(_delay)}");
|
||||
}
|
||||
|
||||
await Task.Delay(_delay);
|
||||
}
|
||||
_logger.LogInformation($"[{_workerType}] Stopped");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Error : {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Task Run(CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Domain.Trades;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Common;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user