Skip to content

Commit

Permalink
Add FailJob API
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 25, 2024
1 parent 95f2dcb commit 581f7c3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions MyApp.ServiceInterface/BackgroundMqServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
9 changes: 9 additions & 0 deletions MyApp.ServiceInterface/JobServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<PostJob>().Where(x => x.CompletedDate != null && x.Error != null));

foreach (var lostJob in lostJobs)
{
Expand All @@ -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",
]
};
}
Expand Down
10 changes: 10 additions & 0 deletions MyApp.ServiceModel/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CheckPostJobsResponse>
Expand Down Expand Up @@ -110,6 +111,15 @@ public class ViewModelQueuesResponse
[ValidateHasRole(Roles.Moderator)]
public class RestoreModelQueues : IGet, IReturn<StringsResponse> {}

[ValidateHasRole(Roles.Moderator)]
public class FailJob : IPost, IReturnVoid
{
public int Id { get; set; }
[ValidateNotEmpty]
public required string Error { get; set; }
}


public class QueryPosts : QueryDb<Post>
{
}
Expand Down
1 change: 1 addition & 0 deletions MyApp.ServiceModel/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@ public class DbWrites
public StartJob? StartJob { get; set; }
public int? AnswerAddedToPost { get; set; }
public List<int>? CompleteJobIds { get; set; }
public FailJob? FailJob { get; set; }
}
1 change: 1 addition & 0 deletions MyApp/Migrations/Migration1000.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 581f7c3

Please sign in to comment.