The Task Scheduler Service is one of external services provided out-of-the-box by AppFabric Applications. It is implemented as stateful (see Episode III) service that allows you to remotely trigger scheduled tasks. The task is wrapped up by class DistributedTask and can operate at defined times or regular intervals. The task-service triggers a callback defined within the task on the specified schedule.
This service is useful to schedule any task I reliable manner. In typical scenario you would use some kind of service hosted for example in Service Control Manager or simply define the schedule task in operative system which would execute some code. It is interesting that everybody use such scenarios, but we often do no think about scaling such task by building of fall-back mechanism. Imagine you have to perform tasks such as sending out a daily report or updating statistics every hour. How this would simple requirement would be implemented to run on two or more machines in parallel? I’m glad to hear any suggestion related to this problem.
Here is the solution which is still in design, but it gives an idea about, how this could work in the near future. Distributed Task is by definition scaled over multiple nodes. it is implemented as stateful service and has ability to synchronize its state between instances on all nodes. Exactly this synchronization is design challenge by implementing task pattern. Fortunately, AppFabric Applications offers Stateful-Services. That means, Task Scheduler is implemented on top of stateful service.
Following picture shows the architecture of Task Scheduler. When the task is started it is span across multiple machines. This is defined by ScaleoutCount on container level.
Once the start time is reached the Task Scheduler will send the message to endpoint which has been previously configured. First message is sent to the Execution Endpoint when task is triggered. Second endpoint receives second message which is send after the task is completed. The implementation of task could be implemented in Execution Endpoint.
Following code shows how to start one task which starts immediately and recurs every 25 seconds. The task registers also both callback endpoints.
TaskSchedulerClient client = ServiceReferences.CreateImport1("Tasks"); DistributedTask taskToCreate = new DistributedTask();
taskToCreate.ExecutionEndPoint = new HttpCallbackEndPoint(new Uri(executionUrl)); taskToCreate.CompletionEndPoint = new HttpCallbackEndPoint(new Uri(completionUrl)); taskToCreate.Recurrence = TaskRecurrence.FromSeconds(25); taskToCreate.StartAfterTimeUTC = DateTime.UtcNow; taskToCreate.Payload = UTF8Encoding.UTF8.GetBytes("payload"); |
I implemented this code in Default.aspx.cs file of sample ASP.NET Web Application. My callback endpoints are registered as Completion.aspx and Execution.aspx pages.
Following example shows how to grab the URI-s of all pages hosted in this Composite Application:
var uriBuilder = new UriBuilder(Request.UrlReferrer.Scheme, Request.UrlReferrer.Host, Request.UrlReferrer.Port); ServiceGroupDefinition serviceDef = Service.ExecutingService.GetDefinition().ServiceGroupDefinition; string virtualDir = webServiceGroupDef.VirtualDirectory; string executionUrl = uriBuilder.ToString() + virtualDir + "/Execution.aspx"; string completionUrl = uriBuilder.ToString() + virtualDir + "/Completion.aspx"; |
Steve has also described how to implement Task Scheduling based on Windows Azure classic programming model.
Posted
Sep 08 2011, 01:00 AM
by
Damir Dobric