Improve workers for backtests

This commit is contained in:
2025-11-10 01:44:33 +07:00
parent 97f2b8229b
commit 7e52b7a734
18 changed files with 740 additions and 144 deletions

View File

@@ -250,5 +250,58 @@ public class JobService
throw;
}
}
/// <summary>
/// Retries a failed or cancelled job by resetting it to Pending status.
/// </summary>
/// <param name="jobId">The job ID to retry</param>
/// <returns>The updated job</returns>
/// <exception cref="InvalidOperationException">Thrown if job cannot be retried</exception>
public async Task<Job> RetryJobAsync(Guid jobId)
{
var job = await _jobRepository.GetByIdAsync(jobId);
if (job == null)
{
throw new InvalidOperationException($"Job with ID {jobId} not found.");
}
// Only allow retrying Failed or Cancelled jobs
// Running jobs should be handled by stale job recovery, not manual retry
if (job.Status != JobStatus.Failed && job.Status != JobStatus.Cancelled)
{
throw new InvalidOperationException($"Cannot retry job with status {job.Status}. Only Failed or Cancelled jobs can be retried.");
}
// Reset job to pending state
job.Status = JobStatus.Pending;
job.AssignedWorkerId = null;
job.LastHeartbeat = null;
job.StartedAt = null;
job.CompletedAt = null;
job.ProgressPercentage = 0;
job.RetryAfter = null;
// Keep ErrorMessage for reference, but clear it on next run
// Keep RetryCount to track total retries
// Reset IsRetryable to true
job.IsRetryable = true;
await _jobRepository.UpdateAsync(job);
_logger.LogInformation("Job {JobId} reset to Pending status for retry", jobId);
return job;
}
/// <summary>
/// Deletes a job from the database.
/// </summary>
/// <param name="jobId">The job ID to delete</param>
/// <exception cref="InvalidOperationException">Thrown if job cannot be found</exception>
public async Task DeleteJobAsync(Guid jobId)
{
await _jobRepository.DeleteAsync(jobId);
_logger.LogInformation("Deleted job {JobId}", jobId);
}
}