Skip to content

Commit

Permalink
fix(jobs): fix schedule fetch jobs time
Browse files Browse the repository at this point in the history
  • Loading branch information
music-soul1-1 committed Sep 26, 2024
1 parent f73a484 commit d51ccd3
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions NureTimetableAPI/Controllers/V2/LessonsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public async Task<IActionResult> GetLessons(
var lastJob = JobStorage.Current.GetConnection().GetRecurringJobs().ToList()
.OrderByDescending(j => j.Cron).FirstOrDefault(j => !j.Id.Contains("keep-alive"));

if (fetchJob == null || lessons == null)
if (fetchJob == null || lessons?.Count == 0)
{
if (lastJob != null && lastJob.LastExecution != null && lastJob.LastExecution > DateTime.Now.AddSeconds(30))
if (lastJob?.LastExecution > DateTime.Now.AddSeconds(30))
{
throw new Exception($"Please wait {lastJob.LastExecution.Value.Second} seconds, and then try again");
throw new Exception($"Please wait 30 seconds, and then try again");
}
var cistLessons = await _cistRepository.GetCistScheduleAsync(id, type);

Expand All @@ -60,13 +60,12 @@ public async Task<IActionResult> GetLessons(
}

lessons = await _sqlRepository.FetchSchedule(id, cistLessons, type, startTime, endTime);
var jobExecutionTime = lastJob?.NextExecution != null ? lastJob.NextExecution.Value.AddMinutes(1) : DateTime.Now;

RecurringJob.AddOrUpdate<ScheduleFetch>(
$"update-{type.ToString().ToLower()}-with-id-{id}",
job => job.Execute(id, type),
(lastJob != null && lastJob.NextExecution != null) ?
Cron.Daily(lastJob.NextExecution.Value.Hour, lastJob.NextExecution.Value.Minute + 1) :
Cron.Daily(DateTime.Now.Hour, DateTime.Now.Minute)
Cron.Daily(jobExecutionTime.Hour, jobExecutionTime.Minute)
);

return Ok(lessons);
Expand Down

0 comments on commit d51ccd3

Please sign in to comment.