/// <summary>
        ///     在Job执行前调用
        ///     Called by the Quartz.IScheduler when a Quartz.IJobDetail is about to be executed
        ///     (an associated Quartz.ITrigger has occurred).
        ///     This method will not be invoked if the execution of the Job was vetoed by a Quartz.ITriggerListener.
        /// </summary>
        /// <param name="context">
        ///     The Quartz.IJobExecutionContext that will be passed to the
        ///     Quartz.IJob'sQuartz.IJob.Execute(Quartz.IJobExecutionContext) method.
        /// </param>
        /// <param name="cancellationToken"> The cancellation instruction.</param>
        /// <returns></returns>
        public virtual async Task JobToBeExecuted(IJobExecutionContext context,
                                                  CancellationToken cancellationToken = default)
        {
            Debug(nameof(IJobListener), nameof(JobToBeExecuted));
            try
            {
                var jobName   = context.GetJobName();
                var jobCode   = context.GetJobCode();
                var autoClose = context.GetAutoClose();
                var runParams = context.GetJobRunParams();

                var msg = $"{JobInfo} begin time:{DateTime.Now:yyyy-MM-dd HH:mm:sss}, autoClose:{autoClose}, runParams:{runParams}";
                JobLogHelper.Info(msg, nameof(JobToBeExecuted));

                // 检查依赖选项是否满足
                //  var isOnceJob = JobContextFunc.IsOnceJob(context);
                //  var depJobs = JobContextFunc.GetDepJobs(context);
                // var isContinueRun = (isOnceJob || string.IsNullOrEmpty(depJobs)) ? true : WaitJobCompleted(origJobName, depJobs.Split(',').ToList(), DateTime.Now.Date);
                //JobContextFunc.SetContinueRunFlag(context.JobDetail, isContinueRun);


                //记录任务开始执行
                // var jobParam = GetJobMeta(JobContextFunc.GetOrigJobName(context));

                var ctrl = Ioc.GetService <IScheduleOrderCtrl>();
                if (ctrl != null)
                {
                    await ctrl.StartJobSafety(JobId, jobName, jobCode, runParams);
                }

                /*
                 *
                 * //TODO do job run log
                 * ScheduleOrderCtrl ctrl = new ScheduleOrderCtrl();
                 * var jobId = ctrl.StartJobSafety(jobName, origJobName, runParams);
                 * JobContextFunc.SetJobDbId(context.JobDetail, jobId.ToString());
                 */

                //remove to TriggerFired do this.
                // JobContextFunc.SetJobDbId(context.JobDetail, GuidHelper.GetGuid());
            }
            catch (Exception ex)
            {
                JobLogHelper.Error($"{JobInfo}:JobToBeExecuted Error {ex}", ex, nameof(JobToBeExecuted));
            }

            // return TaskUtil.CompletedTask;
            await Task.CompletedTask;
        }
Beispiel #2
0
        public virtual async Task Execute(IJobExecutionContext context)
        {
            JobId   = context.GetJobDbId();
            JobCode = context.GetJobCode();
            JobName = context.GetJobName();
            JobContext.SetCurrentJobBaseInfo(JobId, JobCode, JobName);

            var job   = context.JobDetail;
            var state = JobBusinessStateEnum.Processing;

            job.SetJobBusinessState(state);
            var rlt = ExecuteWithResult(context);

            state = await rlt ? JobBusinessStateEnum.Success : JobBusinessStateEnum.Fail;
            job.SetJobBusinessState(state);
        }
        /// <summary>
        ///     在Trigger 触发后,Job 将要被执行时由 Scheduler 调用这个方法。TriggerListener 给了一个选择去否决 Job 的执行。假如这个方法返回 true,这个 Job 将不会为此次 Trigger
        ///     触发而得到执行
        ///     Called by the Quartz.IScheduler when a Quartz.ITrigger has fired, and it's associated
        ///     Quartz.IJobDetail is about to be executed.
        ///     It is called after the
        ///     Quartz.ITriggerListener.TriggerFired(Quartz.ITrigger,Quartz.IJobExecutionContext,System.Threading.CancellationToken)
        ///     method of this interface. If the implementation vetoes the execution (via returning
        ///     true), the job's execute method will not be called.
        /// </summary>
        /// <param name="trigger"> The Quartz.ITrigger that has fired.</param>
        /// <param name="context">
        ///     The Quartz.IJobExecutionContext that will be passed to the
        ///     Quartz.IJob'sQuartz.IJob.Execute(Quartz.IJobExecutionContext) method.
        /// </param>
        /// <param name="cancellationToken"> The cancellation instruction.</param>
        /// <returns> Returns true if job execution should be vetoed, false otherwise. </returns>
        public virtual async Task <bool> VetoJobExecution(ITrigger trigger, IJobExecutionContext context,
                                                          CancellationToken cancellationToken = default)
        {
            Debug(nameof(ITriggerListener), nameof(VetoJobExecution));
            var origJobName = context.GetJobCode();
            //检查依赖选项是否满足
            var isOnceJob     = context.IsOnceJob();
            var depJobs       = context.GetDepJobs(); //jobcodes
            var isContinueRun = isOnceJob || string.IsNullOrEmpty(depJobs) || await WaitJobCompleted(origJobName, depJobs.Split(',').ToList(), DateTime.Now.Date);

            context.JobDetail.SetContinueRunFlag(isContinueRun);

            if (!isContinueRun)
            {
                Debug(nameof(ITriggerListener), origJobName + "未满足运行条件");
                // Do Notice
                await DoNoticeAsync($"前置job:{depJobs} 未全部完成! ");

                return(await Task.FromResult(true));
            }

            return(await Task.FromResult(false));
        }