Azure WebJob doesn't work as expected when async

I am starting to use WebJobs to handle some background tasks and am having trouble handling errors and retrying. It looks like if any of my functions are asynchronous, then the function always reports success in the control panel, although I am making an exception inside that function.

Consider the following simple example ->

    public static void AlwaysFail([QueueTrigger("alwaysfail")] string message)
    {
        throw new Exception("Induced Error");
    }

      

The above code behaves as I expect. The message is unloaded from the queue, an exception is thrown, the error message is not displayed in the WebJobs toolbar, a message is required. This happens 5 times and then the message is stored in the poison queue.

But if I try to do a similar function, but make it asynchronous like below ->

    public async static void AlwaysFail([QueueTrigger("alwaysfail")] string message)
    {
        throw new Exception("Induced Error");
    }

      

Results are not expected. The message slips out of the queue, the exception is thrown, but the report panel reports success and nothing is repeated. Also, in these cases, it seems that although the dashboard reports success, the exception causes the process to crash with an exit code from this world waiting for 60 seconds and restart as shown in the following log ->

[11/11/2014 06:13:35> 0dc4f9: INFO] Execution: 'Functions.AlwaysFail' because a new queue message was found in 'alwaysfail'. [11/11/2014 06:13:36> 0dc4f9: ERR] [11/11/2014 06:13:36> 0dc4f9: ERR] Unhandled Exception: System.InvalidOperationException: Error thrown [11/11/2014 06:13 : 36> 0dc4f9: ERR] in Plmtc.BackgroundWorker.Functions.d__2.MoveNext () [11/11/2014 06:13:36> 0dc4f9: ERR] --- End of the stack trace from the previous location where the exception was thrown - - [11/11/2014 06:13:36> 0dc4f9: ERR] in System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__5 (object state) [11/11/2014 06:13:36> 0dc4f9: ERR] in System .Threading.QueueUserWorkItemCallback.WaitCallback_Context (object state) [11/11/2014 06:13:36> 0dc4f9: ERR] at System.Threading.ExecutionContext.RunInternal (ExecutionContext executeContext, ContextCallback callback, object state, Boolean preserveSyncCtx) [11/11/2014 06:13:36> 0dc4f9: ERR] at System.Threading.ExecutionContext.Run (ExecutionContext executeContext, ContextCallback callback, object state, Boolean preserveSyncCtx) [11/11/2014 06:13:36> 0dc4f9: ERR] at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [11/11/2014 06:13:36> 0Rdc4f ] at System.Threading.ThreadPoolWorkQueue.Dispatch () [11/11/2014 06:13:36> 0dc4f9: ERR] at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [11/11/2014 06:13:36> 0dc4f9 : SYS ERR] Failed to execute work due to exit code -532462766 [11/11/2014 06:13:36> 0dc4f9: SYS INFO] Process dropped, waiting 60 seconds [11/11/2014 06:13:36> 0dc4f9:SYS INFO] Status changed to PendingRestart

The only way I can get the asynchronous function to fail in the control panel is to write one that takes the POCO class as a parameter, but only includes a simple string in the message. In these cases, the failure occurs during message / parameter binding before reaching any of the function code.

Anyone successfully using async functions to respond to queue triggers without these issues?

+3


source to share


1 answer


Because the method async void

, the WebJobs SDK cannot wait for the method to complete. Change it to return a Task

...

public async static Task AlwaysFail([QueueTrigger("alwaysfail")] string message)
{
    throw new Exception("Induced Error");
}

      



See this post for details .

+9


source







All Articles