Beispiel #1
0
        private async Task Step(ProcessingContext context)
        {
            using (var connection = context.Storage.GetConnection())
            {
                var fetched = default(IFetchedJob);
                while (
                    !context.IsStopping &&
                    (fetched = await FetchNextJobCoreAsync(connection)) != null)
                {
                    using (fetched)
                        using (var scopedContext = context.CreateScope())
                        {
                            var job            = fetched.Job;
                            var invocationData = Helper.FromJson <InvocationData>(job.Data);
                            var method         = invocationData.Deserialize();
                            var factory        = scopedContext.Provider.GetService <IJobFactory>();

                            var instance = default(object);
                            if (!method.Method.IsStatic)
                            {
                                instance = factory.Create(method.Type);
                            }

                            try
                            {
                                var sp     = Stopwatch.StartNew();
                                var result =
                                    method.Method.Invoke(instance, method.Args.ToArray()) as Task;
                                if (result != null)
                                {
                                    await result;
                                }
                                sp.Stop();
                                fetched.RemoveFromQueue();
                                _logger.LogInformation(
                                    "Job executed succesfully. Took: {seconds} secs.",
                                    sp.Elapsed.TotalSeconds);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogWarning(
                                    $"Job failed to execute: '{ex.Message}'. Requeuing.");
                                fetched.Requeue();
                                throw;
                            }
                        }
                }
            }
        }
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);
                    }
                }
            }
        }