Exemple #1
0
 public JobLog Post([FromBody] JobLog jobLog)
 {
     if (ModelState.IsValid)
     {
         jobLog = _jobLogs.AddJobLog(jobLog);
         _logger.Log(LogLevel.Information, this, LogFunction.Create, "Job Log Added {JobLog}", jobLog);
     }
     return(jobLog);
 }
Exemple #2
0
        protected async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await Task.Yield(); // required so that this method does not block startup

            try
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    using (var scope = _serviceScopeFactory.CreateScope())
                    {
                        // get name of job
                        string jobType = Utilities.GetFullTypeName(GetType().AssemblyQualifiedName);

                        // load jobs and find current job
                        IJobRepository jobs = scope.ServiceProvider.GetRequiredService <IJobRepository>();
                        Job            job  = jobs.GetJobs().Where(item => item.JobType == jobType).FirstOrDefault();
                        if (job != null && job.IsEnabled && !job.IsExecuting)
                        {
                            // get next execution date
                            DateTime NextExecution;
                            if (job.NextExecution == null)
                            {
                                if (job.StartDate != null)
                                {
                                    NextExecution = job.StartDate.Value;
                                }
                                else
                                {
                                    NextExecution = DateTime.UtcNow;
                                }
                            }
                            else
                            {
                                NextExecution = job.NextExecution.Value;
                            }

                            // determine if the job should be run
                            if (NextExecution <= DateTime.UtcNow && (job.EndDate == null || job.EndDate >= DateTime.UtcNow))
                            {
                                IJobLogRepository jobLogs = scope.ServiceProvider.GetRequiredService <IJobLogRepository>();

                                // create a job log entry
                                JobLog log = new JobLog();
                                log.JobId      = job.JobId;
                                log.StartDate  = DateTime.UtcNow;
                                log.FinishDate = null;
                                log.Succeeded  = false;
                                log.Notes      = "";
                                log            = jobLogs.AddJobLog(log);

                                // update the job to indicate it is running
                                job.IsExecuting = true;
                                jobs.UpdateJob(job);

                                // execute the job
                                try
                                {
                                    log.Notes     = ExecuteJob(scope.ServiceProvider);
                                    log.Succeeded = true;
                                }
                                catch (Exception ex)
                                {
                                    log.Notes     = ex.Message;
                                    log.Succeeded = false;
                                }

                                // update the job log
                                log.FinishDate = DateTime.UtcNow;
                                jobLogs.UpdateJobLog(log);

                                // update the job
                                job.NextExecution = CalculateNextExecution(NextExecution, job.Frequency, job.Interval);
                                job.IsExecuting   = false;
                                jobs.UpdateJob(job);

                                // trim the job log
                                List <JobLog> logs = jobLogs.GetJobLogs().Where(item => item.JobId == job.JobId)
                                                     .OrderByDescending(item => item.JobLogId).ToList();
                                for (int i = logs.Count; i > job.RetentionHistory; i--)
                                {
                                    jobLogs.DeleteJobLog(logs[i - 1].JobLogId);
                                }
                            }
                        }
                    }

                    // wait 1 minute
                    await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
                }
            }
            catch
            {
                // can occur during the initial installation as there is no DBContext
            }
        }