Azure WorkerRole trigger on queue like WebJob


I'm used to use webjob on Azure triggering an Azure queue. It works like a charm.

Azure tutorial webjob + queue

static void Main(string[] args)
{
    JobHost host = new JobHost();
    host.RunAndBlock();
}

public static void ProcessQueueMessage([QueueTrigger("logqueue")] string logMessage, TextWriter logger)
{
    logger.WriteLine(logMessage);
}

What's really good with queueTrigger is until the process triggered by a message isnot done, the message is keep invisible (not delete). So If you turn off the webjob (for webjob update for example) The message will be visible (after a little timeout) in the queue to be process by the updated webjob (perfect).

Now I wanna do the same thing but on a worker role. Today I do like this.

while (true)
{
     var cloudMessage = await sourceImportationQueue.GetMessageAsync();
     if (cloudMessage != null)
           sourceImportationQueue.DeleteMessage(cloudMessage);
      // process my job (few hours)
     else
           await Task.Delay(1000 * 5);
}

But if I stop the worker during is job, I lost the message. So how can I do like webJob trigge

June 26th, 2015 6:15am

Hi Julian,

Thank you for reaching out to us.I am currently researching to gather more information with regards to your request.

Sincerely appreciate your patience.

Regards,

Free Windows Admin Tool Kit Click here and download it now
June 26th, 2015 2:15pm

Hi Julian,

Thank you for reaching out to us.I am currently researching to gather more information with regards to your request.

Sincerely appreciate your patience.

Regards,

June 26th, 2015 6:12pm

Hi Julian,

Thank you for reaching out to us.I am currently researching to gather more information with regards to your request.

Sincerely appreciate your patience.

Regards,

Free Windows Admin Tool Kit Click here and download it now
June 26th, 2015 6:12pm

Hi Julian,

Thank you for reaching out to us.I am currently researching to gather more information with regards to your request.

Sincerely appreciate your patience.

Regards,

June 26th, 2015 6:12pm

Hi Julian,

Thank you for reaching out to us.I am currently researching to gather more information with regards to your request.

Sincerely appreciate your patience.

Regards,

Free Windows Admin Tool Kit Click here and download it now
June 26th, 2015 6:12pm

Hi Julian,

Thank you for reaching out to us.I am currently researching to gather more information with regards to your request.

Sincerely appreciate your patience.

Regards,

June 26th, 2015 6:12pm

Hi Julian50,

Since the maximum lock period for a message is 5minutes,so that the message will re-appear again while your job is still running and if the delete operation was moved to the end it would fail due to having the lost of lock.

Possible solution would be write the job status, for example Azure table and record state throughout the processing of your job.

Regards

Asha99

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 7:31am

Hi Julian50,

Since the maximum lock period for a message is 5minutes,so that the message will re-appear again while your job is still running and if the delete operation was moved to the end it would fail due to having the lost of lock.

Possible solution would be write the job status, for example Azure table and record state throughout the processing of your job.

Regards

Asha99

June 28th, 2015 11:29am

Hi Julian50,

Since the maximum lock period for a message is 5minutes,so that the message will re-appear again while your job is still running and if the delete operation was moved to the end it would fail due to having the lost of lock.

Possible solution would be write the job status, for example Azure table and record state throughout the processing of your job.

Regards

Asha99

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 11:29am

Hi Julian50,

Since the maximum lock period for a message is 5minutes,so that the message will re-appear again while your job is still running and if the delete operation was moved to the end it would fail due to having the lost of lock.

Possible solution would be write the job status, for example Azure table and record state throughout the processing of your job.

Regards

Asha99

June 28th, 2015 11:29am

Hi Julian50,

Since the maximum lock period for a message is 5minutes,so that the message will re-appear again while your job is still running and if the delete operation was moved to the end it would fail due to having the lost of lock.

Possible solution would be write the job status, for example Azure table and record state throughout the processing of your job.

Regards

Asha99

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 11:29am

Hi Asha99,

thank you for your answer. But I really looking for something similar of triggering mechanisme of webjob. It looks a easier solution than add table, add.. So, for webjob I know that the message is re-new or the timeout update until the job fail or success. I don't how to do this.

June 29th, 2015 12:12pm

Finally I find a simple solution. Before running my job of several hours, I launch a task KeepHiddenMessageAsync that update the message with the timeout. Before the end of the timeout a new update of the message is done. If an problem occur then the timeout of the message will be reached and the message will become visible.

        private bool jobIsComplete;

        private void Run()
        {
             while (true)
            {
                 jobIsComplete = false;
                 //get the message
                 var cloudMessage = await queue.GetMessageAsync();

                 if (cloudMessage != null)
                        //run the task to keep the message until end of the job and worker role stopping for an update for example 
                       var keepHiddenMessageTask = KeepHiddenMessageAsync(cloudMessage);

                        //
                        // process my job (few hours)
                        //

                      jobIsComplete = true;
                      await keepHiddenMessageTask;
                      await _queue.DeleteMessageAsync(cloudMessage);
                 else
                       await Task.Delay(1000 * 5);
            }
        }

        private async Task KeepHiddenMessageAsync(CloudQueueMessage iCloudQueueMessage)
        {
            while (true)
            {
                //Update message and hidding during 5 new minutes
                await _queue.UpdateMessageAsync(iCloudQueueMessage, TimeSpan.FromMinutes(5), MessageUpdateFields.Visibility);

                //Wait 4 minutes
                for (int i = 0; i < 60 * 4; i++)
                {
                    if (JobIsComplete)
                        return;
                    else
                        await Task.Delay(1000);
                }
            }
        }
Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 10:52am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics