This project is read-only.

Extensible Windows Azure

The main addition in this release compared to previous releases is the addition of Work Services.

Work Services

We have been using Managed Extensibility Framework (MEF) throughout this project. Up until now it's mostly been used for Dependency Injection purposes. MEF in itself is not built to be a DI Container but solves the extensibility challenge by way of using this technique. For DI we could have used any of the many DI containers out there. In this release, however, the use of MEF and the rationale behind it becomes more apparent.

The current worker role in Windows Azure works by way of entering a perpetual loop, waiting for more work and executing it as it comes in. This is the canonical example for extensibility!

public override void Start()
{
   while (true)
   {
      // do work here
   }
}

The problem arises when you want to begin adding more than one task to your loop or even N tasks. The main app code references all other code and the maintenance of this little hamster wheel soon becomes a nightmare.

public override void Start()
{
   while (true)
   {
      // do work 1
      // or work 2
      // or work 3
     // [...]
   }
}

Enter the WorkService! ;~)

Work Service

The work service turns the above code mess into pure elegance:

protected override void OnStart()
{
   while (true)
   {
      var work = WorkService.GetWork();
      RoleStatus = work.DoWork();
   }
}

But wait a minute that's just hiding the problem away somewhere else! And what if there is no work?

To answer the question first; there is always work. If there isn't the work is an "idle" work task that puts the worker to sleep for a short while.
And, no, this WorkService does not hide the problem away somewhere else. The WorkService uses MEF to import all the available work and sort it in a good and prioritized order!

Inside the work service the main .GetWork() method looks something like this:

[Import]
public IWorkSchedulerProvider WorkSchedulerProvider { get; set; }

[Import]
public IRoleManager RoleManager { get; set; }

[ImportMany]
public IEnumerable<Lazy<IWork, IWorkMetadata>> Works { get; set; }

public IWork GetWork()
{
	var workScheduler = WorkSchedulerProvider.GetCurrentWorkScheduler();
	var orderedWorks = workScheduler.ScheduleWork(Works);

	foreach (var lazyWork in orderedWorks)
	{
		if (lazyWork.Value.HasWork)
		{
			return new WorkTemplate(lazyWork, RoleManager);
		}
	}
	return new IdleWork(RoleManager);
}

There is a blog post here that explains this code snippet further: Extensible Windows Azure projects using MEF

One-Time Work Service

The One-Time Work Serivce is a staeful work service that will execute a specific set of IOneTimeWork tasks once (per Windows Azure Role instance).

Sample

The release also comes with a small sample (default.aspx). This consists of a page that can add (fake) work to two different work queues and a worker role that picks up work tasks to run. The tasks are all sleep tasks. The interesting thing to check out in the sample apart from the code is the usage of logging that shows the roles staring up and logging IOneTimeWork and also shows each IWork task created and executed. The sample has two work tasks “A” and “B” where the first one is more prioritized in the worker.

Cannot resolve release macro, invalid id.

Last edited Sep 6, 2009 at 11:09 PM by NoopMan, version 2

Comments

No comments yet.