Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 54 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<!--
---
name: Azure Functions C# Timer Trigger using Azure Developer CLI
description: This repository contains an Azure Functions timer trigger quickstart written in C# and deployed to Azure Functions Flex Consumption using the Azure Developer CLI (azd). The sample uses managed identity and a virtual network to make sure deployment is secure by default.
name: Azure Functions TypeScript Timer Trigger using Azure Developer CLI
description: This repository contains an Azure Functions timer trigger quickstart written in TypeScript and deployed to Azure Functions Flex Consumption using the Azure Developer CLI (azd). The sample uses managed identity and a virtual network to make sure deployment is secure by default.
page_type: sample
products:
- azure-functions
- azure
- entra-id
urlFragment: starter-timer-trigger-csharp
urlFragment: starter-timer-trigger-typescript
languages:
- csharp
- typescript
- bicep
- azdeveloper
---
-->

# Azure Functions C# Timer Trigger using Azure Developer CLI
# Azure Functions TypeScript Timer Trigger using Azure Developer CLI

This template repository contains an timer trigger reference sample for functions written in C# (isolated process mode) 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 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](https://github.com/codespaces/badge.svg)](https://codespaces.new/azure-samples/functions-quickstart-dotnet-azd-timer)
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/azure-samples/functions-quickstart-typescript-azd-timer)

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](#run-your-app-from-the-terminal).

Expand All @@ -35,12 +35,9 @@ This codespace is already configured with the required tools to complete this tu
## Prerequisites

- [Azure Storage Emulator (Azurite)](https://learn.microsoft.com/azure/storage/common/storage-use-azurite) - Required for local development with Azure Functions
- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0)
- [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local?pivots=programming-language-csharp#install-the-azure-functions-core-tools)
- [Node.js 18 LTS](https://nodejs.org/en/download/) - Required for TypeScript/JavaScript Azure Functions
- [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local?pivots=programming-language-javascript#install-the-azure-functions-core-tools)
- [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd)
- To use Visual Studio to run and debug locally:
- [Visual Studio 2022](https://visualstudio.microsoft.com/vs/).
- Make sure to select the **Azure development** workload during installation.
- To use Visual Studio Code to run and debug locally:
- [Visual Studio Code](https://code.visualstudio.com/)
- [Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions)
Expand All @@ -52,16 +49,16 @@ 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:

```shell
azd init --template functions-quickstart-dotnet-azd-timer
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:

```shell
git clone https://github.com/Azure-Samples/functions-quickstart-dotnet-azd-timer.git
cd functions-quickstart-dotnet-azd-timer
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.
Expand All @@ -74,23 +71,31 @@ You can initialize a project from this `azd` template in one of these ways:
docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite
```

2. From the `timer` folder, run this command to start the Functions host locally:
2. From the `timer` folder, install dependencies and build the TypeScript code:

```shell
func start
npm install
npm run build
```

3. Wait for the timer schedule to execute the timer trigger.
3. Start the Functions host locally:

4. When you're done, press Ctrl+C in the terminal window to stop the `func.exe` host process.
```shell
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 `timer` app folder in a new terminal.
2. Run the `code .` code command to open the project in Visual Studio Code.
3. In the command palette (F1), type `Azurite: Start`, which enables debugging without warnings.
4. Press **Run/Debug (F5)** to run in the debugger. Select **Debug anyway** if prompted about local emulator not running.
5. Wait for the timer schedule to trigger your timer function.
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

Expand Down Expand Up @@ -124,49 +129,48 @@ azd down

## Source Code

The function code for the timer trigger is defined in [`timerFunction.cs`](./timer/timerFunction.cs).
The function code for the timer trigger is defined in [`timerFunction.ts`](./timer/src/functions/timerFunction.ts).

This code shows the timer function implementation:

```csharp
/// <summary>
/// Timer-triggered function that executes on a schedule defined by TIMER_SCHEDULE app setting.
/// </summary>
/// <param name="myTimer">Timer information including schedule status</param>
/// <param name="context">Function execution context</param>
/// <remarks>
/// 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.
/// </remarks>
[Function("timerFunction")]
public void Run(
[TimerTrigger("%TIMER_SCHEDULE%", RunOnStartup = true)] TimerInfo myTimer,
FunctionContext context
)
{
_logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

if (myTimer.IsPastDue)
{
_logger.LogWarning("The timer is running late!");
```typescript
/**
* 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 isolated worker process C# library uses the [TimerTriggerAttribute](https://github.com/Azure/azure-functions-dotnet-worker/blob/main/extensions/Worker.Extensions.Timer/src/TimerTriggerAttribute.cs) from [Microsoft.Azure.Functions.Worker.Extensions.Timer](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.Timer) to define the function
The TypeScript/JavaScript library uses the [timer trigger](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=javascript-v4%2Cin-process&pivots=programming-language-javascript) from [@azure/functions](https://www.npmjs.com/package/@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.
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.
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. **Dependency Injection**: The function uses dependency injection to get a properly configured logger, following best practices for Azure Functions.
4. **Modern TypeScript**: Uses Azure Functions v4 programming model with TypeScript for enhanced type safety and developer experience.

5. **Isolated Process Mode**: This function runs in isolated process mode, which provides better isolation and more flexibility compared to in-process execution.
5. **Async/Await**: Leverages modern JavaScript async patterns for better performance and readability.

### Configuration

Expand Down
6 changes: 3 additions & 3 deletions azure.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json

name: functions-quickstart-dotnet-azd-timer
name: functions-quickstart-typescript-azd-timer
metadata:
template: functions-quickstart-dotnet-azd-timer@1.0.0
template: functions-quickstart-typescript-azd-timer@1.0.0
services:
api:
project: ./timer/
language: dotnet
language: js
host: function
4 changes: 2 additions & 2 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ module api './app/api.bicep' = {
tags: tags
applicationInsightsName: monitoring.outputs.name
appServicePlanId: appServicePlan.outputs.resourceId
runtimeName: 'dotnet-isolated'
runtimeVersion: '8.0'
runtimeName: 'node'
runtimeVersion: '18'
storageAccountName: storage.outputs.name
enableBlob: storageEndpointConfig.enableBlob
enableQueue: storageEndpointConfig.enableQueue
Expand Down
24 changes: 0 additions & 24 deletions timer.sln

This file was deleted.

Loading