Schedules represent a time frame to repeatedly run a Modal function.
modal.Cron
Cron jobs are a type of schedule, specified using the Unix cron tab syntax.
Constructor
modal.Cron(
cron_string: str,
timezone: str = "UTC",
)
Construct a schedule that runs according to a cron expression string.
Cron expression string (e.g., “0 9 * * *”).
Timezone for the schedule (e.g., “America/New_York”).
Usage
import modal
app = modal.App()
@app.function(schedule=modal.Cron("* * * * *"))
def f():
print("This function will run every minute")
Examples
# Run at 4:05am UTC every night
modal.Cron("5 4 * * *")
# Run every Thursday at 9am UTC
modal.Cron("0 9 * * 4")
# Run daily at 6am New York time (accounts for daylight saving)
modal.Cron("0 6 * * *", timezone="America/New_York")
modal.Period
Create a schedule that runs every given time interval.
Constructor
modal.Period(
*,
years: int = 0,
months: int = 0,
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: float = 0,
)
Construct a period-based schedule.
Number of years between runs.
Number of months between runs.
Number of weeks between runs.
Number of days between runs.
Number of hours between runs.
Number of minutes between runs.
Number of seconds between runs. Only this parameter can be a float.
Usage
import modal
import math
app = modal.App()
@app.function(schedule=modal.Period(days=1))
def f():
print("This function will run every day")
# More examples
modal.Period(hours=4) # Runs every 4 hours
modal.Period(minutes=15) # Runs every 15 minutes
modal.Period(seconds=math.pi) # Runs every 3.14159... seconds
Notes
Only seconds can be a float. All other arguments are integers.
days=1 will trigger the function the same time every day. This does not have the same behavior as seconds=86400 since days have different lengths due to daylight savings and leap seconds.Similarly, using months=1 will trigger the function on the same day each month.
This behaves similar to the dateutil package.