Azure - how do I run a job that calls a function in the webservice every hour?

前端 未结 3 1309
孤独总比滥情好
孤独总比滥情好 2020-12-18 14:23

Im using Microsoft Azure and have a webservice and a SQL Azure db, I want to run a function every hour but not sure how to go about doing this? I assume it has something to

3条回答
  •  独厮守ぢ
    2020-12-18 15:27

    In the Run() method of either a Web Role or Worker Role, you could kick off a thread that sleeps until the top of the hour, wakes up, performs whatever task(s) you want, and goes back to sleep. Just remember that, when having multiple instances of a Web or Worker Role that's doing scheduling, you need to make sure only one of those instances is actually doing the scheduling. One way to accomplish that is to try leasing a blob, prior to starting the scheduler thread. If you lock it, go for it. If not, just recheck periodically. Eventually the instance that obtained the lock will release it when its instance recycles (which should happen at least once monthly).

    Alternatively, you could place messages on a queue with visibilitytimeout set to a specific # of seconds correlating to some hourly time period. Then, each of your Web or Worker instances can periodically poll the queue for tasks to work on. The messages you push into the queue won't be visible to queue-readers until the visibility timeout period is reached.

提交回复
热议问题