Esempio n. 1
0
        public async Task <SchedulerStatus> GetStatus(CancellationToken cancellationToken = default)
        {
            var result = new SchedulerStatus
            {
                ServerTime = DateTime.Now,
                Jobs       = new List <SchedulerJobStatus>()
            };

            if (failedToLoad)
            {
                result.SchedulerState = SchedulerStatus.State.FailedToLoad;
                return(result);
            }

            if (quartzScheduler.IsStarted)
            {
                result.SchedulerState = SchedulerStatus.State.Active;
            }
            if (quartzScheduler.InStandbyMode)
            {
                result.SchedulerState = SchedulerStatus.State.StandBy;
            }

            var jobKeys = await quartzScheduler.GetJobKeys(GroupMatcher <JobKey> .AnyGroup());

            foreach (var key in jobKeys)
            {
                var jobDetail = await quartzScheduler.GetJobDetail(key, cancellationToken);

                var triggers = await quartzScheduler.GetTriggersOfJob(key, cancellationToken);

                var trigger = triggers.FirstOrDefault();

                var internalDetail = internalDetails.First(dt => dt.JobKey.Equals(jobDetail.Key));
                var name           = internalDetail.Name;
                var cronExpression = internalDetail.CronExpression;

                var triggerActive = trigger != null &&
                                    (await quartzScheduler.GetTriggerState(trigger.Key, cancellationToken) == TriggerState.Normal);

                var jobStatus = new SchedulerJobStatus
                {
                    Name             = name,
                    Active           = triggerActive,
                    JobType          = jobDetail.JobType.Name,
                    CronExpression   = cronExpression,
                    PreviousFireTime = trigger?.GetPreviousFireTimeUtc()?.ToLocalTime().DateTime,
                    NextFireTime     = trigger?.GetNextFireTimeUtc()?.ToLocalTime().DateTime
                };
                if (!String.IsNullOrEmpty(jobStatus.CronExpression))
                {
                    jobStatus.CronExpressionDescription = ExpressionDescriptor
                                                          .GetDescription(jobStatus.CronExpression)
                                                          .ToLowerFirstChar();
                }

                if (internalDetail.LastExecutionException != null)
                {
                    var all = internalDetail
                              .LastExecutionException
                              .GetAllMessages();
                    jobStatus.FailureMessage = String.Join(Environment.NewLine, all);
                }

                result.Jobs.Add(jobStatus);
            }
            result.Jobs.Sort((job1, job2) => job1.Name.CompareTo(job2.Name));

            return(result);
        }
Esempio n. 2
0
        public async Task <SchedulerStatus> GetStatus(CancellationToken cancellationToken = default)
        {
            var result = new SchedulerStatus
            {
                ServerTime = DateTime.Now,
                Jobs       = new List <SchedulerJobStatus>()
            };

            if (_failedToLoad)
            {
                result.SchedulerState = SchedulerStatus.State.FailedToLoad;
                return(result);
            }

            if (_quartzScheduler.IsStarted)
            {
                result.SchedulerState = SchedulerStatus.State.Active;
            }
            if (_quartzScheduler.InStandbyMode)
            {
                result.SchedulerState = SchedulerStatus.State.StandBy;
            }

            var jobKeys = await _quartzScheduler.GetJobKeys(GroupMatcher <JobKey> .AnyGroup(), cancellationToken);

            var lastExecutions = _historyManager.GetLastExecutions(_internalDetails.Select(i => i.Name)).ToList();

            foreach (var key in jobKeys)
            {
                var jobDetail = await _quartzScheduler.GetJobDetail(key, cancellationToken);

                if (jobDetail == null)
                {
                    continue;
                }

                var triggers = await _quartzScheduler.GetTriggersOfJob(key, cancellationToken);

                var trigger = triggers.FirstOrDefault();

                var internalDetail   = _internalDetails.First(dt => dt.JobKey.Equals(jobDetail.Key));
                var executionDetails = lastExecutions.FirstOrDefault(d => d.JobName == internalDetail.Name);

                var cronExpression = internalDetail.CronExpression;

                var triggerActive = trigger != null &&
                                    (await _quartzScheduler.GetTriggerState(trigger.Key, cancellationToken) == TriggerState.Normal);

                var jobStatus = new SchedulerJobStatus
                {
                    Name             = internalDetail.Name,
                    Active           = triggerActive,
                    RefireIfMissed   = internalDetail.RefireIfMissed,
                    JobType          = jobDetail.JobType.FullName,
                    CronExpression   = cronExpression,
                    PreviousFireTime = executionDetails?.ExecutedAt,
                    NextFireTime     = trigger?.GetNextFireTimeUtc()?.ToLocalTime().DateTime,
                    JobParameters    = jobDetail
                                       .JobDataMap
                                       .ToDictionary(p => p.Key, p => p.Value.ToString()),
                    LastExecutionFailed  = executionDetails?.ExecutionFailed ?? false,
                    LastExecutionMessage = executionDetails?.ExecutionMessage ?? "-- N/A --"
                };
                if (!String.IsNullOrEmpty(jobStatus.CronExpression))
                {
                    jobStatus.CronExpressionDescription = ExpressionDescriptor
                                                          .GetDescription(jobStatus.CronExpression, new Options {
                        Use24HourTimeFormat = true
                    })
                                                          .ToLowerFirstChar();
                }

                result.Jobs.Add(jobStatus);
            }
            result.Jobs.Sort((job1, job2) => String.Compare(job1.Name, job2.Name, StringComparison.Ordinal));

            return(result);
        }