Skip to content

Commit c3aa2c4

Browse files
authored
Add native support for warmup trigger (#180)
* Add warmup trigger * Additional changes to types * Add back trigger property * Minor changes to docs, types, signatures * Revert a few minor edits * PR review suggestions * Add interface for warmupContext
1 parent cc818aa commit c3aa2c4

File tree

7 files changed

+53
-2
lines changed

7 files changed

+53
-2
lines changed

src/addBindingName.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
export { InvocationContext } from './InvocationContext';
45
export { HttpRequest } from './http/HttpRequest';
56
export { HttpResponse } from './http/HttpResponse';
6-
export { InvocationContext } from './InvocationContext';
77

88
const bindingCounts: Record<string, number> = {};
99
/**
10-
* If the host spawns multiple workers, it expects the metadata (including binding name) to be the same accross workers
10+
* If the host spawns multiple workers, it expects the metadata (including binding name) to be the same across workers.
1111
* That means we need to generate binding names in a deterministic fashion, so we'll do that using a count
1212
* There's a tiny risk users register bindings in a non-deterministic order (i.e. async race conditions), but it's okay considering the following:
1313
* 1. We will track the count individually for each binding type. This makes the names more readable and reduces the chances a race condition will matter

src/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
StorageBlobFunctionOptions,
2020
StorageQueueFunctionOptions,
2121
TimerFunctionOptions,
22+
WarmupFunctionOptions,
2223
} from '@azure/functions';
2324
import * as coreTypes from '@azure/functions-core';
2425
import { CoreInvocationContext, FunctionCallback } from '@azure/functions-core';
@@ -145,6 +146,10 @@ export function cosmosDB(name: string, options: CosmosDBFunctionOptions): void {
145146
generic(name, convertToGenericOptions(options, <any>trigger.cosmosDB));
146147
}
147148

149+
export function warmup(name: string, options: WarmupFunctionOptions): void {
150+
generic(name, convertToGenericOptions(options, trigger.warmup));
151+
}
152+
148153
export function generic(name: string, options: GenericFunctionOptions): void {
149154
if (!hasSetup) {
150155
setup();

src/trigger.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
StorageQueueTriggerOptions,
2323
TimerTrigger,
2424
TimerTriggerOptions,
25+
WarmupTrigger,
26+
WarmupTriggerOptions,
2527
} from '@azure/functions';
2628
import { addBindingName } from './addBindingName';
2729

@@ -90,6 +92,13 @@ export function cosmosDB(options: CosmosDBTriggerOptions): CosmosDBTrigger {
9092
});
9193
}
9294

95+
export function warmup(options: WarmupTriggerOptions): WarmupTrigger {
96+
return addTriggerBindingName({
97+
...options,
98+
type: 'warmupTrigger',
99+
});
100+
}
101+
93102
export function generic(options: GenericTriggerOptions): FunctionTrigger {
94103
return addTriggerBindingName(options);
95104
}

types/app.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { HttpFunctionOptions, HttpHandler, HttpMethodFunctionOptions } from './h
99
import { ServiceBusQueueFunctionOptions, ServiceBusTopicFunctionOptions } from './serviceBus';
1010
import { StorageBlobFunctionOptions, StorageQueueFunctionOptions } from './storage';
1111
import { TimerFunctionOptions } from './timer';
12+
import { WarmupFunctionOptions } from './warmup';
1213

1314
/**
1415
* Registers an http function in your app that will be triggered by making a request to the function url
@@ -143,6 +144,18 @@ export function eventGrid(name: string, options: EventGridFunctionOptions): void
143144
*/
144145
export function cosmosDB(name: string, options: CosmosDBFunctionOptions): void;
145146

147+
/**
148+
* Registers a function in your app that will be triggered when an instance is added to scale a running function app.
149+
* The warmup trigger is only called during scale-out operations, not during restarts or other non-scale startups.
150+
* Make sure your logic can load all required dependencies without relying on the warmup trigger.
151+
* Lazy loading is a good pattern to achieve this goal.
152+
* The warmup trigger isn't available to apps running on the Consumption plan.
153+
* For more information, please see the [Azure Functions warmup trigger documentation](https://learn.microsoft.com/azure/azure-functions/functions-bindings-warmup?tabs=isolated-process&pivots=programming-language-javascript).
154+
* @param name The name of the function. The name must be unique within your app and will mostly be used for your own tracking purposes
155+
* @param options Configuration options describing the inputs, outputs, and handler for this function
156+
*/
157+
export function warmup(name: string, options: WarmupFunctionOptions): void;
158+
146159
/**
147160
* Registers a generic function in your app that will be triggered based on the type specified in `options.trigger.type`
148161
* Use this method if your desired trigger type does not already have its own method

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export * from './storage';
2020
export * from './table';
2121
export * from './timer';
2222
export * as trigger from './trigger';
23+
export * from './warmup';
2324

2425
/**
2526
* Void if no `return` output is registered

types/trigger.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
StorageQueueTriggerOptions,
2121
} from './storage';
2222
import { TimerTrigger, TimerTriggerOptions } from './timer';
23+
import { WarmupTrigger, WarmupTriggerOptions } from './warmup';
2324

2425
/**
2526
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook-trigger?&pivots=programming-language-javascript)
@@ -66,6 +67,11 @@ export function eventGrid(options: EventGridTriggerOptions): EventGridTrigger;
6667
*/
6768
export function cosmosDB(options: CosmosDBTriggerOptions): CosmosDBTrigger;
6869

70+
/**
71+
* [Link to docs and examples](https://learn.microsoft.com/azure/azure-functions/functions-bindings-warmup?tabs=isolated-process&pivots=programming-language-javascript)
72+
*/
73+
export function warmup(options: WarmupTriggerOptions): WarmupTrigger;
74+
6975
/**
7076
* A generic option that can be used for any trigger type
7177
* Use this method if your desired trigger type does not already have its own method

types/warmup.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import { FunctionOptions, FunctionResult, FunctionTrigger } from './index';
5+
import { InvocationContext } from './InvocationContext';
6+
7+
export interface WarmupContextOptions {}
8+
export type WarmupHandler = (warmupContext: WarmupContextOptions, context: InvocationContext) => FunctionResult;
9+
10+
export interface WarmupFunctionOptions extends WarmupTriggerOptions, Partial<FunctionOptions> {
11+
handler: WarmupHandler;
12+
13+
trigger?: WarmupTrigger;
14+
}
15+
16+
export interface WarmupTriggerOptions {}
17+
export type WarmupTrigger = FunctionTrigger & WarmupTriggerOptions;

0 commit comments

Comments
 (0)