You are currently viewing the new Anvil Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the new Anvil Editor Docs

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 are available on the Personal Plan and above

Scheduled Tasks are for running Background Tasks on the server. To repeatedly run client code in the user’s browser, use the Timer Component.
Scheduled Tasks run in the Published version of your app. When configuring Scheduled Tasks, make sure you’re using the ‘published’ version of your app.

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 Add button sidebar menu in the Sidebar Menu.

Then, select the Scheduled Tasks Service Scheduled Task Icon.

Services list with Scheduled Tasks Service highlighted

Add a Scheduled task

In the Scheduled Tasks tab, click + Add task.

Scheduled Tasks window with `Add Task` button highlighted.

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.

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.

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.`

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.

The Scheduled Tasks tab will now list which environments are running the Scheduled Tasks.

Scheduled Tasks tab with list of environments running Scheduled Tasks

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.

To get started, choose Scheduled Tasks from the Gear Menu.

The Gear Menu with Scheduled Tasks highlighted.

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.

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.

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.

Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.