Scheduling remote cronjobs
A common requirement is to perform some task at a given time every day or week automatically. Modal facilitates this through function schedules.
Let’s say we have a Python module
heavy.py with a function,
# heavy.py def perform_heavy_computation(): ... if __name__ == "__main__": perform_heavy_computation()
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() def perform_heavy_computation(): ...
To activate the schedule, deploy your app, either through the CLI:
modal app deploy --name daily_heavy heavy.py::stub
if __name__ == "__main__": stub.deploy("daily_heavy")
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 Deployments section on the Modal web site.
modal.Period lets you specify an interval
between function calls, e.g.
# runs once every 5 hours def perform_heavy_computation(): ...
# runs at 8 am (UTC) every Monday def perform_heavy_computation(): ...