Why does my continuous azure webjob run the function twice?

时间秒杀一切 提交于 2021-02-07 18:44:30

问题


I have created my first azure webjob that runs continously;

I'm not so sure this is a code issue, but for the sake of completeness here is my code:

static void Main()
{
   var host = new JobHost();
   host.CallAsync(typeof(Functions).GetMethod("ProcessMethod"));
   host.RunAndBlock();
}

And for the function:

[NoAutomaticTrigger]
public static async Task ProcessMethod(TextWriter log)
{
   log.WriteLine(DateTime.UtcNow.ToShortTimeString() + ": Started");
   while (true)
   {
       Task.Run(() => RunAllAsync(log));
       await Task.Delay(TimeSpan.FromSeconds(60));
   }
   log.WriteLine(DateTime.UtcNow.ToShortTimeString() + "Shutting down..");
}

Note that the async task fires off a task of its own. This was to ensure they were started quite accurately with the same interval. The job itself is to download an url, parse and input some data in a db, but that shouldnt be relevant for the multiple instance issue I am experiencing.

My problem is that once this has been running for roughly 5 minutes a second ProcessMethod is called which makes me have two sessions simoultaniously doing the same thing. The second method says it is "started from Dashboard" even though I am 100% confident I did not click anything to start it off myself.

Anyone experienced anything like it?


回答1:


Change the instance count to 1 from Scale tab of WebApp in Azure portal. By default it is set to 2 instances which is causing it to run two times.




回答2:


I can't explain why it's getting called twice, but I think you'd be better served with a triggered job using a CRON schedule (https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#CreateScheduledCRON), instead of a Continuous WebJob.

Also, it doesn't seem like you are using the WebJobs SDK, so you can completely skip that. Your WebJob can be as simple as a Main that directly does the work. No JobHost, no async, and generally easier to get right.



来源:https://stackoverflow.com/questions/35247523/why-does-my-continuous-azure-webjob-run-the-function-twice

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