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

主宰稳场 提交于 2019-12-31 01:50:14

问题


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.

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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/31754145/azure-webjob-scheduler-every-30-minutes-from-8am-6pm

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