Home

Awesome

BranchStatus
mainBuild Status
devBuild Status

Durable Functions for Node.js

The durable-functions npm package allows you to write Durable Functions for Node.js. Durable Functions is an extension of Azure Functions that lets you write stateful functions and workflows in a serverless environment. The extension manages state, checkpoints, and restarts for you. Durable Functions' advantages include:

You can find more information at the following links:

A durable function, or orchestration, is a solution made up of different types of Azure Functions:

Durable Functions' function types and features are documented in-depth here.

Getting Started

You can follow the Visual Studio Code quickstart to get started with a function chaining example, or follow the general checklist below:

  1. Install prerequisites:

  2. Create an Azure Functions app. Visual Studio Code's Azure Functions plugin is recommended.

  3. Install the Durable Functions extension

Run this command from the root folder of your Azure Functions app:

func extensions install -p Microsoft.Azure.WebJobs.Extensions.DurableTask -v 1.8.3

durable-functions requires Microsoft.Azure.WebJobs.Extensions.DurableTask 1.8.3 or greater.

  1. Install the durable-functions npm package at the root of your function app:
npm install durable-functions
  1. Write an activity function (see sample):
module.exports = async function(context) {
    // your code here
};
  1. Write an orchestrator function (see sample):
const df = require('durable-functions');
module.exports = df.orchestrator(function*(context){
    // your code here
});

Note: Orchestrator functions must follow certain code constraints.

  1. Write your client function (see sample):
module.exports = async function (context, req) {
    const client = df.getClient(context);
    const instanceId = await client.startNew(req.params.functionName, undefined, req.body);

    context.log(`Started orchestration with ID = '${instanceId}'.`);

    return client.createCheckStatusResponse(context.bindingData.req, instanceId);
};

Note: Client functions are started by a trigger binding available in the Azure Functions 2.x major version. Read more about trigger bindings and 2.x-supported bindings.

Samples

The Durable Functions samples demonstrate several common use cases. They are located in the samples directory. Descriptive documentation is also available:

const df = require("durable-functions");

module.exports = df.orchestrator(function*(context){
    context.log("Starting chain sample");
    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;
});

How it works

Durable Functions

One of the key attributes of Durable Functions is reliable execution. Orchestrator functions and activity functions may be running on different VMs within a data center, and those VMs or the underlying networking infrastructure is not 100% reliable.

In spite of this, Durable Functions ensures reliable execution of orchestrations. It does so by using storage queues to drive function invocation and by periodically checkpointing execution history into storage tables (using a cloud design pattern known as Event Sourcing). That history can then be replayed to automatically rebuild the in-memory state of an orchestrator function.

Read more about Durable Functions' reliable execution.

Durable Functions JS

The durable-functions shim lets you express a workflow in code as a generator function wrapped by a call to the orchestrator method. orchestrator treats yield-ed calls to your function context's df object, like context.df.callActivity, as points where you want to schedule an asynchronous unit of work and wait for it to complete.

These calls return a Task or TaskSet object signifying the outstanding work. The orchestrator method appends the action(s) of the Task or TaskSet object to a list which it passes back to the Functions runtime, plus whether the function is completed, and any output or errors.

The Azure Functions extension schedules the desired actions. When the actions complete, the extension triggers the orchestrator function to replay up to the next incomplete asynchronous unit of work or its end, whichever comes first.