>> Hi everyone. My name is Marie and
I'm an Engineer on the Azure Functions team.
In this video, I'm going to be talking about
Cron Jobs and Timers in Serverless.
A Cron Job is a Linux commands that is used for
scheduling tasks to be executed sometime in the future.
Timer triggers are what we use in
Azure Functions to create functions that run on a schedule.
Cron Jobs in Azure functions are called Timer-triggered functions.
Conceptually, they're pretty much the same.
Timer triggered functions are great for automation.
You can write functions that clean up resources,
start and stop VMs on a schedule,
create recurring notifications or resources,
sync resources in off hours,
implement a polling service,
or help it with ETL pipelines.
When you think about Serverless,
you probably think about
near-infinite auto scale and bursty unpredictable load.
How does a regularly scheduled function fit into this picture?
The first immediate benefit from
Serverless timer functions is that functions are built per use.
You only have to pay for
the compute power while your function is executing.
If you have a job that runs once a day,
you won't have to pay for
all that time that the function is waiting idly by.
The second benefit of using Azure Functions for
a scheduled job is that
the programming model makes it really easy to use.
You don't have to go and implement your own timer and
deal with all the concerns of trying to
implement something that will continue working on
a schedule even in the face of failures.
Let's see what this actually looks like in code.
In this demo, I'll be running
a Timer-triggered Function to
monitor the health of some API endpoint.
Through this demo, I mainly want to show you what
a Timer triggered function looks
like so that you can write your own.
A fundamental part of
a Timer-triggered Function that every timer function
has is this timer trigger type binding definition
in function.json.
This file was created using
the Azure Functions VS code extension
when creating a new function.
My function is scheduled to run according to this Cron expression.
The expected Cron expression has
six components corresponding to second,
minute, hour, day, month,
and day of week.
I'll cover what the numbers,
slashes, and asterisk mean later in this video.
But the way to read this schedule
is that it runs every five seconds.
My function code that will run
every five seconds just pings an endpoint,
measures the time taken in that call,
and then logs that time.
It also logs if there are any 500 status codes.
I'm going to run this with func
start although I could also run this by pressing "F5".
One thing that I did before having this actually run
was to add in local.settings.json,
a connection string for Azure web job storage.
This connection string is a connection string to
an Azure storage account which is
required to use Timer-triggered Functions.
Now that my function is up and running,
and it's struggling a bit because of all of
the recording that is happening on my laptop.
But you can see that it is running every five seconds
and it is measuring the time taken for that call to the endpoint.
There's more I could do with
this health checker function to use this data I'm now gathering.
For example, I could build alerting on top of
the logs from this function that get pushed application insights.
I'll leave that for another time though.
One last thing that I want to show you is that
Serverless Timer-triggers execute as singletons,
meaning that for a single timer,
you will never have two overlapping executions.
If my timer here is configured to run every second,
but my function code takes five seconds,
I'll only see one function execution every five seconds.
Here I have this code here that is sleeping for five seconds,
and then in my function json,
the star over one means that it's executing every single second.
Now, let's watch it run.
It's only executing every five seconds,
even though my configuration says one second because
those previous executions are
overlapping into the next one's time.
This can be helpful if your executions are
modifying the same external resource.
For example, if you need to get around this behavior though,
you can with durable functions.
Like I mentioned earlier in the demo,
those six components for a Cron expression
that will define the schedule
have different types thrown in there,
like numbers, slashes and stars.
I'm going to leave this chart out there,
and I won't go through each of these in detail,
but you can pause this video and read through it.
There's also information and documentation available
on the official Azure Functions docs for timer triggers.
From this video, I hope that you took away that
Serverless Timer-triggered Functions are easy to use,
and they're powerful for any automation that you need to do.
Thank you so much for watching
and check out some of the links below.
[MUSIC]