Azure Functions: Testing TimerTrigger

In Azure Functions, timer triggers are used to auto-run a specific function based on a CRON schedule. For example, we have the following code with the Azure Function App named Shoken:

public static class TimerFunctions
{
  [FunctionName("MyFunction")]
  public static void Run([TimerTrigger("%CRON_EXPR%")]TimerInfo myTimer, ILogger log)
  {
    if (myTimer.IsPastDue) { log.LogInformation("(Timer is running late)"); }
  }
}

This uses the CRON schedule expression CTRON_EXPR in the local.settings.json (if running the Azure Function locally) or in the Azure Function Configuration if running within the Azure Function App.

If this expression has some duration, it can be a pain to wait for its next run to determine if it is running properly. You may wish to use curl or Postman to help speed up the process, especially when running within Azure Portal.

Side note: the use of an App Key via x-functions-key is not required if running the Azure Function locally.

In Azure Portal, make sure Authentication / Authorization is off. This prevents users from being prompted for a login. The functions are still protected with a key.

In Azure Portal, get the _master host key.

This is used to grant access from the remote client.

Postman Headers

Notice the format used for the POST URL:

https://{azure-function-app-name}.azurewebsites.net/admin/functions/{azure-function-name}

Add the x-function-key and value from the _master key. This provides remote access to MyFunction.

Postman Body

Make sure Application/JSON is selected. Of particular importance, you must include the following body or you will receive the error 415 Unsupported Media Type.

{}

If all works as desired, when Send is selected in Postman, the following response is returned: 202 Accepted.

If using curl (this was done from my Windows 10 machine), you can use:

curl -i -X POST -H "Content-Type:application/json" -H "x-functions-key:RsSq1G6L3cB6tjWdKkpVIPFJwljMBw3JsquaT6LRLCrZtWkSI4NpcA==" -d "{}" https://shoken.azurewebsites.net/admin/functions/MyFunction

It will also return a 202 code:

HTTP/1.1 202 Accepted
Request-Context: appId=cid-v1:9261fe16-32da-4135-aac9-0e8d37091ac7
Set-Cookie: ARRAffinity=fa6f515d09aa8852709d18e8f093440d1939e1424f79b892d190a053d8cc258c;Path=/;HttpOnly;Domain=shoken.azurewebsites.net
Date: Wed, 02 Sep 2020 18:18:30 GMT
Content-Length: 0

Now, one thing that got me was that when I first attempted to do the above, I kept receiving the error 404 Not Found from both Postman and Curl. I could browse directly (or change my POST to a GET in Postman and Curl) to https://shoken.azurewebsites.net/admin/functions/MyFunction, and I got a 200 OK response and a successful JSON response about the Azure Function. This told me the endpoint was working as expected. After digging around for a while, I found an interesting URL option that include a /status at the end:

https://shoken.azurewebsites.net/admin/functions/MyFunction/status

In calling this via a GET, I got a JSON response with an error message indicating my CRON expression was invalid. While I had done my research on CRON, I had picked an online tool to build my CRON expression that wasn’t compatible with Azure Functions. Dang it! After fiddling around, I found the format that worked for me was:

{second} {Minute} {Hour} {Day-of-Month} {Month} {Day-of-Week}

It uses asterisks (*) for any value, comma (,) for list value separator, dash (-) for range of values, and divide (/) for step values. There are a number of good websites that provide more details, specific to Azure Functions, including Microsoft Docs.

Azure Portal allows testing Azure Functions via web browser by selecting the Azure Function, selecting Code + Test, selecting Test/Run. It too fails with an improper CRON expression, but it doesn’t provide very useful errors. Depending on Key selected, I got 404 Not Found and 401 Unauthorized errors.

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s