Pausing Background Work
Pausing background work
An application's website has to answer when someone visits it, but its background work doesn't have to run while nobody does. Turn on Suspend on its page, pick the containers that are background work, such as a queue worker or Horizon, and they stop once the application has been quiet for a while. The website, its database and cache keep running, so a visitor never waits for anything to start, and a visit starts what was paused again within seconds.
This is lpod's suspend, run by systemd's own units, so nothing of Pods' needs to run for it, and it keeps working when Pods itself is off:
| Unit | What it does |
|---|---|
APP-suspend.timer, APP-suspend.service |
Every minute, checks whether the application is quiet, and pauses the containers once it is |
APP-suspend.path, APP-suspend-wake.service |
Only while something is paused: a visit changes the visits file, which starts the paused containers again |
Quiet is no visit for the time you pick (15 minutes by default, from 1min to 30d, such as 1h 30min), counted from when the application started when that's later. With an idle command, it also has to exit 0: lpod runs it with sh -c in the container you pick, and anything else keeps the containers running. For a Laravel application, laravel-podman's php artisan podman:idle checks the queue, Horizon's supervisors and the database for work left. The first line the command prints is the reason the page shows.
Pods itself: Pods' own pods.yml suggests pausing its queue worker, pods-queue, after 15 minutes, with podman:idle as the idle command, so it keeps running while a volume export waits or runs. Starting an export is a visit, so it wakes the queue. Turn it on only when you reach Pods through the Pods proxy: on 127.0.0.1:9393 or through an SSH tunnel, Pods gets no visits there, and a paused queue would never start again.
Visits are a file per application, ~/.local/state/pods/visits/APP.log on the host, which the proxy writes a line to for each visit. The Pods proxy writes it for every route of the application, as Caddy access logs with nothing of the request in them but the time, rolled at 1 MB. Health checks (/up, /health, /healthz, /livez, /readyz and /ping) and uptime monitors such as Uptime Kuma, UptimeRobot and Oh Dear are left out, so they don't keep an application awake. Only the file's modification time counts. The directory is relative to the home of each host's user, visits_directory in Pods' proxy settings, and Pods learns that home from lpod when it checks the host. Until it does, the switch stays off, with a note to update lpod. The proxy mounts that directory through the drop-in Pods writes, pods-proxy.container.d/pods-proxy.conf. A proxy from Pods 1.x doesn't have it until its settings are saved, so Pods writes it, and restarts the proxy, when you turn suspend on and on the next pods:sync. Without it, Caddy would log the visits inside its container, and nothing paused would wake.
What it pauses, and what not:
- Only the containers you picked, never the application's main container. Leave the scheduler out: it barely uses anything, and a paused one skips its tasks.
- It only starts what it paused itself, never a container you stopped, and nothing while the application is stopped. Stop (
APP down) forgets what it paused, so a visit doesn't start what you stopped, and so does turning it off, which starts what's paused first. - A restart of the application, such as for an update, starts what was paused again.
The page shows what's paused and since when, the settings, and the last check with its reason, such as no visit for 15m0s. Pods suggests the containers from the application's pods.yml, or else from its Quadlet files: the ones that run Laravel's queue or Horizon. A Vite dev server isn't suggested: once it stops, Laravel looks for a production build, so the first page after a pause fails. The dialog doesn't offer a container that the main one or another container of the application needs, such as a database it names in Requires=, Requisite=, BindsTo=, PartOf= or StopPropagatedFrom=: systemd would stop that one with it, and lpod refuses to pause it. In a pod, such as Immich, that's the server's database and cache, which the pod itself doesn't need.
The same units work without Pods, from a terminal: lpod APP suspend enable --stdin (see "Suspend" in lpod's docs/commands.md).
From Pods 1.x: suspend replaces on-demand, which slept the whole application behind a systemd socket. On a rootless host with SELinux, the proxy's container can't reach such a socket without opening a port, so a visit through the proxy couldn't wake the application. Pods no longer turns on-demand on or off. pods:sync (daily, or run it yourself) moves each on-demand application once: one with workers pauses them when it's quiet instead, with its idle timeout as the quiet time and its idle check as the idle command, and one without runs all the time. An idle check that ran as another user, such as docker, is left out, since suspend runs the idle command as the container's own user (often root, which could leave files the application can't write), so suspend only goes by visits; add an idle command on the application page once its container's user is right. An application with workers waits for Pods to know the host user's home, which its visits file needs, and when turning suspend on fails, the next pods:sync tries again with the same settings.
