コード例 #1
0
 protected virtual void OnStepExit(ProcessingContext context)
 {
 }
コード例 #2
0
        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);
                    }
                }
            }
        }
コード例 #3
0
 protected virtual CancellationToken GetTokenToWaitOn(ProcessingContext context)
 {
     return(context.CancellationToken);
 }
コード例 #4
0
 private CancellationTokenSource CreateLinked(ProcessingContext context)
 {
     return(CancellationTokenSource.CreateLinkedTokenSource(
                context.CancellationToken,
                _cts.Token));
 }
コード例 #5
0
 protected override CancellationToken GetTokenToWaitOn(ProcessingContext context)
 {
     return(_linkedCts.Token);
 }
コード例 #6
0
 protected override void OnStepExit(ProcessingContext context)
 {
     context.Pulsed -= HandlePulse;
     _linkedCts.Dispose();
     _cts.Dispose();
 }
コード例 #7
0
 protected override void OnStepEnter(ProcessingContext context)
 {
     _cts            = new CancellationTokenSource();
     _linkedCts      = CreateLinked(context);
     context.Pulsed += HandlePulse;
 }
コード例 #8
0
 private ProcessingContext(ProcessingContext other)
 {
     Provider          = other.Provider;
     Storage           = other.Storage;
     CancellationToken = other.CancellationToken;
 }