Cron
#
Deta Cronnote
Cron is a feature of Deta Micros. For the cron to trigger, your code must be deployed on a Micro.
A Deta Micro can be set to run on a schedule from the deta cli
using the deta cron set command.
In order to set a micro to run on a schedule, the micro's code needs to define a function that will be run on the schedule with the help of our library deta
.
The deta
library is pre-installed on a micro and can just be imported directly.
#
Set CronUse deta cron set
to schedule the micro.
You can set the cron in two ways:
#
RateYou can define the rate
at which a micro should run. It is comprised of a value
and unit
.
value
: is a non-zero positive integerunit
: unit of time, can beminute, minutes, hour, hours, day, days
. If thevalue
is1
the unit must beminute
,hour
orday
.
Examples
deta cron set "1 minute"
: Run every minutedeta cron set "2 hours"
: Run every two hoursdeta cron set "5 days"
: Run every five days
#
Cron expressionsCron expressions allow you more flexibility and precision when setting a cron job. Cron expressions have six required fields, which are separated by white space.
Field | Values | Wildcards |
---|---|---|
Minutes | 0-59 | ,-*/ |
Hours | 0-23 | ,-*/ |
Day-of-month | 1-31 | ,-*?/LW |
Month | 1-12 or JAN-DEC | ,-*/ |
Day-of-week | 1-7 or SUN-SAT | ,-*?L# |
Year | 1970-2199 | ,-*/ |
Wildcards
The , (comma) wildcard includes additional values. In the Month field, JAN,FEB,MAR would include January, February, and March.
The - (dash) wildcard specifies ranges. In the Day field, 1-15 would include days 1 through 15 of the specified month.
The (asterisk) wildcard includes all values in the field. In the Hours field, would include every hour. You cannot use * in both the Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other.
The / (forward slash) wildcard specifies increments. In the Minutes field, you could enter 1/10 to specify every tenth minute, starting from the first minute of the hour (for example, the 11th, 21st, and 31st minute, and so on).
The ? (question mark) wildcard specifies one or another. In the Day-of-month field you could enter 7 and if you didn't care what day of the week the 7th was, you could enter ? in the Day-of-week field.
The L wildcard in the Day-of-month or Day-of-week fields specifies the last day of the month or week.
The W wildcard in the Day-of-month field specifies a weekday. In the Day-of-month field, 3W specifies the weekday closest to the third day of the month.
The # wildcard in the Day-of-week field specifies a certain instance of the specified day of the week within a month. For example, 3#2 would be the second Tuesday of the month: the 3 refers to Tuesday because it is the third day of each week, and the 2 refers to the second day of that type within the month.
Limits
You can't specify the Day-of-month and Day-of-week fields in the same cron expression. If you specify a value (or a *) in one of the fields, you must use a ? (question mark) in the other.
Cron expressions that lead to rates faster than 1 minute are not supported.
Examples
deta cron set "0 10 * * ? *"
: Run at 10:00 am(UTC) every daydeta cron set "15 12 * * ? *"
: Run at 12:15 pm(UTC) every daydeta cron set "0 18 ? * MON-FRI *"
: Run at 6:00 pm(UTC) every Monday through Fridaydeta cron set "0 8 1 * ? *"
: Run at 8:00 am(UTC) every 1st day of the monthdeta cron set "0/15 * * * ? *"
: Run every 15 minutesdeta cron set "0/5 8-17 ? * MON-FRI *"
: Run every 5 minutes Monday through Friday between 8:00 am and 5:55 pm(UTC)
#
Code- JavaScript
- Python
With this code deployed on a deta micro, the deta cron set
commands will execute the function based on the cron rate or expression. For example
will set the function to run every 10 minutes. In order to see the execution logs, you can use the visor
#
EventsA function that is triggered from the cron must take an event
as the only argument. The event
will have the following attribute.
event.type
:string
type of an event, will becron
when triggered by a cron event
#
Remove CronUse deta cron remove
to remove a schedule from the micro.
#
Cron and HTTPYou can combine both cron and HTTP triggers in the same deta micro. For this you need to instantiate your app using the deta
library that is pre-installed on a micro.
- JavaScript
- Python
#
IssuesIf you run into any issues, consider reporting them in our Github Discussions.