Azure Function Timer Trigger not firing - NCrontab

Azure Function Timer Trigger not firing - NCrontab
Understanding Azure Function Timer Triggers

If you've recently ventured into the world of Azure Functions, you're likely familiar with the versatility they offer when it comes to scheduling tasks. In this guide, we'll delve into using Azure Functions' Time Trigger function with Node.js and Visual Studio Code, uncovering some important nuances along the way.

This article assumes that you have some familiarity with Node.js and a basic understanding of Azure.

Getting Started

To begin, you'll need Node.js and Visual Studio Code installed, along with the Azure Functions extension. When you create a new function, it comes with some boilerplate code that includes a schedule property defining when the function should run. By default, it's set to run every 5 minutes.

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

app.timer('timerTrigger2', {
    schedule: '0 */5 * * * *',
    handler: (myTimer, context) => {
        context.log('Timer function processed request.');

The schedule parameter in the above code tells the Azure Function how often to fire.

The Challenge

But what if you want your function to run every 11 hours during a day? Crafting the right Cron expression can be tricky, and that's where I encountered some difficulties. I turned to an online Cron expression generator, hoping it would simplify the process. Here's what it gave me:

0 0 */11 * *

Feeling confident, I integrated this expression into my code above and deployed the Azure Function. However, it didn't work as expected. I scoured the logs for clues but found nothing. To troubleshoot and debug locally, I temporarily changed the Cron expression to run every minute, and it worked flawlessly. So why wasn't it firing every few hours?

The Revelation

After some online research, I uncovered the reason behind the issue. Azure Functions use an NCRONTAB expression, which requires a six-part format instead of the traditional five-part Cron expression. The online generator that I found online had provided a five-part expression, leading to the problem.

To be fair, the Visual Studio Code extension for Azure Functions provides the correct six-part format by default. My mistake was changing it using a five-part expression from an online tool - d'oh!

The Solution

To rectify this, I recommend using the NCrontab Expression Tester. This user-friendly tool not only helps you test your expressions but also generates the correct six-part Cron expressions tailored for Azure Functions.

When I updated my code to use the 6 part format:

0 0 */11 * * *

It ran perfectly!

Why NCrontab?

You might be wondering why Azure Functions use the NCrontab six-part format instead of the traditional five-part format. The answer lies in its flexibility. The six-part format allows you to specify seconds, enabling you to run your functions with higher precision and frequency.

* * * * * *
- - - - - -
| | | | | |
| | | | | +--- day of week (0 - 6) (Sunday=0)
| | | | +----- month (1 - 12)
| | | +------- day of month (1 - 31)
| | +--------- hour (0 - 23)
| +----------- min (0 - 59)
+------------- sec (0 - 59)

For further details on NCrontab expressions, you can visit the GitHub repository here.

Happy coding!