Scheduled Azure WebJob but NoAutomaticTrigger-Method not invoked

Robert Muehsig picture Robert Muehsig · Jun 30, 2014 · Viewed 8.4k times · Source

This question is related to this one: Running Azure WebJob without queue

My scenario: I just want to write a file each hour into the blob storage and don't need any queue system. From the former question I got this code - which worked fine the first time it is triggered:

    [NoAutomaticTrigger]
    public static void DoWork([Blob("container/foobar.txt")] TextWriter writer)
    {
        writer.Write("Hello World " + DateTime.Now.ToShortTimeString())"
    }

    static void Main()
    {
        JobHost host = new JobHost();
        host.Call(typeof(Program).GetMethod("DoWork"));

        host.RunAndBlock();
    }

The website is running with "AlwaysOn=true" and the webjob is "Running" all the time but now the scheduler throws following error - and nothing happens: "Cannot start a new run since job is already running."

The current result is that the file is only written once. If I switch "AlwaysOn" to false it "works", but it seems dirty because without the Always on the job result is "Aborted".

Answer

Victor Hurdugaci picture Victor Hurdugaci · Jun 30, 2014

A job marked as "on demand" must complete. In your case, the problem is the RunAndBlock call at the end. If you call that method, the console app never stops (that's why the job "is already running") because RunAndBlock is basically a while(true) loop.

RunAndBlock should be used only for continuous jobs.