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

后端 未结 2 1574
梦如初夏
梦如初夏 2021-01-21 02:48

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.

相关标签:
2条回答
  • 2021-01-21 03:21

    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.

    0 讨论(0)
  • 2021-01-21 03:36

    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.

    0 讨论(0)
提交回复
热议问题