Azure WebJob/Scheduler every 30 minutes from 8am-6

2019-02-25 05:34发布

When I go to configure a Schedule in the Azure management console, I'm only given the option of scheduling with an absolute end date/time (or never ending) and an interval.

enter image description here

So I can't, from this UI, schedule a job to every 30 minutes run every day from 8:00 AM to 6:00 PM only (i.e. don't run from 6:01 PM to 7:59 AM). Windows Task Manager and all other schedulers (cron, quartz) I've used before support the behaviour I want.

Is type of schedule supported at all in Azure, e.g. through the API or a hackish use of the Portal HTTP/JSON interfaces? I don't mind "hacking" the schedule once - it would beat embedding the schedule into the actual job script/application.

2条回答
Juvenile、少年°
2楼-- · 2019-02-25 05:55

You can use the built-in scheduling which is more flexible than the Azure one. You can learn more about how that works from this blog post http://blog.amitapple.com/post/2015/06/scheduling-azure-webjobs/

The summary: create a file called settings.job that contains the following piece of json

{"schedule": "cron expression for the schedule"}

in your case the cron expression for "every 30 minutes from 8am to 6pm" would be 0,30 8-18 * * *

so the JSON you want is

{"schedule": "0,30 8-18 * * *"}

Keep in mind that this uses the timezone of the machine, which is UTC by default.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-25 06:19

This is something you need to implement in your WebJob. I have a similar issue in that I have WebJobs with complex schedules. Fortunately it isn't hard to implement.

This snippit gets your local time (Eastern from what I can tell) from UTC which everything is Azure is set to. It then checks if it is Saturday or Sunday and if it is exits out (not sure if you need this). It then checks whether it is before 8AM or after 6PM and if it is exits out. If it passes both those conditions the WebJob runs.

        //Get current time, adjust 4 hours to convert UTC to Eastern Time
        DateTime dt = DateTime.Now.AddHours(-4);

        //This job should only run Monday - Friday from 8am to 6pm Eastern Time.
        if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday) return;
        if (dt.Hour < 8 || dt.Hour > 16) return;

        //Go run WebJob

Hope this helps.

查看更多
登录 后发表回答