private async Task WaitUntilNextJobOrLeaseRenewal(DateTimeOffset leaseRenewalTime, CancellationToken cancellationToken) { SampleDataJob nextJob = _jobs[0]; DateTimeOffset waitUntil = leaseRenewalTime <= nextJob.NextRunTime ? leaseRenewalTime : nextJob.NextRunTime; TimeSpan delay = waitUntil - DateTimeOffset.UtcNow; if (delay <= TimeSpan.Zero) { return; } await Task.Delay(delay, cancellationToken).ConfigureAwait(false); }
/// <summary> /// Runs the next available job (if any) and returns the run time of the job that will run next. /// </summary> private async Task RunNextJob() { // Run the first job in the list SampleDataJob job = _jobs[0]; await job.Run().ConfigureAwait(false); // Remove it from the beginning of the list and re-insert it in the correct position based on its new NextRunTime _jobs.RemoveAt(0); int index = _jobs.BinarySearch(job, JobNextRunTimeComparer.Instance); if (index < 0) { index = ~index; } _jobs.Insert(index, job); }