Skip to content

This Quickstart uses Azure Developer command-line (azd) tools to create functions that respond to HTTP requests. After testing the code locally, you deploy it to a new serverless function app you create running in a Flex Consumption plan in Azure Functions.

License

Notifications You must be signed in to change notification settings

im-samz/functions-quickstart-typescript-azd-timer

 
 

Repository files navigation

Azure Functions TypeScript Timer Trigger using Azure Developer CLI

This template repository contains an timer trigger reference sample for functions written in TypeScript (Azure Functions v4 Node.js model) and deployed to Azure using the Azure Developer CLI (azd). The sample uses managed identity and a virtual network to make sure deployment is secure by default. You can opt out of a VNet being used in the sample by setting VNET_ENABLED to false in the parameters.

This project is designed to run on your local computer. You can also use GitHub Codespaces:

Open in GitHub Codespaces

This codespace is already configured with the required tools to complete this tutorial using either azd or Visual Studio Code. If you're working a codespace, skip down to Run your app section.

Common Use Cases for Timer Triggers

  • Regular data processing: Schedule batch processing jobs to run at specific intervals
  • Maintenance tasks: Perform periodic cleanup or maintenance operations on your data
  • Scheduled notifications: Send automated reports or alerts on a fixed schedule
  • Integration polling: Regularly check for updates in external systems that don't support push notifications

Prerequisites

Initialize the local project

You can initialize a project from this azd template in one of these ways:

  • Use this azd init command from an empty local (root) folder:

    azd init --template functions-quickstart-typescript-azd-timer

    Supply an environment name, such as flexquickstart when prompted. In azd, the environment is used to maintain a unique deployment context for your app.

  • Clone the GitHub template repository locally using the git clone command:

    git clone https://github.com/Azure-Samples/functions-quickstart-typescript-azd-timer.git
    cd functions-quickstart-typescript-azd-timer

    You can also clone the repository from your own fork in GitHub.

Prepare your local environment

Add a file named local.settings.json in the src folder with the following contents:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "node",
        "TIMER_SCHEDULE": "*/30 * * * * *"
    }
}

The TIMER_SCHEDULE setting defines when your timer function runs using NCRONTAB format. The example above runs every 30 seconds. For more information on NCRONTAB expressions, see Configuration.

Run your app from the terminal

  1. Start Azurite storage emulator in a separate terminal window:

    docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite
  2. From the src folder, install dependencies and build the TypeScript code:

    npm install
    npm run build
  3. Start the Functions host locally:

    npm start
  4. Wait for the timer schedule to execute the timer trigger.

  5. When you're done, press Ctrl+C in the terminal window to stop the func.exe host process.

Run your app using Visual Studio Code

  1. Open the src app folder in a new terminal.
  2. Run the code . code command to open the project in Visual Studio Code.
  3. Install dependencies: npm install
  4. In the command palette (F1), type Azurite: Start, which enables debugging without warnings.
  5. Press Run/Debug (F5) to run in the debugger. Select Debug anyway if prompted about local emulator not running.
  6. Wait for the timer schedule to trigger your timer function.

Deploy to Azure

Run this command to provision the function app, with any required Azure resources, and deploy your code:

azd up

Alternatively, you can opt-out of a VNet being used in the sample. To do so, use azd env to configure VNET_ENABLED to false before running azd up:

azd env set VNET_ENABLED false
azd up

Redeploy your code

You can run the azd up command as many times as you need to both provision your Azure resources and deploy code updates to your function app.

Note

Deployed code files are always overwritten by the latest deployment package.

Clean up resources

When you're done working with your function app and related resources, you can use this command to delete the function app and its related resources from Azure and avoid incurring any further costs:

azd down

Source Code

The function code for the timer trigger is defined in timerFunction.ts.

This code shows the timer function implementation:

/**
 * Timer-triggered Azure Function that demonstrates scheduled execution.
 * 
 * Timer-triggered function that executes on a schedule defined by TIMER_SCHEDULE app setting.
 * 
 * The runOnStartup: true parameter is useful for development and testing as it triggers
 * the function immediately when the host starts, but should typically be set to false
 * in production to avoid unexpected executions during deployments or restarts.
 */
export async function timerFunction(myTimer: Timer, context: InvocationContext): Promise<void> {
    context.log(`TypeScript Timer trigger function executed at: ${new Date().toISOString()}`);

    if (myTimer.isPastDue) {
        context.warn("The timer is running late!");
    }
}

app.timer('timerFunction', {
    schedule: '%TIMER_SCHEDULE%',
    runOnStartup: true,
    handler: timerFunction
});

The TypeScript/JavaScript library uses the timer trigger from @azure/functions to define the function

Key Features

  1. Parameterized Schedule: The function uses the %TIMER_SCHEDULE% environment variable to determine the execution schedule, making it configurable without code changes.

  2. RunOnStartup Parameter: Setting runOnStartup: true makes the function execute immediately when the app starts, in addition to running on the defined schedule. This is useful for testing but can be disabled in production.

  3. Past Due Detection: The function checks if the timer is past due using the myTimer.isPastDue property, allowing for appropriate handling of delayed executions.

  4. Modern TypeScript: Uses Azure Functions v4 programming model with TypeScript for enhanced type safety and developer experience.

  5. Async/Await: Leverages modern JavaScript async patterns for better performance and readability.

Configuration

The timer schedule is configured through the TIMER_SCHEDULE application setting, which follows the NCRONTAB expression format. For example:

  • 0 */5 * * * * - Run once every 5 minutes
  • 0 0 */1 * * * - Run once every hour
  • 0 0 0 * * * - Run once every day at midnight

For more information on NCRONTAB expressions, see Timer trigger for Azure Functions.

About

This Quickstart uses Azure Developer command-line (azd) tools to create functions that respond to HTTP requests. After testing the code locally, you deploy it to a new serverless function app you create running in a Flex Consumption plan in Azure Functions.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Bicep 94.0%
  • TypeScript 3.5%
  • Dockerfile 2.5%