Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当不符合运行时条件时,将Quartz作业延迟一段时间,但仍保留旧的计划程序?_C#_Quartz - Fatal编程技术网

C# 当不符合运行时条件时,将Quartz作业延迟一段时间,但仍保留旧的计划程序?

C# 当不符合运行时条件时,将Quartz作业延迟一段时间,但仍保留旧的计划程序?,c#,quartz,C#,Quartz,我正在寻找解决Quartz作业的方法 我有一个Quartz作业是由我的调度程序执行的,它被定义为Cron表达式。但是,在这个工作中,我必须检查运行时条件。如果不匹配,作业将延迟启动约2小时,然后保持旧调度程序正常 可能吗 public void Execute(IJobExecutionContext context) { if (_isMaintenanceSystem) { // Delay job // When delay, job fir

我正在寻找解决
Quartz作业的方法

我有一个Quartz作业是由我的调度程序执行的,它被定义为Cron表达式。但是,在这个工作中,我必须检查运行时条件。如果不匹配,作业将延迟启动约2小时,然后保持旧调度程序正常

可能吗

public void Execute(IJobExecutionContext context)
{
    if (_isMaintenanceSystem)
    {
        // Delay job
        // When delay, job fires and keep old scheduler as normal.
    }

    SendMail(_emailSetting, fileAttachment);
}

是。您可以通过执行两个步骤来完成:

第一步。像这样修改你的主要工作

因此,它将遵循
单一责任原则
。它意味着做一件事

public void Execute(IJobExecutionContext context)
{
    if (_isMaintenanceSystem)
    {
        _logger.Info($"Start UM checking");
        context.Scheduler.PauseJob(context.Trigger.JobKey);
        
        // You would schedule a task named CheckUMJob
        var job = JobBuilder.Create<CheckUMJob>()
            .WithIdentity("CheckUMJob", "CheckUMJob_schedule")
            .Build();
        var trigger = TriggerBuilder.Create()
            .WithIdentity("CheckUMJob", "CheckUMJob_schedule")
            .WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever(15))
            .StartNow()
            .Build();
        context.Scheduler.ScheduleJob(job, trigger);
     }

    // Do your task here as normally
    SendMail(_emailSetting, fileAttachment);
}
public class CheckUMJob : IJob
{
    private readonly ILogger _logger;

    public CheckUMJob(ILogger loggere)
    {
        _logger = logger;
    }

    public void Execute(IJobExecutionContext context)
    {
        if (!_isMaintenanceSystem)
        {
            _logger.Info("End of maintenance time. Resume the main job");

            context.Scheduler.DeleteJob(context.Trigger.JobKey);
            context.Scheduler.ResumeAll();
        }
    }
}