예제 #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;
 }