Beispiel #1
0
        public async Task ProcessCoreAsync(ProcessingContext context)
        {
            while (!context.IsStopping)
            {
                OnStepEnter(context);
                try
                {
                    await Step(context);

                    context.ThrowIfStopping();

                    var token = GetTokenToWaitOn(context);
                    await token.WaitHandle.WaitOneAsync(_pollingDelay);
                }
                finally
                {
                    OnStepExit(context);
                }
            }
        }
Beispiel #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);
                    }
                }
            }
        }