user1018711
user1018711

Reputation:

Azure Worker role error handling

working with worker roles is a little bit tricky if something goes wrong. I have exception occurring in my worker role, which forces role to quit and restart.

I decided to implement azure diagnostics solution to log what is going on. It works, when i try to write message to trace, but i am unable to catch exceptions and log them. I surround my code with try catch and try to write exception contents to trace.

Is this right approach? And why does it not work? Is there any better way to log exceptions in worker roles? Thank you. My code is:

public class WorkerRole : RoleEntryPoint
    {

    private const int WAIT_INTERVAL_SECONDS = 15;

    public override void Run()
    {
        Trace.WriteLine("$projectname$ entry point called", "Information");

        while (true)
        {

            try
            {
                string id = MainWCFRole.Storage.TrackProcessingQueueDAO.retrieveMsgContents();
                if ((id != null) && (!id.Equals("")))
                {
                    var points = MainWCFRole.Storage.TrackRawDataDAO.retrieve(id);

                    Processor.process(id, points);
                }
                else
                {
                    Thread.Sleep(WAIT_INTERVAL_SECONDS * 1000);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceInformation("Something went wrong");
                Trace.TraceError(ex.ToString());
                throw ex;
            }


        }
    }

    public override bool OnStart()
    {
        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();
        dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
        dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
        dmc.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
        dmc.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);


        DiagnosticMonitor.Start("StorageConnectionString", dmc);

        Trace.TraceInformation("Starting Worker Role TrackProcessor");

        return base.OnStart();
    }

}

Processor.process(id, points) is method which throws exception. More precisely, it is when i try to call SQL azure query. I know that diagnostics works, because when role starts, it calls Trace.TraceInformation("Starting Worker Role TrackProcessor"); and it is visible in the table storage as mesage. Then over time more messages occur, as role encounters exception and is forced to restart. However, no error messages in log.

Upvotes: 2

Views: 4542

Answers (2)

Brian Reischl
Brian Reischl

Reputation: 7356

If the Run() method exits, then your worker is recycled. The worker may not have time to transfer logs to storage before it gets recycled.

The simplest solution is to not re-throw the exception after you catch it. Then your worker role will continue running until it is shut down manually.

Upvotes: 3

Tom
Tom

Reputation: 1611

Did you hook up the Trace Listener for the worker role? Take a look at: http://blogs.msdn.com/b/jimoneil/archive/2010/10/08/azure-home-part-8-worker-role-and-azure-diagnostics.aspx

Worker Role app.config.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <trace>
            <listeners>
                <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener,                            Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0,                            Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    name="AzureDiagnostics">
                    <filter type="" />
                </add>
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

Upvotes: 0

Related Questions