From bbbaca185381801e6a0746bd00995b293b0ce480 Mon Sep 17 00:00:00 2001 From: Maxim Samsonov Date: Sat, 7 Sep 2024 20:47:51 +0300 Subject: [PATCH] Cleanup of staled rounds --- dkgNode/appsettings.Development.json | 4 +- dkgNode/appsettings.json | 6 +- dkgServiceNode/Controllers/OpsController.cs | 6 +- dkgServiceNode/Data/DbEnsure.cs | 6 +- .../RequestProcessor/RequuestProcessor.cs | 156 ------------------ dkgWebNode/appsettings.json | 2 +- 6 files changed, 12 insertions(+), 168 deletions(-) delete mode 100644 dkgServiceNode/Services/RequestProcessor/RequuestProcessor.cs diff --git a/dkgNode/appsettings.Development.json b/dkgNode/appsettings.Development.json index b2dcdb6..469ef73 100644 --- a/dkgNode/appsettings.Development.json +++ b/dkgNode/appsettings.Development.json @@ -1,8 +1,8 @@ { "Logging": { "LogLevel": { - "Default": "Information", - "Microsoft.Hosting.Lifetime": "Information" + "Default": "Warning", + "Microsoft.Hosting.Lifetime": "Warning" } } } diff --git a/dkgNode/appsettings.json b/dkgNode/appsettings.json index 1cf2031..05dc4ce 100644 --- a/dkgNode/appsettings.json +++ b/dkgNode/appsettings.json @@ -1,9 +1,9 @@ { "Logging": { "LogLevel": { - "Default": "Information", - "Microsoft.Hosting.Lifetime": "Information", - "Microsoft.EntityFrameworkCore": "Information" + "Default": "Warning", + "Microsoft.Hosting.Lifetime": "Warning", + "Microsoft.EntityFrameworkCore": "Warning" } }, "Node": { diff --git a/dkgServiceNode/Controllers/OpsController.cs b/dkgServiceNode/Controllers/OpsController.cs index 17e70cf..cb266d8 100644 --- a/dkgServiceNode/Controllers/OpsController.cs +++ b/dkgServiceNode/Controllers/OpsController.cs @@ -510,7 +510,11 @@ public async Task> Status(StatusReport statusReport var lastRoundHistory = dkgContext.GetLastNodeRoundHistory(node.Id, statusReport.RoundId); RStatus? rStatus = null; - if (round != null) rStatus = round.Status; + if (round != null) + { + await UpdateRoundState(round); + rStatus = round.Status; + } if (actionMap.TryGetValue((rStatus, statusReport.Status), out var function)) { diff --git a/dkgServiceNode/Data/DbEnsure.cs b/dkgServiceNode/Data/DbEnsure.cs index ef23ce4..c3783a5 100644 --- a/dkgServiceNode/Data/DbEnsure.cs +++ b/dkgServiceNode/Data/DbEnsure.cs @@ -257,12 +257,8 @@ public static void Ensure(string connectionString) } Ensure_0_8_0(connection); - PuVersionUpdate("0.9.6", connection); - PuVersionUpdate("0.10.2", connection); - PuVersionUpdate("0.11.0", connection); EnsureVersion("0.12.1", sqlScript_0_12_1, connection); - PuVersionUpdate("0.12.2", connection); - PuVersionUpdate("0.12.3", connection); + PuVersionUpdate("0.12.4", connection); } } diff --git a/dkgServiceNode/Services/RequestProcessor/RequuestProcessor.cs b/dkgServiceNode/Services/RequestProcessor/RequuestProcessor.cs deleted file mode 100644 index cf7f11e..0000000 --- a/dkgServiceNode/Services/RequestProcessor/RequuestProcessor.cs +++ /dev/null @@ -1,156 +0,0 @@ -using System.Threading.Tasks; -using System.Collections.Concurrent; -using Microsoft.EntityFrameworkCore; -using dkgServiceNode.Models; -using System.Xml.Linq; -using dkgServiceNode.Data; -using dkgServiceNode.Controllers; -using Microsoft.Extensions.Logging.Configuration; - -namespace dkgServiceNode.Services.RequestProcessor -{ - public class RequestProcessor(ILogger lgger) - { - private readonly ConcurrentQueue requestQueue = new(); - private readonly CancellationTokenSource cancellationTokenSource = new(); - private Task? backgroundTask = null; - private bool isRunning = false; - private DkgDbContext? dbContext; - private readonly ILogger logger = lgger; - - public enum Command - { - Add, - Update, - Delete - } - - public class Request(Command commandType, object data) - { - public Command CommandType { get; } = commandType; - public object Data { get; } = data; - } - - public void Start(DkgDbContext dC) - { - if (isRunning) - { - logger.LogWarning("Request Processor is already running. 'Start' ignored."); - } - else - { - - isRunning = true; - dbContext = dC; - backgroundTask = Task.Run(ProcessRequests, cancellationTokenSource.Token); - logger.LogInformation("Request Processor has been started."); - } - } - - public void Stop() - { - if (!isRunning) - { - logger.LogWarning("Request Processor is not running. 'Stop' ignored."); - } - else - { - cancellationTokenSource.Cancel(); - backgroundTask?.Wait(); - isRunning = false; - backgroundTask = null; - logger.LogInformation("Request Processor has been stopped."); - } - } - - public void EnqueueRequest(Request request) - { - requestQueue.Enqueue(request); - } - - public void EnqueueRequest(Command commandType, object data) - { - requestQueue.Enqueue(new Request(commandType, data)); - } - - private async Task ProcessRequests() - { - while (!cancellationTokenSource.Token.IsCancellationRequested && dbContext != null) - { - if (requestQueue.TryDequeue(out var request)) - { - try - { - await HandleRequestAsync(request); - } - catch (Exception ex) - { - logger.LogError("An error occurred while processing a request: {msg}.", ex.Message ); - } - } - await Task.Delay(100); - } - } - - private async Task HandleRequestAsync(Request request) - { - switch (request.CommandType) - { - case Command.Add: - await HandleAddAsync(request.Data); - break; - case Command.Update: - await HandleUpdateAsync(request.Data); - break; - case Command.Delete: - await HandleDeleteAsync(request.Data); - break; - default: - logger.LogError("Unknown command type '{command}'.", request.CommandType); - break; - } - } - - private async Task HandleAddAsync(object data) - { - if (data is Node node) - { - dbContext!.Nodes.Add(node); - await dbContext.SaveChangesAsync(cancellationTokenSource.Token.IsCancellationRequested); - } - else if (data is Round round) - { - dbContext!.Rounds.Add(round); - await dbContext.SaveChangesAsync(cancellationTokenSource.Token.IsCancellationRequested); - } - } - - private async Task HandleUpdateAsync(object data) - { - if (data is Node node) - { - dbContext!.Nodes.Update(node); - await dbContext.SaveChangesAsync(cancellationTokenSource.Token.IsCancellationRequested); - } - else if (data is Round round) - { - dbContext!.Rounds.Update(round); - await dbContext.SaveChangesAsync(cancellationTokenSource.Token.IsCancellationRequested); - } - } - - private async Task HandleDeleteAsync(object data) - { - if (data is Node node) - { - dbContext!.Nodes.Remove(node); - await dbContext.SaveChangesAsync(cancellationTokenSource.Token.IsCancellationRequested); - } - else if (data is Round round) - { - dbContext!.Rounds.Remove(round); - await dbContext.SaveChangesAsync(cancellationTokenSource.Token.IsCancellationRequested); - } - } - } -} diff --git a/dkgWebNode/appsettings.json b/dkgWebNode/appsettings.json index 7cb1bd5..d66442c 100644 --- a/dkgWebNode/appsettings.json +++ b/dkgWebNode/appsettings.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Debug", + "Default": "Warning", "Microsoft.AspNetCore": "Warning" } },