4

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?

mathewc
  • 13,312
  • 2
  • 45
  • 53
WPFUser
  • 403
  • 4
  • 16
  • Was the accepted answer your issue? I'm seeing the same exact thing, except my instance count is set to 1... This is for a webjob which has been working as expected for many months. – Shaun Rowan Feb 22 '17 at 01:58

2 Answers2

5

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.

user1859853
  • 66
  • 1
  • 3
1

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.

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • 1
    Agreed. The above is not a pattern you want to use. It would result in a single function invocation in the Dashboard with an ever growing amount of log output, that never completes. Instead, do what David suggests if you're not using any other features of the WebJobs SDK, or you could use the SDK TimerTrigger which lets you run recurring work like this (samples [here](https://github.com/Azure/azure-webjobs-sdk-extensions/blob/master/src/ExtensionsSample/Samples/TimerSamples.cs)). – mathewc Feb 07 '16 at 16:44
  • Great input. I did plan to run the job more frequently than once a minute which afaik excludes cron, though the TimerTrigger might be more of what I'm looking for. Thanks for the input guys. – WPFUser Feb 08 '16 at 15:07
  • cron can actually go up to once a second. However, it does have a limitation that if the job takes longer than the interval between invocations (e.g. it runs every 20 seconds and takes 30 second to run), some invocations can be missed. – David Ebbo Feb 08 '16 at 18:57