Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serverless Workflows with Durable Functions

Serverless Workflows with Durable Functions

Serverless is the next evolution in cloud computing. Azure Functions are simple, short-lived, stateless, single-purpose actions that respond to events in the cloud.

But your applications are probably complex, multi-step, long-running, and stateful! Are you still able to reap the benefits of serverless? Do you have to hand-craft the interactions of stateless isolated functions yourself?

In this talk, Mikhail will demonstrate how Durable Functions combine Azure Functions and Durable Tasks to enable you to manage the state of workflows in the cloud. The toolbox includes "fan-in fan-out" pattern, waiting for external events and human interaction, error handling, retries and timeouts, and more.

Mikhail Shilkov

April 09, 2019
Tweet

More Decks by Mikhail Shilkov

Other Decks in Programming

Transcript

  1. Mikhail Shilkov Software Developer Microsoft Azure MVP Serverless Azure Functions

    Functional Programming F# @MikhailShilkov https://mikhail.io
  2. $

  3. [FunctionName("SendText")] public static string SendText( [ActivityTrigger] string message) { var

    number = Config.alertMobileNumber; SmsService.Send(number, message); }
  4. [FunctionName("SendText")] public static string SendText( [ActivityTrigger] string message) { var

    number = Config.alertMobileNumber; SmsService.Send(number, message); }
  5. [FunctionName("SendText")] public static string SendText( [ActivityTrigger] string message) { var

    number = Config.alertMobileNumber; SmsService.Send(number, message); }
  6. public static async Task Sequential(DurableOrchestrationContext context) { var text =

    await context.CallActivityAsync<string> ("SendText", "SMS Text"); var email = await context.CallActivityAsync<string> ("SendEmail", "Email Text"); var entry = new AlertEntry(text, email); await context.CallActivityAsync("RecordAlert", entry); }
  7. 1. Orchestrator Started 2. ExecutionStarted: RaiseAlert 3. TaskScheduled: SendText 4.

    OrchestratorCompleted 5. TaskCompleted: "Text sent at 11:33" 6. OrchestratorStarted 7. TaskScheduled: SendEmail 8. OrchestratorCompleted 9. TaskCompleted: “Email sent at 11:35"
  8. public static async Task Sequential(DurableOrchestrationContext context) { var text =

    await context.CallActivityAsync<string> ("SendText", "SMS Text"); var email = await context.CallActivityAsync<string> ("SendEmail", "Email Text"); var entry = new AlertEntry(text, email); await context.CallActivityAsync("RecordAlert", entry); }
  9. public static async Task SuperOrchestrator( DurableOrchestrationContext context) { await context.CallSubOrchestratorAsync

    ("RaiseAlert", serverOverloaded); await context.CallSubOrchestratorAsync ("RaiseAlert", applicationDown); }
  10. public static async Task ErrorHandling(DurableOrchestrationContext context) { var text =

    await context.CallActivityAsync<string>("SendText", "SMS Text"); string email = null; try { email = await context.CallActivityAsync<string>("SendEmail", "Email Text"); } catch (FunctionFailedException) { email = await context.CallActivityAsync<string>("SendAlternativeEmail", "Email"); } var entry = new AlertEntry(text, email); await context.CallActivityAsync("RecordAlert", entry); }
  11. public static async Task ErrorHandling(DurableOrchestrationContext context) { var text =

    await context.CallActivityAsync<string>("SendText", "SMS Text"); string email = null; try { email = await context.CallActivityAsync<string>("SendEmail", "Email Text"); } catch (FunctionFailedException) { email = await context.CallActivityAsync<string>("SendAlternativeEmail", "Email"); } var entry = new AlertEntry(text, email); await context.CallActivityAsync("RecordAlert", entry); }
  12. public static async Task Retries(DurableOrchestrationContext context) { var text =

    await context.CallActivityAsync<string>("SendText", "SMS Text"); var options = new RetryOptions( firstRetryInterval: TimeSpan.FromMinutes(1), maxNumberOfAttempts: 4); options.BackoffCoefficient = 2.0; await context.CallActivityWithRetryAsync("SendEmail", options, email); var entry = new AlertEntry(text, email); await context.CallActivityAsync("RecordAlert", entry); }
  13. public static async Task Parallel(DurableOrchestrationContext context) { var textTask =

    context.CallActivityAsync<string> ("SendText", "SMS Text"); var emailTask = context.CallActivityAsync<string> ("SendEmail", "Email Text"); var text = await textTask; var email = await emailTask; var entry = new AlertEntry(text, email); await context.CallActivityAsync("RecordAlert", entry); }
  14. public static async Task Parallel(DurableOrchestrationContext cxt) { var tasks =

    alerts.Select(alert => cxt.CallActivityAsync<string>("SendText", alert)); var emails = Task.WhenAll(tasks); // ... }
  15. var email = await context.CallActivityAsync<string>("SendEmail", "Email Text"); // Wait for

    10 minutes and check again var waitUntil = context.CurrentUtcDateTime.Date.Add(TimeSpan.FromMinutes(10)); await context.CreateTimer(waitUntil); var isResolved = await context.CallActivityAsync<bool>("CheckStatus", "Sensor"); if (!isResolved) { var text = await context.CallActivityAsync<string>("SendText", "SMS Text"); // Wait for result with timeout var activityTask = context.WaitForExternalEvent<bool>("Approval"); var timeoutTask = context.CreateTimer(deadline); // Determine the outcome var winner = await Task.WhenAny(activityTask, timeoutTask); return winner == activityTask && activityTask.Result; }
  16. var email = await context.CallActivityAsync<string>("SendEmail", "Email Text"); // Wait for

    10 minutes and check again var waitUntil = context.CurrentUtcDateTime.Date.Add(TimeSpan.FromMinutes(10)); await context.CreateTimer(waitUntil); var isResolved = await context.CallActivityAsync<bool>("CheckStatus", "Sensor"); if (!isResolved) { var text = await context.CallActivityAsync<string>("SendText", "SMS Text"); // Wait for result with timeout var activityTask = context.WaitForExternalEvent<bool>("Approval"); var timeoutTask = context.CreateTimer(deadline); // Determine the outcome var winner = await Task.WhenAny(activityTask, timeoutTask); return winner == activityTask && activityTask.Result; }
  17. var email = await context.CallActivityAsync<string>("SendEmail", "Email Text"); // Wait for

    10 minutes and check again var waitUntil = context.CurrentUtcDateTime.Date.Add(TimeSpan.FromMinutes(10)); await context.CreateTimer(waitUntil); var isResolved = await context.CallActivityAsync<bool>("CheckStatus", "Sensor"); if (!isResolved) { var text = await context.CallActivityAsync<string>("SendText", "SMS Text"); // Wait for result with timeout var activityTask = context.WaitForExternalEvent<bool>("Approval"); var timeoutTask = context.CreateTimer(deadline); // Determine the outcome var winner = await Task.WhenAny(activityTask, timeoutTask); return winner == activityTask && activityTask.Result; }
  18. var email = await context.CallActivityAsync<string>("SendEmail", "Email Text"); // Wait for

    10 minutes and check again var waitUntil = context.CurrentUtcDateTime.Date.Add(TimeSpan.FromMinutes(10)); await context.CreateTimer(waitUntil); var isResolved = await context.CallActivityAsync<bool>("CheckStatus", "Sensor"); if (!isResolved) { var text = await context.CallActivityAsync<string>("SendText", "SMS Text"); // Wait for result with timeout var activityTask = context.WaitForExternalEvent<bool>("Approval"); var timeoutTask = context.CreateTimer(deadline); // Determine the outcome var winner = await Task.WhenAny(activityTask, timeoutTask); return winner == activityTask && activityTask.Result; }
  19. var email = await context.CallActivityAsync<string>("SendEmail", "Email Text"); // Wait for

    10 minutes and check again var waitUntil = context.CurrentUtcDateTime.Date.Add(TimeSpan.FromMinutes(10)); await context.CreateTimer(waitUntil); var isResolved = await context.CallActivityAsync<bool>("CheckStatus", "Sensor"); if (!isResolved) { var text = await context.CallActivityAsync<string>("SendText", "SMS Text"); // Wait for result with timeout var activityTask = context.WaitForExternalEvent<bool>("Approval"); var timeoutTask = context.CreateTimer(deadline); // Determine the outcome var winner = await Task.WhenAny(activityTask, timeoutTask); return winner == activityTask && activityTask.Result; }
  20. // activity.js module.exports = function(context) { return `Hello ${context.bindings.name}!`; };

    // orchestrator.js const df = require("durable-functions"); module.exports = df.orchestrator(function*(context) { const output = []; output.push(yield context.df.callActivity("E1_SayHello", "Tokyo")); output.push(yield context.df.callActivity("E1_SayHello", "Seattle")); output.push(yield context.df.callActivity("E1_SayHello", "London")); return output; });
  21. // Activity let sayHello = Activity.define "SayHello" (sprintf "Hello %s!")

    // Orchestrator let workflow = orchestrator { let! hello1 = Activity.call sayHello "Tokyo" let! hello2 = Activity.call sayHello "Seattle" let! hello3 = Activity.call sayHello "London" return [hello1; hello2; hello3] }