Azure Functions
Azure Functions
1. Introduction
Azure Functions is a server less solution that allows you to write less code, maintain less infrastructure, and save on costs. Instead of worrying about deploying and maintaining servers, the cloud infrastructure provides all the up-to-date resources needed to keep your applications running.
Azure Functions provides "compute on-demand" in two significant ways.
First, Azure Functions allows you to implement your system's logic into readily available blocks of code. These code blocks are called "functions". Different functions can run anytime you need to respond to critical events.
Second, as requests increase, Azure Functions meets the demand with as many resources and function instances as necessary - but only while needed. As requests fall, any extra resources and application instances drop off automatically.
Triggers are what cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers have associated data, which is often provided as the payload of the function.
A Timer trigger lets you run a function on a schedule. The timer trigger is provided in the Microsoft.Azure.WebJobs.Extensions. The following example shows a C# function..
[FunctionName("Function1")]
public void Run([TimerTrigger("%Time%")] TimerInfo myTimer, TraceWriter log, ExecutionContext context)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
Here the %Time% is coming from local.setting.json file. Our local.setting.json file consists these codes bellow
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"Time": "0 */1 * * * *"
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SyncConnectionString": "Server=DIPENJIT-PC;Initial Catalog=DEMO_CMS_AZURE_FUNCTION;MultipleActiveResultSets=true;integrated security=true;ConnectRetryCount=0"
}
}
Azure Functions uses the NCronTab library to interpret NCRONTAB expressions. An NCRONTAB expression is similar to a CRON expression except that it includes an additional sixth field at the beginning to use for time precision in seconds:
{second} {minute} {hour} {day} {month} {day-of-week}
Each field can have one of the following types of values:
Here are some examples of NCRONTAB expressions we can use for the timer trigger in Azure Functions.
| Example | When triggered |
|---|---|
0 */5 * * * * | once every five minutes |
0 0 * * * * | once at the top of every hour |
0 0 */2 * * * | once every two hours |
0 0 9-17 * * * | once every hour from 9 AM to 5 PM |
0 30 9 * * * | at 9:30 AM every day |
0 30 9 * * 1-5 | at 9:30 AM every weekday |
0 30 9 * Jan Mon | at 9:30 AM every Monday in January |
2. Implemented Code:
Our example project consists the following code in the Function1.cs file
using System;
using DemofunctionApp.Services.CronService;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace DemofunctionApp
{
public class Function1
{
private readonly IDataCronService _dataCronService;
public Function1(IDataCronService dataCronService)
{
_dataCronService = dataCronService;
}
[FunctionName("Function1")]
public void Run([TimerTrigger("%Time%")] TimerInfo myTimer, TraceWriter log, ExecutionContext context)
{
_dataCronService.PoolingBatchProcess(context, log);
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
}
IDataCronService is an interface injected into the Function.
Comments
Post a Comment