Scheduled Tasks
Sometimes you want to run server functions at particular times, regardless of user activity on your app. For example, you might need to run analytics on your Data Tables every night, or send out a daily email digest. For users on paid plans, Scheduled Tasks let you do just that - you can choose to run any Background Task every minute, hour, day, week, or month, on a schedule you configure.
Scheduled Tasks launch standard Background Tasks, as if you had launched them manually using anvil.server.launch_background_task(...)
. You can see the launched Background Tasks in the Background Tasks dialog, and find output in the App Logs
In a typical use-case, we may have defined two Background Tasks in a Server Module:
poll_sensor
collects data from a remote sensor (probably using the Uplink).send_email_digest
sends a summary email, and should be run once a week.
To get started, add the Scheduled Tasks Service to your app by clicking the blue plus button in the Sidebar Menu.
![](../img/add-new-service-new-ide.png)
Then, select the Scheduled Tasks Service .
![Services list with Scheduled Tasks Service highlighted](img/scheduled-tasks/add-scheduled-tasks-service-new-ide.png)
Add a Scheduled task
In the Scheduled Tasks tab, click + Add task
.
![Scheduled Tasks window with `Add Task` button highlighted.](img/scheduled-tasks/add-a-scheduled-task-new-ide.png)
Choose poll_sensor
from the Task to Run
drop-down menu, confirm that we want to run it every 10 minutes, then click the Add
button.
![A dialog saying 'Run poll_sensor every 10 mintes'. The 'poll_sensor', '10' and 'minutes' can be changed.](img/scheduled-tasks/scheduled-tasks-create-1-new-ide.png)
Next, Choose send_email_digest
from the Task to Run
drop-down menu, and configure it to run every Monday at 8am. Note that all Scheduled Tasks are configured in UTC - the same time zone your Server Modules run in.
![A dialog saying 'Run send_email_digest every 1 week on Monday at 08:00 UTC'. The important variables can be changed.](img/scheduled-tasks/scheduled-tasks-create-2-new-ide.png)
Configure environments
You might want different environments to run different Scheduled Tasks. You might not want to run Scheduled Tasks in every environment – for example, your development environment might not need to send “weekly digest” emails to all users.
To select which environments run Scheduled Tasks, click on Configure Environments
in the Scheduled Tasks tab.
![Scheduled Tasks tab with the `Configure environments button highlighted.`](img/scheduled-tasks/configure-environments.png)
Then select Show advanced settings
for the Environment you’d like to run your Scheduled Tasks and tick Run scheduled tasks
.
![Environments window with `Show advanced settings` button highlighted.](img/scheduled-tasks/environments-show-advanced.png)
The Scheduled Tasks tab will now list which environments are running the Scheduled Tasks.
![Scheduled Tasks tab with list of environments running Scheduled Tasks](img/scheduled-tasks/scheduled-tasks-tab-listing-environments.png)
See when a task last ran
Click on a Scheduled Task to see when it last ran, and when it will next run.
![The Scheduled Tasks dialog showing the poll_sensor task set to run every 10 minutes.](img/scheduled-tasks/scheduled-tasks-view-new-ide.png)
To get started, choose Scheduled Tasks from the Gear Menu.
![The Gear Menu with Scheduled Tasks highlighted.](img/scheduled-tasks/scheduled-tasks-menu.png)
Choose poll_sensor
from the drop-down menu, confirm that we want to run it every 10 minutes (or click the parameters to change), then add the task with the green “” button.
![A dialog saying 'Run poll_sensor every 10 mintes'. The 'poll_sensor', '10' and 'minutes' can be changed.](img/scheduled-tasks/scheduled-tasks-create-1.png)
Next, Choose send_email_digest
from the drop-down menu, and configure it to run every Monday at 8am. Note that all Scheduled Tasks are configured in UTC - the same time zone your Server Modules run in.
![A dialog saying 'Run send_email_digest every 1 week on Monday at 08:00 UTC'. The important variables can be changed.](img/scheduled-tasks/scheduled-tasks-create-2.png)
Click on a Scheduled Task to see when it last ran, and when it will next run.
![The Scheduled Tasks dialog showing the poll_sensor task set to run every 10 minutes.](img/scheduled-tasks/scheduled-tasks-view.png)
Do you still have questions?
Our Community Forum is full of helpful information and Anvil experts.