#Running eLucid8 as a service
elucid8 serve keeps one deployment loaded and answers requests as they arrive (The long-running service). With --http it takes them over HTTP from any number of clients. This page shows how to run it for others to use: in a container with Docker, under systemd on Linux or launchd on macOS, behind nginx or Caddy for HTTPS, with health checks, Prometheus and logs, and how to stop, upgrade and back it up.
What the service lets others do, and what to keep private, is summed up in Is it safe to run?.
Everything below uses the guide's program, a running sum over six readings, as the deployment. Replace its files with your own.
#1. A deployment and a token
A deployment is a folder holding the compiled plan, deploy.json and the data it names (Deploy tutorial, Command reference). Here, deploy/:
File deploy/running.el8:
-- A running sum, and a ratio that fails where x is 0.
x = input("x", t : 1000)
running = x fby.t (running + next.t x)
ratio = running / x
File deploy/deploy.json:
{ "format": "elucid8-deploy/2",
"inputs": { "x": { "from": "running-x.csv" } } }
File deploy/running-x.csv:
t,value
0,3
1,1
2,4
3,0
4,5
5,9
Compile the program into deploy/plan.json (or download a saved program from the playground, which gives the plan):
curl -sS --fail-with-body --data-binary @deploy/running.el8 -o deploy/plan.json https://elucid8-lang.org/api/compile
A service that listens on anything but the loopback address needs a bearer token: every request but GET /health must carry it (Authorization: Bearer …). Keep it in a file of its own, never in deploy.json or on a command line:
openssl rand -hex 32 > token
#2. Docker
The image holds the elucid8 command for Linux (a static binary, no other library) on Alpine Linux, and runs elucid8 serve as a user of its own (uid 10001) with:
- the deployment folder at
/deploy(/deploy/plan.json,/deploy/deploy.json); - the token at
/run/secrets/elucid8_token(a mounted file, or a Docker secret); - HTTP on port 8080, on every address in the container;
- a health check on
/health.
#Building the image
Put the Linux binary from the Download page beside the Dockerfile, as elucid8-linux-x64 (Intel and AMD) or elucid8-linux-arm64 (ARM, Apple silicon), and build:
docker build -t elucid8 .
With both binaries beside it, docker buildx build --platform linux/amd64,linux/arm64 -t elucid8 . builds the image for both. The image is about 7 MB to download and 14 MB unpacked (Alpine is about 9 MB of that, unpacked).
File Dockerfile:
# The elucid8 command in a container, serving a deployment over HTTP (docs/running-as-a-service.md).
#
# Build it from the static Linux binary of the elucid8 command (from the Download page, or built with
# `cargo build --release -p elucid8-runtime --bin elucid8 --target <arch>-unknown-linux-musl`), put
# beside this file as `elucid8-linux-x64` (x86-64) or `elucid8-linux-arm64` (ARM64):
#
# docker build -t elucid8 . # for this machine's architecture
# docker buildx build --platform linux/amd64,linux/arm64 -t elucid8 . # both (both binaries beside it)
#
# Run it with the deployment folder (plan.json, deploy.json and its data) at /deploy and a token file as a
# secret at /run/secrets/elucid8_token:
#
# docker run --init -p 8080:8080 -v "$PWD/deploy:/deploy:ro" -v "$PWD/token:/run/secrets/elucid8_token:ro" elucid8
#
# The base is Alpine, not `scratch`: the command needs nothing from it (it is statically linked, and
# makes no TLS connection itself), but programs that serve sources or answer as services
# (`"process": [...]` in deploy.json) need a shell, certificates and an interpreter, and the health check
# needs `wget`. Alpine gives a shell, busybox's `wget` and the CA certificates in about 9 MB; add Python or Node
# in an image built FROM this one when a deployment's programs need them.
FROM alpine:3.22 AS binary
ARG TARGETARCH
COPY elucid8-linux-* /tmp/
RUN case "$TARGETARCH" in \
amd64) arch=x64 ;; \
arm64) arch=arm64 ;; \
*) echo "no elucid8 binary for $TARGETARCH" >&2; exit 1 ;; \
esac \
&& test -f "/tmp/elucid8-linux-$arch" || { echo "put elucid8-linux-$arch beside the Dockerfile" >&2; exit 1; } \
&& install -m 0755 "/tmp/elucid8-linux-$arch" /elucid8
FROM alpine:3.22
# A user of its own, with no login and no home: the service needs to read the deployment and, if a name
# map saves (deploy.json "names"), to write that file.
RUN addgroup -S -g 10001 elucid8 && adduser -S -D -H -h /deploy -s /sbin/nologin -u 10001 -G elucid8 elucid8
COPY --from=binary /elucid8 /usr/local/bin/elucid8
WORKDIR /deploy
USER elucid8:elucid8
EXPOSE 8080
# `elucid8 serve` stops cleanly on SIGTERM (what `docker stop` sends): the requests it has received are
# answered, subscriptions end, and it exits 0. Give it as long as the longest request (docker stop -t).
STOPSIGNAL SIGTERM
# /health answers 200 when the deployment is loaded and the service takes requests, 503 while it loads or
# stops; it needs no token.
HEALTHCHECK --interval=10s --timeout=3s --start-period=60s --retries=3 \
CMD wget -q -O /dev/null http://127.0.0.1:8080/health || exit 1
ENTRYPOINT ["elucid8"]
CMD ["serve", "/deploy/plan.json", "--config", "/deploy/deploy.json", "--http", "0.0.0.0:8080", "--token-file", "/run/secrets/elucid8_token"]
Why Alpine and not scratch. The command needs nothing from the image. But a deployment whose inputs come from programs ("process": ["python3", …]) needs a shell and an interpreter, programs that call HTTPS services need CA certificates, the health check needs wget, and a shell helps when something goes wrong. Build an image FROM elucid8 that adds what your programs need (RUN apk add --no-cache python3).
The user. The service runs as uid 10001, so the token file and the deployment must be readable by it. With Docker Desktop (macOS, Windows) mounted files are; on Linux, give the token to that user (sudo chown 10001 token && chmod 600 token), or use a Docker secret.
#Running it
docker run -d --name elucid8 --init -p 127.0.0.1:8080:8080 -v "$PWD/deploy:/deploy:ro" -v "$PWD/token:/run/secrets/elucid8_token:ro" elucid8
curl -s http://127.0.0.1:8080/health
curl -s -H "Authorization: Bearer $(cat token)" -H "Content-Type: application/json" http://127.0.0.1:8080/query -d '{"id": 1, "query": "running[t = 0..5]"}' | jq -c '[.results[].outcome.value]'
docker stop elucid8
{"status":"ready","protocol":"elucid8-serve/1"}
[3,4,8,8,13,22]
The same answers elucid8 run plan.json --config deploy.json --query 'running[t = 0..5]' gives. -p 127.0.0.1:8080:8080 publishes the port on this machine only; put HTTPS in front (§5) before publishing it further. --init runs a small init as process 1, which collects the children of programs that serve inputs or answer them on demand when they exit; elucid8 handles signals itself.
docker stop sends SIGTERM: the service stops taking requests, answers those it has (§7), and exits 0. Docker waits 10 seconds before SIGKILL; give it longer if requests may take longer (docker stop -t 60, or stop_grace_period below). docker logs elucid8 shows its log lines (§6).
#Docker Compose
File compose.yaml (beside the Dockerfile, the binary, deploy/ and token):
# elucid8 serve in a container (docs/running-as-a-service.md). Beside this file: the Dockerfile and the
# elucid8 binary for Linux (elucid8-linux-x64 or elucid8-linux-arm64), the deployment folder deploy/
# (plan.json, deploy.json and its data) and a file `token` holding the bearer token.
#
# docker compose up -d # builds the image the first time
# docker compose logs -f # the service's log lines
# docker compose down # SIGTERM: the requests received are answered, then it exits 0
services:
elucid8:
image: ${ELUCID8_IMAGE:-elucid8}
build: .
# A small init as process 1, which reaps the children of programs that serve sources or answer as
# services; elucid8 itself handles SIGTERM.
init: true
ports:
- "127.0.0.1:${ELUCID8_PORT:-8080}:8080"
volumes:
- ./deploy:/deploy:ro
secrets:
- elucid8_token
# As long as the longest request may take: `docker compose down` waits this long before SIGKILL.
stop_grace_period: 60s
restart: unless-stopped
mem_limit: 4g
logging:
driver: json-file
options:
max-size: "50m"
max-file: "5"
secrets:
elucid8_token:
file: ./token
docker compose up -d
docker compose ps
docker compose down
restart: unless-stopped starts the service again if it stops for any other reason than docker compose down or docker stop. The deployment is mounted read-only (:ro); drop :ro if a name map saves ("save": true in deploy.json), which writes the map back into the folder.
#3. systemd (Linux)
Put the elucid8 binary at /usr/local/bin/elucid8, the deployment at /srv/elucid8/ and the token at /etc/elucid8/token (readable by root only: systemd hands it to the service), make a user for it, and install the unit:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin elucid8
sudo install -m 0644 elucid8.service /etc/systemd/system/elucid8.service
sudo systemctl daemon-reload
sudo systemctl enable --now elucid8
journalctl -u elucid8 -f
File elucid8.service:
[Unit]
Description=eLucid8 service (elucid8 serve)
Documentation=https://elucid8-lang.org/learn/running-as-a-service
After=network-online.target
Wants=network-online.target
[Service]
Type=exec
User=elucid8
Group=elucid8
WorkingDirectory=/srv/elucid8
# The token as a credential: systemd copies it to a private directory (%d) only this service can read.
LoadCredential=token:/etc/elucid8/token
ExecStart=/usr/local/bin/elucid8 serve /srv/elucid8/plan.json --config /srv/elucid8/deploy.json --http 127.0.0.1:8080 --token-file %d/token
# A new plan or new data without a restart (the service reads deploy.json and the plan again);
# `$$` is a `$` for systemd.
ExecReload=/bin/sh -c 'curl -fsS -H "Authorization: Bearer $$(cat %d/token)" -H "Content-Type: application/json" -d {} http://127.0.0.1:8080/reload'
# SIGTERM stops it cleanly: the requests it has received are answered, then it exits 0.
KillSignal=SIGTERM
TimeoutStopSec=90
Restart=on-failure
RestartSec=2
# Standard input is /dev/null: with --http the service carries on; standard error (its log) goes to the journal.
StandardError=journal
# Limits worth setting (section 9).
MemoryMax=4G
LimitNOFILE=4096
# Hardening: it reads its folder and writes nothing else (add ReadWritePaths=/srv/elucid8 if a name map saves).
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
[Install]
WantedBy=multi-user.target
systemctl stop elucid8 sends SIGTERM and waits TimeoutStopSec before SIGKILL; systemctl reload elucid8 asks the service to read deploy.json and the plan again (§8). Restart=on-failure starts it again after a crash or a non-zero exit, not after systemctl stop. %d is the directory of the service's credentials (systemd 250 or later; on older versions write ${CREDENTIALS_DIRECTORY}). Without --http, closing standard input stops the service: under systemd, always give --http.
#4. launchd (macOS)
A LaunchAgent runs it when you log in (a LaunchDaemon in /Library/LaunchDaemons runs it at boot, as a user you name with UserName). Put the binary at /usr/local/bin/elucid8 and the deployment and token in ~/elucid8/, replace YOU with your user name, then:
cp org.elucid8-lang.serve.plist ~/Library/LaunchAgents/
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/org.elucid8-lang.serve.plist
launchctl print gui/$(id -u)/org.elucid8-lang.serve
launchctl bootout gui/$(id -u)/org.elucid8-lang.serve
File org.elucid8-lang.serve.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.elucid8-lang.serve</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/elucid8</string>
<string>serve</string>
<string>plan.json</string>
<string>--config</string>
<string>deploy.json</string>
<string>--http</string>
<string>127.0.0.1:8080</string>
<string>--token-file</string>
<string>token</string>
</array>
<key>WorkingDirectory</key>
<string>/Users/YOU/elucid8</string>
<key>RunAtLoad</key>
<true/>
<!-- Started again if it stops with an error; not after a clean stop. -->
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<!-- launchctl bootout sends SIGTERM, then SIGKILL after this many seconds. -->
<key>ExitTimeOut</key>
<integer>90</integer>
<key>StandardErrorPath</key>
<string>/Users/YOU/elucid8/elucid8.log</string>
</dict>
</plist>
#5. HTTPS in front
elucid8 serve speaks plain HTTP. For anything beyond one machine, put a proxy in front that holds the certificate and passes requests to 127.0.0.1:8080. Two settings matter: subscriptions are event streams (text/event-stream), which the proxy must pass on as they come and keep open (the service asks proxies not to buffer them, with X-Accel-Buffering: no, and sends a comment every 15 seconds while a stream is idle); and request bodies (rows sent with POST /inputs/NAME/rows) can be large.
#nginx
File elucid8.conf (in /etc/nginx/conf.d/):
server {
listen 443 ssl;
server_name elucid8.example.com;
ssl_certificate /etc/ssl/elucid8.example.com.crt;
ssl_certificate_key /etc/ssl/elucid8.example.com.key;
# The service's own limit is 64 MiB (http.maxBodyBytes).
client_max_body_size 64m;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Connection "";
# Subscriptions are event streams: pass them on as they come, and keep them open.
proxy_buffering off;
proxy_read_timeout 1h;
}
}
#Caddy
Caddy gets and renews the certificate itself, and passes event streams on as they come.
File Caddyfile:
elucid8.example.com {
request_body {
max_size 64MB
}
reverse_proxy 127.0.0.1:8080
}
The Authorization header passes through both unchanged, so the token is still the service's check. Keep the service's own address on loopback (--http 127.0.0.1:8080, or the container's port published on 127.0.0.1 only), so that the proxy is the only way in.
#6. Health, metrics and logs
Health. GET /health needs no token and answers at once, while a query runs: 200 {"status":"ready","protocol":"elucid8-serve/1"} when the service takes requests; 503 {"status":"loading",…} while the deployment loads; 503 {"status":"stopping",…} after a shutdown or a signal. The image's HEALTHCHECK uses it, and so can a load balancer or Kubernetes' readiness probe.
Metrics. GET /metrics (with the token) answers in the Prometheus text format: requests by status, query times (a histogram), the caches' size and entries, versions held and dropped, subscriptions, evaluations and service calls, elucid8_ready, elucid8_requests_waiting (requests taken in and not yet answered), connections and HTTP responses by endpoint and status (Over HTTP). A Prometheus scrape with the token from a file:
File prometheus.yml:
scrape_configs:
- job_name: elucid8
scrape_interval: 15s
authorization:
type: Bearer
credentials_file: /etc/prometheus/elucid8-token
static_configs:
- targets: ["127.0.0.1:8080"]
Values. From 0.19.0, GET /values answers the program's own values in the same format: the queries the configuration's http.values names, each cell a time series, for a Grafana dashboard of the results themselves (Seeing your results). Unlike /metrics, a scrape of /values takes its turn with the other requests.
Worth an alert: elucid8_ready == 0 for longer than the deployment takes to load; elucid8_requests_waiting growing (requests arrive faster than they are answered: one is answered at a time); elucid8_requests_total{status="overBudget"} rising; elucid8_cache_bytes near elucid8_cache_budget_bytes.
Logs. One JSON line per request on standard error (the journal under systemd, docker logs in a container), and one per event (Logs):
{"event":"listening","address":"0.0.0.0:8080","token":true}
{"event":"ready","plan":"/deploy/plan.json","runtime":"elucid8/0.18.0","protocol":"elucid8-serve/1"}
{"client":"172.17.0.1:62068","endpoint":"POST /query","id":1,"op":"query","query":"running","points":6,"versions":{"x":"d20b5fc4a3d402cb"},"ms":0.074,"evaluations":6,"computed":6,"cacheHits":0,"status":"done"}
{"event":"signal","signal":"SIGTERM","then":"shutdown"}
{"client":"SIGTERM","endpoint":"signal","id":null,"op":"shutdown","status":"done"}
{"event":"stopped"}
Each request's line has the client's address, the endpoint, the request's id and operation, the versions it read, its time and work, and its status. Refused HTTP requests have a line each ("event":"http", with the status). Logs never hold answers, data, inputs to services, environment variables or the token.
#7. Stopping
SIGTERM (what docker stop, systemctl stop and launchctl bootout send) or SIGINT (Ctrl-C) stops the service as a shutdown request does: it takes nothing more (HTTP answers 503, /health says stopping), answers the requests it has already received, runs the subscription answers already queued, gives each subscription its last reply ("status":"ended"), and exits 0. A signal while the deployment loads stops it once it has loaded. A second signal stops it at once: the running request and every waiting one are answered cancelled.
So give the stop as long as the longest request takes (docker stop -t, stop_grace_period, TimeoutStopSec, ExitTimeOut); after that the process is killed and the requests still waiting get no answer (their clients see the connection close). A request can be given a time limit of its own (budgetMs, or http.budgetMs for every HTTP request).
#8. Upgrading, reloading and backing up
A new plan or new data, without stopping. Replace the files in the deployment folder, then ask the service to read deploy.json and the plan again: POST /reload (systemctl reload elucid8 with the unit above). An unchanged plan keeps the caches; a changed one starts afresh; a reload that cannot read its files changes nothing and says why (reload). New rows can also be sent as they arrive (POST /inputs/NAME/rows), with nothing written to the folder.
A new version of the command. Replace the binary (or build the image again) and restart the service: docker compose up -d --build, systemctl restart elucid8. The service keeps its caches in memory only, so a restart computes them again as requests arrive; nothing on disk changes. Check a new version against a deployment first: elucid8 check plan.json --config deploy.json reads every file and query without computing. A plan compiled for another plan version is refused with a message saying so: compile it again.
Backups. The deployment folder is everything the service reads: the plan, deploy.json and the data. The only thing the service writes there is a name map that saves ("names" with "save": true): it gains the names that arrive, so back up that file with the data. Nothing else needs a backup.
#9. Limits worth setting
| limit | where | why |
|---|---|---|
| memory for the caches | --cache-bytes 2G (or cacheBytes in deploy.json) | a long-running service keeps what it computed; without a budget the caches grow with what is asked |
| memory for the process | mem_limit (Compose), MemoryMax= (systemd) | above the cache budget: one request's own work is not in it |
| time per request | http.budgetMs in deploy.json, or budgetMs per request | one slow query holds every request behind it |
| request bodies | http.maxBodyBytes (64 MiB) | rows arrive in request bodies |
| connections and subscriptions | http.maxConnections (64), http.maxSubscriptions (32) | each holds a connection |
| threads | --threads N | on a machine shared with other work |
| stop grace period | stop_grace_period, TimeoutStopSec | as long as the longest request (§7) |
#10. How this page is checked
The Docker image is built and run by a test with the deployment above: its answers are those of elucid8 run, the health check passes, docker stop exits 0, the Compose file starts and stops it, and the nginx configuration proxies HTTPS to it; the Caddyfile is checked with caddy validate and the Prometheus file with promtool check config. The systemd unit and the launchd file are checked for their syntax only.