protected virtual void OnStepExit(ProcessingContext context) { }
private async Task ProcessCoreAsync(ProcessingContext context) { var storage = context.Storage; var jobs = await GetJobsAsync(storage); if (!jobs.Any()) { _logger.LogInformation( "Couldn't find any cron jobs to schedule, cancelling processing of cron jobs."); throw new OperationCanceledException(); } LogInfoAboutCronJobs(jobs); context.ThrowIfStopping(); var computed = Compute(jobs); while (!context.IsStopping) { var now = DateTime.UtcNow; var nextJob = ElectNextJob(computed); var due = nextJob.Next; var timeSpan = due - now; if (timeSpan.TotalSeconds > 0) { await context.WaitAsync(timeSpan); } context.ThrowIfStopping(); using (var scopedContext = context.CreateScope()) { var factory = scopedContext.Provider.GetService <IJobFactory>(); var job = (IJob)factory.Create(nextJob.JobType); try { var sw = Stopwatch.StartNew(); await job.ExecuteAsync(); sw.Stop(); _logger.LogInformation( "Cron job '{jobName}' executed succesfully. Took: {seconds} secs.", nextJob.Job.Name, sw.Elapsed.TotalSeconds); } catch (Exception ex) { _logger.LogWarning( $"Cron job '{{jobName}}' failed to execute: '{ex.Message}'.", nextJob.Job.Name); } using (var connection = storage.GetConnection()) { now = DateTime.UtcNow; nextJob.Update(now); await connection.UpdateCronJobAsync(nextJob.Job); } } } }
protected virtual CancellationToken GetTokenToWaitOn(ProcessingContext context) { return(context.CancellationToken); }
private CancellationTokenSource CreateLinked(ProcessingContext context) { return(CancellationTokenSource.CreateLinkedTokenSource( context.CancellationToken, _cts.Token)); }
protected override CancellationToken GetTokenToWaitOn(ProcessingContext context) { return(_linkedCts.Token); }
protected override void OnStepExit(ProcessingContext context) { context.Pulsed -= HandlePulse; _linkedCts.Dispose(); _cts.Dispose(); }
protected override void OnStepEnter(ProcessingContext context) { _cts = new CancellationTokenSource(); _linkedCts = CreateLinked(context); context.Pulsed += HandlePulse; }
private ProcessingContext(ProcessingContext other) { Provider = other.Provider; Storage = other.Storage; CancellationToken = other.CancellationToken; }