wickjon
wickjon

Reputation: 920

Using time specific timer in Azure

I have my worker role and I need to run a specific task exactly at 10 AM each day.

public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {                  
        while (true)
        {
            if (DateTime.Now.Hour == 10)
            {
                //Do Specific timer Job();
            }
            //Do Normal Worker process();

            Thread.Sleep(TimeSpan.FromMinutes(1));

        }
    }

This runs the timer job multiple times since I get only the hour, I cant just check it with a specific time say 10.00 since there is a chance that it might be skipped by the main worker process.

I need to know the ideal way to implement this.

I have seen this link and those mentioned in the answers,

How to Schedule a task in windows azure worker role

But I need a solution without using azure scheduler, or any third party tool :( Anyway by which I can use timers to check with the specific time ?

Upvotes: 1

Views: 266

Answers (2)

djn
djn

Reputation: 113

Insert a simple queue message and set the visibility timeout of that message. So that the message will be visible only at that time and you can do the action.

public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {                  
        while (true)
        {
            CloudQueueMessage queuemessage = queuestorage.GetMessage(queue name)
            if(queueMessage.exist && queueMessage insertion time < 10 am today)
            {
                if (DateTime.Now.Hour >= 10)
                {
                    //Do Specific timer Job();
                    //Delete the queue message and insert new one for the task next day.
                }
            }

            //Do Normal Worker process();

            Thread.Sleep(TimeSpan.FromMinutes(1));

        }
    }
}

Upvotes: 0

Vladmir
Vladmir

Reputation: 1265

Probably you can use Azure WebJobs

http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#Scheduler

And also, today's post from Scott Hanselman with variety of libraries including Quartz.Net from previous commentator.

http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx

Upvotes: 1

Related Questions