Skip to content

Commit

Permalink
Add ability to RestoreFailedJobs
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 25, 2024
1 parent 581f7c3 commit 28aeccd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 10 additions & 4 deletions MyApp.ServiceInterface/JobServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ public void Any(FailJob request)
});
}

public object Any(RestoreModelQueues request)
public async Task<object> Any(RestoreModelQueues request)
{
var pendingJobs = workerQueues.GetAll();
var pendingJobIds = pendingJobs.Select(x => x.Id).ToSet();
var incompleteJobs = Db.Select(Db.From<PostJob>().Where(x => x.CompletedDate == null));
var incompleteJobs = await Db.SelectAsync(Db.From<PostJob>().Where(x => x.CompletedDate == null));
var missingJobs = incompleteJobs.Where(x => !pendingJobIds.Contains(x.Id) && x.StartedDate == null).ToList();
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));
var failedJobs = await Db.SelectAsync(Db.From<PostJob>().Where(x => x.CompletedDate != null && x.Error != null));
var restoreFailedJobs = request.RestoreFailedJobs == true;

foreach (var lostJob in lostJobs)
{
Expand All @@ -58,6 +59,11 @@ public object Any(RestoreModelQueues request)
{
workerQueues.Enqueue(missingJob);
}
foreach (var failedJob in failedJobs)
{
workerQueues.Enqueue(failedJob);
}
var failedSuffix = restoreFailedJobs ? ", restored!" : "";

return new StringsResponse
{
Expand All @@ -66,7 +72,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",
$"{failedJobs.Count} failed jobs{failedSuffix}",
]
};
}
Expand Down
5 changes: 4 additions & 1 deletion MyApp.ServiceModel/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ public class ViewModelQueuesResponse
}

[ValidateHasRole(Roles.Moderator)]
public class RestoreModelQueues : IGet, IReturn<StringsResponse> {}
public class RestoreModelQueues : IGet, IReturn<StringsResponse>
{
public bool? RestoreFailedJobs { get; set; }
}

[ValidateHasRole(Roles.Moderator)]
public class FailJob : IPost, IReturnVoid
Expand Down

0 comments on commit 28aeccd

Please sign in to comment.