-
Notifications
You must be signed in to change notification settings - Fork 295
Example Video Encoding
Assume that user wants to build a code orchestration that will encode a video and then send an email to the user when it is done.
To implement this using the Service Bus Durable Task Framework, the user will write two Task Activities for encoding a video and sending email and one Task Orchestration that orchestrates between these two.
public class EncodeVideoOrchestration : TaskOrchestration<string, string>
{
public override async Task<string> RunTask(OrchestrationContext context,
string input)
{
string encodedUrl =
await context.ScheduleTask<string>(typeof (EncodeActivity), input);
await context.ScheduleTask<object>(typeof (EmailActivity), input);
return encodedUrl;
}
}
In this orchestration, the user is scheduling the Encode Video activity, waiting for the response and then scheduling the Send Email activity. The framework will ensure that the state of the execution is preserved durably. E.g., if the node hosting the task orchestration above crashed before scheduling the Encode Video activity, on restart it will know to schedule this activity. If the node crashed after it had scheduled the activity but before the response came back, on restart it will be smart enough to know that the activity was already scheduled and it will directly start waiting for the response of the EncodeVideo activity.
public class EncodeActivity : TaskActivity<string, string>
{
protected override string Execute(TaskContext context, string input)
{
Console.WriteLine("Encoding video " + input);
// TODO : actually encode the video to a destination
return "http://<azurebloblocation>/encoded_video.avi";
}
}
public class EmailActivity : TaskActivity<string, object>
{
protected override object Execute(TaskContext context, string input)
{
// TODO : actually send email to user
return null;
}
}
The user code above (EncodeVideoOrchestration, EncodeActivity and EmailActivity) needs to be hosted and available somewhere to be useful.
This is how users can load these orchestration and activity classes in a worker and start processing requests to create new orchestration instances.
string serviceBusConnString = "Endpoint=sb://<namespace>.servicebus.windows.net/;SharedSecretIssuer=[issuer];SharedSecretValue=[value]";
TaskHubWorker hubWorker = new TaskHubWorker("myvideohub", serviceBusConnString)
.AddTaskOrchestrations(typeof (EncodeVideoOrchestration))
.AddTaskActivities(typeof (EncodeActivity), typeof (EmailActivity))
.Start();
Multiple instances of these workers can be running concurrently against the same task hub to provide load balancing as required. The framework guarantees that a particular orchestration instance code would only be executing on a single worker at one time.
The TaskHubWorker also exposes methods to stop the worker instance.
The last remaining piece is creation and management of orchestration instances i.e. how to actually trigger the code orchestrations that the user has loaded and how to monitor or terminate them.
string serviceBusConnString = "Endpoint=sb://<namespace>.servicebus.windows.net/;SharedSecretIssuer=[issuer];SharedSecretValue=[value]";
TaskHubClient client = new TaskHubClient("myvideohub", serviceBusConnString);
client.CreateOrchestrationInstance(typeof (EncodeVideoOrchestration), "http://<azurebloblocation>/MyVideo.mpg");