# Automation

Automation saves money by acting for you. You can set rules on a single server, and keep a group of servers at a size you choose.

## Rules for one server

Open a server, go to **Settings**, and find **Automation**. You can also set the same rules in the last step of **Deploy**, under **Automation (optional)**. Turn on any of these:

- **Stop after a running time.** The server stops after it has run for the hours you set. Starting it again begins a new run.
- **Stop when the GPU is idle.** The server stops when GPU use stays under a percentage for the minutes you set (at least 10). It needs fresh usage readings across the whole period, so a server that has just started, or one that reports nothing, is never stopped by this rule.
- **Destroy at a date and time.** The server and its disk are deleted at that moment. This cannot be undone.
- **Spending limit.** The server stops when what it has been charged over its whole life reaches the amount you set. The smallest limit is 0.10 USD (10 cents). A server you start again after that stops again, so raise or clear the limit first.
- **Weekly schedule.** Pick days, a start time and a stop time in a time zone. The server is started at the start time and stopped at the stop time. Times follow the clock of that zone, including daylight saving changes. A stop time earlier than the start time ends the next morning. The schedule acts only at those moments, so a server you start or stop by hand in between is left alone.

Every time a rule acts, the server's **Activity** shows it and an entry is kept in the account log. You also get a notice: a stop time, the maximum running time, the idle rule, the spending limit and a destroy each send one email (and an in-app notice if you turned those on). Starts and stops by the weekly schedule happen at times you chose, so they only show as in-app notices, never as emails. Choose the channels under **Settings > Notifications**, row **Servers**. Starting a server by schedule needs enough balance, like any start.

Charges that have not yet been booked (up to a minute or two) are counted against the spending limit, so the stop happens close to the amount you set.

### Add the timetable to your calendar

Under the rules, **Add to calendar (.ics)** downloads a calendar file with the server's planned starts and stops for the next four weeks: one event per weekly-schedule window, and the stop or destroy time when one is set. Import it into your calendar app. The file is a snapshot of the saved rules, so download it again after a change. Rules without a fixed time (running time, idle, spending limit) are not in it. Through the API: `GET /api/v1/servers/{id}/automation/calendar?weeks=8` (1 to 12 weeks); the CLI: `apidly automation calendar ID --out server.ics`.

## Server groups

A group keeps a number of servers of one GPU, template, disk and set of SSH keys. Set the number you want, and optionally a minimum and a maximum. Each minute the group:

- requests new servers when it has fewer than wanted, at most two per minute. These requests follow the same rules as any other request: approval, balance check and price check.
- wakes stopped members first, because their disks are kept.
- stops (or destroys, as you chose) the newest members when it has more than wanted.
- removes a server that failed and requests a replacement.

You can set a weekly schedule on a group. Inside a window the group keeps the count of that window; outside every window it falls to its minimum (zero if unset). You can also set a highest hourly price: the group does not request servers while the plan costs more.

A group pauses itself and emails you when your balance would last less than one hour, or when three requests in a row fail. Running servers keep running. Fix the cause and press **Resume**. Deleting a group keeps its servers; they just stop being managed.

To stop a whole group, set **Servers wanted** and the minimum to zero with **Stop the newest** chosen: the disks are kept, and when you raise the number again the stopped servers start first. With **Destroy the newest**, the servers and their disks are deleted.

Groups are also available through the API under `/v1/groups`.
