Latest Replies
Tuesday
Jul202010

Lokad CQRS - Advanced Task Scheduling with Calendars in the Cloud

This is the next article in the Learning series for Lokad.CQRS Guidance. In the previous tutorial we've talked about using NHibernate Module to working with relational databases in the cloud. Sample-03 was covered.

Recently there was a question in Lokad community about implementing advanced scheduling capabilities with Lokad Cloud project.

I am considering creating a project to add some additional scheduling functionality to the Lokad.Cloud project. Specifically, I would like to be able to schedule tasks to run at specific times. Does anyone have any thoughts on what they would like to see in this project?

The question is interesting for two reasons:

  • First, it touches separation of Concerns (SOC) between Lokad.Cloud and Lokad.CQRS.
  • Second, there already is a reference implementation of advanced calendar scheduler in Lokad.CQRS ecosystem. It was not open sourced before, but now it is.

SoC between Lokad Cloud and Lokad CQRS

As we've mentioned in the Roadmap earlier, Lokad Cloud and CQRS projects target distinct audiences and scenarios. This helps them to stay focused and concise, reducing complexity and development friction around the frameworks as well as the solutions implemented with them.

Lokad.CQRS and Lokad.Cloud

Both projects share simple task schedulers that allow to execute commands at predefined intervals of time within your Windows Azure implementation. Yet there are differences.

Lokad.Cloud Task Scheduler is designed for the distributed processing and load balancing tasks, featuring integration with the Web Console, that allows to override timer intervals to fine-tune performance of algorithms on-the-run. That's the kind of functionality that is usually required by the Lokad.Cloud solutions.

Lokad.CQRS task scheduler executes operations at predefined intervals as well. It does not provide default integration with Web UI, instead specifying sleep timeout at the end of each operation. All operations are resolved and reliably executed within a separate IoC Container lifetime scope and transaction, providing native support for working with NHibernate Module and any other operation supporting System.Transactions. Such functionality is required by enterprise and business applications. It also provides foundation for delivering advanced calendar scheduling specific to your project. We'll talk about that later in this article.

Note: As it was mentioned earlier, both Lokad Cloud and Lokad CQRS projects are eventually going to become more interoperable, than they are now. Things like Lokad Message Format and reusable components are being architected into the design to allow building cloud solutions for Windows Azure that share the best of the two worlds: powerful scalability for cloud-intensive operations coupled with flexibility and reliability for enterprise and integration scenarios.

Advanced Task Scheduling Implementation

There is no default Calendar Scheduling module in Lokad CQRS simply because every project will have it's own persistence, configuration and serialization specific.

Yet, it is extremely easy to implement one. Article on Task Scheduler actually shows how easy it is to do that with NHibernate and Message Handling modules.

Advanced Calendar Scheduler in Lokad CQRS App Engine

In short, we create our schedule entries and save them to the database like this:

var schedule = new ScheduleEntity
{
  ScheduleFrequency = ScheduleFrequency.Daily,
  ScheduleTime = 4.Hours(),
  PayloadData = payload.Data,
  PayloadType = payload.Type,
  ScheduleId = scheduleId,  
  Title = "Run daily at 4AM"
};
_session.Save(schedule);

or like this:

var schedule = new ScheduleEntity
{
  NextRun = new DateTime(2012, 12, 21);
  PayloadData = payload.Data,
  PayloadType = payload.Type,
  ScheduleId = scheduleId,  
  Title = "Run once at the supposed end of the world"
};
_session.Save(schedule);

Where payload just contains serialized information about the message to be sent (it could be any command or event), when the moment comes. This message will go to Message Handling Module, starting the appropriate handler in a reliable and transactional manner.

Following Schedule Frequencies are supported:

public enum ScheduleFrequency
{
  None,
  Daily,
  Weekly,
  WorkDays,
  Every,
  Monthly
}

However, if you take a closer look at CalendarSchedulerTask, you'll see how easy it is to add more complex rules. This class is just an operation running every minute within the Task Scheduler. It is responsible for checking the calendar table, dispatching new commands and rescheduling commands that have some recurrent logic (i.e.: Daily, Monthly). The reference source is included within the Lokad.CQRS TaskScheduler documentation.

Further Reading:

Summary

This was another tutorial from Lokad.CQRS Guidance Guidance. We've talked about implementing advanced calendar scheduling with this framework. You can subscribe to updates RSS for more articles.

« Lokad CQRS - Message Throttling and Auto Scaling in the Cloud | Main | Squarespace - Beware of the Broken Links »