Smeltery
Get started

Deployment

v0.1

systemd: start on boot, restart on crash

/etc/systemd/system/myapp.service:

.env
[Unit]
Description=myapp (Smeltery)
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/srv/myapp
ExecStart=/srv/myapp/myapp serve
Restart=on-failure
RestartSec=2
KillSignal=SIGTERM
TimeoutStopSec=45
UMask=0027
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/srv/myapp/storage /srv/myapp/database
PrivateTmp=true
PrivateDevices=true
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectKernelLogs=true
ProtectControlGroups=true
ProtectClock=true
ProtectHostname=true
RestrictSUIDSGID=true
RestrictNamespaces=true
RestrictRealtime=true
LockPersonality=true
MemoryDenyWriteExecute=true
CapabilityBoundingSet=
AmbientCapabilities=
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
SystemCallArchitectures=native
SystemCallFilter=@system-service
SystemCallFilter=~@privileged @resources

[Install]
WantedBy=multi-user.target
shell
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp
journalctl -u myapp -f
curl -s http://127.0.0.1:8000/up
  • enable starts the app at every boot; --now also starts it now.
  • WorkingDirectory is the app root: .env, public/, storage/ and the SQLite file are found from there.
  • On SIGTERM (systemctl stop / restart) the app stops accepting connections, finishes running requests and stops its agents within SHUTDOWN_TIMEOUT, and exits with 0. TimeoutStopSec (45) gives it that time before systemd kills it.
  • Restart=on-failure restarts the app after a crash and after an exit with an error (code 1: the port is taken, APP_KEY is missing, the database cannot be reached, a boot hook failed), but not after a clean stop. A handler that panics answers 500 and the app keeps running.
  • With a CACHE_STORE that spans processes (database, redis, memcached, file), after a crash the restarted process waits for the dead one's agent leases to expire (WATCHFIRE_LEASE_TTL, 30 s) before it runs those agents again; after a clean stop or restart the leases are released and the agents start at once.
  • Type=simple: the app does not use sd_notify or socket activation.
  • The ProtectSystem / ReadWritePaths lines make the whole file system read-only for the app except storage/ and database/. With PostgreSQL or MySQL on the same machine, add After=postgresql.service (or mysql.service) and drop /srv/myapp/database.
  • UMask=0027: files the process creates are never readable by other users. The lines after PrivateTmp take away what a web app does not use: /home, devices, kernel settings and modules, other address families than Unix sockets, IPv4 and IPv6, every capability, system calls outside @system-service, and memory that is both writable and executable. systemd-analyze security myapp rates the unit. A Smeltery app with SQLite runs under all of them (serving pages, migrations, Watchfire agents, a clean stop on SIGTERM).
  • /up answers 200 OK without a session or the database (see Routes and controllers).

One process or two. serve runs the web server and the Watchfire agents, queue workers and scheduler together: one unit is a complete deployment. To run the background work in its own process (for example to restart the web server without stopping long-running agents), run serve --no-agents and work as two units that replace myapp (myapp-web listens on the same port, so stop and disable myapp first):

shell
sudo systemctl disable --now myapp
sudo cp /etc/systemd/system/myapp.service /etc/systemd/system/myapp-web.service
sudo cp /etc/systemd/system/myapp.service /etc/systemd/system/myapp-work.service

Then change two lines in each copy:

File Description= ExecStart=
myapp-web.service myapp web (Smeltery) /srv/myapp/myapp serve --no-agents
myapp-work.service myapp Watchfire (Smeltery) /srv/myapp/myapp work
shell
sudo systemctl daemon-reload
sudo systemctl enable --now myapp-web myapp-work

Back to one process: sudo systemctl disable --now myapp-web myapp-work, then sudo systemctl enable --now myapp.

Anvil's sockets can have a process of their own as well: see A separate socket process.

The processes coordinate through the cache store's locks when CACHE_STORE (or WATCHFIRE_LOCK_STORE) spans processes: database, redis, memcached, or file on one machine. Then each agent runs in one process at a time (the others hold it in standby and take it over when its process stops or dies), each scheduled tick runs once, and the database queue gives each job to one worker. The web process's dashboard shows and controls the agents of the work process. With memory, array or null every process runs every agent and every tick itself. Details: "Several processes" in Watchfire. agents:logs <name> reads an agent's recent lines from the process that runs it; journalctl -u myapp-work has the full log. work exits with 1 when the app registers no background work. A headless app (no web routes) runs only work; set WATCHFIRE_API_ADDR=127.0.0.1:8001 for its API and the agents:* commands.

↑↓ moveEnter openEsc close