Run a job every X days but only between two specific dates and times

五迷三道 提交于 2020-01-06 08:46:10

问题


I think the title says it all.

I would like to run a job that e.g. Starts at Jun 19 2014 (say at 7 AM), ends at December 25 2015 (say at 11 PM) and runs every 9 days in between these two dates. I can set it up to work without an end date. But I don't know how to include all of it in one expression.

Update: Does adding an EndAt() to my TriggerBuilder work?

mytrigger = (ICronTrigger)TriggerBuilder.Create()
    .WithIdentity(triggerName, triggerGroup)
    .WithCronSchedule(cron)
    .EndAt(xxxx)
    .Build();

回答1:


You're in the right direction, schedules that need a lot of research to be generated with a cron expression can be easily generated via the API. For example, the trigger you want is the following:

    var startDate = new DateTime(2014, 06, 19, 7, 0, 0);
    var endDate = new DateTime(2015, 12, 25, 23, 0, 0);
    var cronExpression = "0 0 12 1/9 * ? *"; //every nine days

    ITrigger trig = TriggerBuilder.Create()
        .StartAt(startDate)
        .WithCronSchedule(cronExpression)
        .WithDescription("description")
        .WithIdentity(triggerKey)
        .WithPriority(1)
        .EndAt(endDate)
        .Build();

If you want to see the cron expression generated:

    ICronTrigger trigger = (ICronTrigger)trig;
    string cronExpression = trigger.CronExpressionString;


来源:https://stackoverflow.com/questions/26591225/run-a-job-every-x-days-but-only-between-two-specific-dates-and-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!