0

I'm trying to gain some understanding and experience in creating background processes on Azure.

I've created a simple console app and converted it to Azure Worker Role. How do I invoke it? I tried to use Azure Scheduler but looks like the scheduler can only invoke a worker role through message queues or HTTP/HTTPS.

I never thought about any type of communication as my idea was to create a background process that does not really communicate with any other app. Do I need to convert the worker role to a web role and invoke it using Azure Scheduler using HTTP/HTTPS?

Sam
  • 26,817
  • 58
  • 206
  • 383
  • Are you talking about a Worker Role or a Web Job. It sounds to me like you actually want a Web Job. – Brendan Green Oct 30 '14 at 04:28
  • I think you're missing a key point about Worker Roles: They are *virtual machines*. They're not a task you simply invoke. – David Makogon Oct 30 '14 at 11:32
  • David, you're right. I wasn't clear about that. What is the solution I'm looking for then? I simply want to call a background task. – Sam Oct 30 '14 at 14:54

1 Answers1

-1

Worker role has three events:

  1. OnStart
  2. OnRun
  3. OnStop
public class WorkerRole : RoleEntryPoint
  {
      ManualResetEvent CompletedEvent = new ManualResetEvent(false);

      public override void Run()
      {
          //Your background processing code
          CompletedEvent.WaitOne();
      }

      public override bool OnStart()
      {
          return base.OnStart();
      }

      public override void OnStop()
      {
          CompletedEvent.Set();
          base.OnStop();
      }
  }

The moment you run/debug your console converted worker role. First two (OnStart & OnRun) fires in sequence. Now in OnRun you have to keep the thread alive, either by using a while loop or using ManualResetEvent this is where your background processing code would live.

OnStop is fired when you either release the thread from OnRun or something un-expected goes. This is the place to dispose your objects. Close unclosed file-handles database connection etc.

Abhijeet
  • 13,562
  • 26
  • 94
  • 175