Muokkaa

Jaa


Azure Functions warmup trigger

This article explains how to work with the warmup trigger in Azure Functions. A warmup trigger is invoked when an instance is added to scale a running function app. The warmup trigger lets you define a function that runs when a new instance of your function app is started. You can use a warmup trigger to preload custom dependencies so your functions are ready to start processing requests immediately. Some actions for a warmup trigger might include opening connections, loading dependencies, or running any other custom logic before your app begins receiving traffic.

The following considerations apply when using a warmup trigger:

  • The warmup trigger isn't available to apps running on the Consumption plan.
  • The warmup trigger isn't supported on version 1.x of the Functions runtime.
  • Support for the warmup trigger is provided by default in all development environments. You don't have to manually install the package or register the extension.
  • There can be only one warmup trigger function per function app, and it can't be invoked after the instance is already running.
  • The warmup trigger is only called during scale-out operations, not during restarts or other nonscaling startups. Make sure your logic can load all required dependencies without relying on the warmup trigger. Lazy loading is a good pattern to achieve this goal.
  • Dependencies created by warmup trigger should be shared with other functions in your app. To learn more, see Static clients.
  • If the built-in authentication (also known as Easy Auth) is used, HTTPS Only should be enabled for the warmup trigger to get invoked.

Example

A C# function can be created by using one of the following C# modes:

  • Isolated worker model: Compiled C# function that runs in a worker process that's isolated from the runtime. Isolated worker process is required to support C# functions running on LTS and non-LTS versions .NET and the .NET Framework.
  • In-process model: Compiled C# function that runs in the same process as the Functions runtime.
  • C# script: Used primarily when you create C# functions in the Azure portal.

The following example shows a C# function that runs on each new instance when added to your app.

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace SampleApp
{
    public static class Warmup
    {
        [Function(nameof(Warmup))]
        public static void Run([WarmupTrigger] object warmupContext, FunctionContext context)
        {
            var logger = context.GetLogger(nameof(Warmup));
            logger.LogInformation("Function App instance is now warm!");
        }
    }
}

The following example shows a warmup trigger that runs when each new instance is added to your app.

@FunctionName("Warmup")
public void warmup( @WarmupTrigger Object warmupContext, ExecutionContext context) {
    context.getLogger().info("Function App instance is warm.");
}

The following example shows a JavaScript function with a warmup trigger that runs on each new instance when added to your app:

const { app } = require('@azure/functions');

app.warmup('warmupTrigger', {
    handler: (warmupContext, context) => {
        context.log('Function App instance is warm.');
    },
});

The following example shows a TypeScript function with a warmup trigger that runs on each new instance when added to your app:

import { app, InvocationContext, WarmupContext } from '@azure/functions';

export async function warmupFunction(warmupContext: WarmupContext, context: InvocationContext): Promise<void> {
    context.log('Function App instance is warm.');
}

app.warmup('warmup', {
    handler: warmupFunction,
});

Here's the function.json file:

{
    "bindings": [
        {
            "type": "warmupTrigger",
            "direction": "in",
            "name": "warmupContext"
        }
    ]
}

PowerShell example code pending.

The following example shows a warmup trigger in a function.json file and a Python function that runs on each new instance when it'is added to your app.

Your function must be named warmup (case-insensitive) and there can only be one warmup function per app.

Here's the function.json file:

{
    "bindings": [
        {
            "type": "warmupTrigger",
            "direction": "in",
            "name": "warmupContext"
        }
    ]
}

For more information, see Configuration.

Here's the Python code:

import logging
import azure.functions as func


def main(warmupContext: func.Context) -> None:
    logging.info('Function App instance is warm.')

Attributes

Both in-process and isolated worker process C# libraries use the WarmupTrigger attribute to define the function. C# script instead uses a function.json configuration file.

Use the WarmupTrigger attribute to define the function. This attribute has no parameters.

Annotations

Warmup triggers don't require annotations. Just use a name of warmup (case-insensitive) for the FunctionName annotation.

Configuration

There are no properties that need to be set on the options object passed to the app.warmup() method.

Configuration

The following table explains the binding configuration properties that you set in the function.json file.

function.json property Description
type Required - must be set to warmupTrigger.
direction Required - must be set to in.
name Required - the variable name used in function code. A name of warmupContext is recommended for the binding parameter.

See the Example section for complete examples.

Usage

The following considerations apply to using a warmup function in C#:

  • Your function must be named warmup (case-insensitive) using the Function attribute.
  • A return value attribute isn't required.
  • Use the Microsoft.Azure.Functions.Worker.Extensions.Warmup package
  • You can pass an object instance to the function.

Your function must be named warmup (case-insensitive) using the FunctionName annotation.

See the list of considerations at the top of the page for general usage advice.

The function type in function.json must be set to warmupTrigger.

Next steps