Scheduling remote cron jobs
A common requirement is to perform some task at a given time every day or week automatically. Modal facilitates this through function schedules.
Basic scheduling
Let’s say we have a Python module heavy.py
with a function,
perform_heavy_computation()
.
# heavy.py
def perform_heavy_computation():
...
@stub.local_entrypoint()
def main():
perform_heavy_computation.call()
To schedule this function to run once per day, we create a Modal Stub and attach
our function to it with the @stub.function
decorator and a schedule parameter:
# heavy.py
import modal
stub = modal.Stub()
@stub.function(schedule=modal.Period(days=1))
def perform_heavy_computation():
...
To activate the schedule, deploy your app, either through the CLI:
modal deploy --name daily_heavy heavy.py
Or programmatically:
if __name__ == "__main__":
modal.runner.deploy_stub(stub)
When you make changes to your function, just rerun the deploy command to overwrite the old deployment.
Monitoring your scheduled runs
To see past execution logs for the scheduled function, go to the Apps section on the Modal web site.
Schedules currently cannot be paused. Instead the schedule should be removed and the app redeployed. Schedules can be started manually on the app’s dashboard page, using the “run now” button.
Schedule types
There are two kinds of base schedule values -
modal.Period
and
modal.Cron
.
modal.Period
lets you specify an interval
between function calls, e.g. Period(days=1)
or Period(hours=5)
:
# runs once every 5 hours
@stub.function(schedule=modal.Period(hours=5))
def perform_heavy_computation():
...
modal.Cron
gives you finer control using
cron syntax:
# runs at 8 am (UTC) every Monday
@stub.function(schedule=modal.Cron("0 8 * * 1"))
def perform_heavy_computation():
...
For more details, see the API reference for Period, Cron and Function