10

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".

1 Answer 1

17

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.

3
  • Thanks - works like a charm. I took the "RunAndBlock" from this code example - maybe it should be removed there as well: aspnet.codeplex.com/SourceControl/latest#Samples/AzureWebJobs/… Jul 2, 2014 at 6:08
  • 1
    Hm... I have to agree that the sample might be confusing. The idea was to deploy that job as continuous, not on demand. I'll see if I can rewrite it. Thanks for letting me know Jul 2, 2014 at 15:57
  • 8
    Should i use host.Start() method to manually trigger a webjob ?
    – ManirajSS
    Nov 24, 2015 at 9:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.