From 581f7c3dd17f6914da6cd2ae555389121d0cd4a0 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Mon, 25 Mar 2024 17:22:50 +0800 Subject: [PATCH] Add FailJob API --- MyApp.ServiceInterface/BackgroundMqServices.cs | 9 +++++++++ MyApp.ServiceInterface/JobServices.cs | 9 +++++++++ MyApp.ServiceModel/Posts.cs | 10 ++++++++++ MyApp.ServiceModel/Stats.cs | 1 + MyApp/Migrations/Migration1000.cs | 1 + 5 files changed, 30 insertions(+) diff --git a/MyApp.ServiceInterface/BackgroundMqServices.cs b/MyApp.ServiceInterface/BackgroundMqServices.cs index 0998b01..e0f67c6 100644 --- a/MyApp.ServiceInterface/BackgroundMqServices.cs +++ b/MyApp.ServiceInterface/BackgroundMqServices.cs @@ -104,6 +104,15 @@ public async Task Any(DbWrites request) } } } + + if (request.FailJob != null) + { + await Db.UpdateOnlyAsync(() => new PostJob { + CompletedDate = DateTime.UtcNow, + Error = request.FailJob.Error, + }, + x => x.PostId == request.FailJob.Id); + } if (request.AnswerAddedToPost != null) { diff --git a/MyApp.ServiceInterface/JobServices.cs b/MyApp.ServiceInterface/JobServices.cs index 716df7e..010d38d 100644 --- a/MyApp.ServiceInterface/JobServices.cs +++ b/MyApp.ServiceInterface/JobServices.cs @@ -32,6 +32,13 @@ public object Any(GetNextJobs request) }; } + public void Any(FailJob request) + { + MessageProducer.Publish(new DbWrites { + FailJob = request + }); + } + public object Any(RestoreModelQueues request) { var pendingJobs = workerQueues.GetAll(); @@ -41,6 +48,7 @@ public object Any(RestoreModelQueues request) var startedJobs = incompleteJobs.Where(x => x.StartedDate != null).ToList(); var lostJobsBefore = DateTime.UtcNow.Add(TimeSpan.FromMinutes(-5)); var lostJobs = startedJobs.Where(x => x.StartedDate < lostJobsBefore && missingJobs.All(m => m.Id != x.Id)).ToList(); + var failedJobsCount = Db.Count(Db.From().Where(x => x.CompletedDate != null && x.Error != null)); foreach (var lostJob in lostJobs) { @@ -58,6 +66,7 @@ public object Any(RestoreModelQueues request) $"{incompleteJobs.Count} incomplete jobs in database", $"{startedJobs.Count} jobs being processed by workers", $"{missingJobs.Count} missing and {lostJobs.Count} lost jobs re-added to queue", + $"{failedJobsCount} failed jobs", ] }; } diff --git a/MyApp.ServiceModel/Posts.cs b/MyApp.ServiceModel/Posts.cs index c6e58c3..d9ecb91 100644 --- a/MyApp.ServiceModel/Posts.cs +++ b/MyApp.ServiceModel/Posts.cs @@ -70,6 +70,7 @@ public class PostJob public string? Worker { get; set; } public string? WorkerIp { get; set; } public DateTime? CompletedDate { get; set; } + public string? Error { get; set; } } public class CheckPostJobs : IGet, IReturn @@ -110,6 +111,15 @@ public class ViewModelQueuesResponse [ValidateHasRole(Roles.Moderator)] public class RestoreModelQueues : IGet, IReturn {} +[ValidateHasRole(Roles.Moderator)] +public class FailJob : IPost, IReturnVoid +{ + public int Id { get; set; } + [ValidateNotEmpty] + public required string Error { get; set; } +} + + public class QueryPosts : QueryDb { } diff --git a/MyApp.ServiceModel/Stats.cs b/MyApp.ServiceModel/Stats.cs index 1ccee72..5db0481 100644 --- a/MyApp.ServiceModel/Stats.cs +++ b/MyApp.ServiceModel/Stats.cs @@ -118,4 +118,5 @@ public class DbWrites public StartJob? StartJob { get; set; } public int? AnswerAddedToPost { get; set; } public List? CompleteJobIds { get; set; } + public FailJob? FailJob { get; set; } } \ No newline at end of file diff --git a/MyApp/Migrations/Migration1000.cs b/MyApp/Migrations/Migration1000.cs index 0ee08e1..0577bae 100644 --- a/MyApp/Migrations/Migration1000.cs +++ b/MyApp/Migrations/Migration1000.cs @@ -86,6 +86,7 @@ public class PostJob public string? Worker { get; set; } public string? WorkerIp { get; set; } public DateTime? CompletedDate { get; set; } + public string? Error { get; set; } } public override void Up()