README
¶
sqi-server
sqi-server is the control plane for a sqi render farm. A single binary runs
the job scheduler, REST API, WebSocket gateway, an embedded NATS JetStream
broker (or connects to a NATS cluster you run), and the state store: embedded
SQLite by default, or an external PostgreSQL database. Workers connect to the
broker over NATS; clients interact with the server over HTTP and WebSocket.
What it does
| Component | Responsibility |
|---|---|
| Scheduler | Assigns ready tasks to available workers, enforcing priority, capability matching, compute-location affinity, and usage pool limits. |
| REST API | /api/v1/… endpoints for job submission, status queries, worker management, and system configuration. |
| WebSocket gateway | /api/v1/ws endpoint for real-time push of job, task, worker, and log events to subscribed clients. |
| NATS broker | The JetStream broker used for work assignment, status reporting, log streaming, and worker heartbeats: in-process by default, or an external NATS server or cluster with nats.mode: external (see Running on an external NATS cluster). With broker authentication on, the server answers the broker's auth callout for every worker connection. |
| State store | The database holding all durable state — jobs, tasks, workers, farms, queues, usage pools, and audit log: an embedded SQLite file by default, or PostgreSQL 16 or later with store.driver: postgres (see Running on PostgreSQL). |
| Web UI host | Serves the embedded SPA at the root (/, with SPA fallback for extensionless paths and for paths under /ui/) and the OpenAPI spec at /api/v1/openapi.yaml. |
| mDNS responder | Advertises _sqi._tcp on the local network so workers and the CLI can discover the server without manual address configuration. |
Build
Prerequisites: Go 1.26 or newer (the go directive in go.mod pins 1.26.3).
# Build just the server binary into ./bin/
make build-server
# Or build both server and worker
make build
# Or build directly with go
go build -o bin/sqi-server ./cmd/sqi-server
Cross-compiled binaries for all target platforms (Linux, macOS, Windows × amd64/arm64) can be produced with:
make build-all
Run
# Start with all defaults (listens on 0.0.0.0:8080, SQLite at ./sqi.db)
./bin/sqi-server serve
# Start with a config file
./bin/sqi-server serve --config config/sqi-server.yaml
# Override specific values via flags or environment variables
SQI_LOG_FORMAT=text ./bin/sqi-server serve --http-addr 127.0.0.1:8090
# Write a starter configuration file to the platform's default location
# (--force replaces one that exists; --user ACCOUNT on Windows)
./bin/sqi-server config init
# Print the effective merged configuration and exit (useful for debugging)
./bin/sqi-server config print
# Show build metadata
./bin/sqi-server version
On Windows, sqi-server service install|uninstall|start|stop|status installs,
uninstalls, starts, stops and inspects the Windows service (from an elevated
shell) — see docs/operations.md.
The service command exists only in Windows builds.
The first time the server starts it creates the SQLite database and runs all pending migrations automatically. No manual schema setup is required. On PostgreSQL the database must exist already, and the server creates the schema in it.
See config/sqi-server.example.yaml
for a fully commented configuration file, and
docs/configuration.md for a complete reference
of every option.
Smoke test
The following sequence verifies the server is healthy, accepts job submissions,
and returns expected responses. It requires curl and jq.
1. Start the server
./bin/sqi-server serve &
SERVER_PID=$!
sleep 1
2. Liveness and readiness
curl -sf http://localhost:8080/healthz # expects: {"status":"ok"}
curl -sf http://localhost:8080/readyz # expects: {"status":"ok","checks":{"nats":{"status":"ok"},"sqlite":{"status":"ok"}}}
# (the store's check is "postgres" with store.driver: postgres)
3. Submit a minimal OpenJD job
FARM=$(curl -sf http://localhost:8080/api/v1/farms | jq -r '.[0].id')
QUEUE=$(curl -sf "http://localhost:8080/api/v1/queues?farm_id=$FARM" | jq -r '.items[0].id')
JOB_ID=$(curl -sf -X POST \
"http://localhost:8080/api/v1/jobs?farm_id=$FARM&queue_id=$QUEUE&owner=smoke" \
-H 'Content-Type: application/yaml' \
--data-binary '
specificationVersion: "jobtemplate-2023-09"
name: smoke-test
steps:
- name: hello
script:
actions:
onRun:
command: echo
args: ["hello from sqi"]
' | jq -r '.id')
echo "Submitted job: $JOB_ID"
4. Query the job
curl -sf http://localhost:8080/api/v1/jobs/$JOB_ID | jq '{id, name, status}'
5. List tasks
curl -sf http://localhost:8080/api/v1/jobs/$JOB_ID/tasks | jq '[.items[] | {id, status}]'
6. Check Prometheus metrics
curl -sf http://localhost:8080/metrics | grep sqi_
7. Verify the OpenAPI spec is served
curl -sf http://localhost:8080/api/v1/openapi.yaml | head -5
8. Stop the server
kill $SERVER_PID
Database migrations
Schema migrations are embedded in the binary and applied automatically at
startup. To manage migrations manually (each subcommand takes --db for a
SQLite file or --dsn for a PostgreSQL database, and otherwise uses the
configured store):
# Show current migration status
./bin/sqi-server migrate status
# Apply all pending migrations
./bin/sqi-server migrate up
Migrations are forward-only: there is no migrate down. To go back to an older
sqi-server, restore the backup taken before the upgrade
(Rollback). A binary older than the
database refuses to start on it, naming both schema versions. migrate up
takes the single-server lock, so it refuses to run while a server uses the
database. migrate status takes no lock and writes nothing: a database never
migrated lists every migration pending and stays empty (so a fresh PostgreSQL
database can still take a restore), and on SQLite it only reads the file, so
it is safe to run on a backup and does not create a missing one.
Migration SQL files live in
internal/store/migrations/ (SQLite) and
internal/store/migrations/postgres/
(PostgreSQL, starting at 00037_baseline.sql).
Leave the password out of --dsn, since the process list shows a command
line, and supply it through PGPASSWORD or a password file instead. A
PostgreSQL deployment in Docker Compose is in
deploy/docker-compose.postgres.yml.
Backup and restore
# Online backup of the configured store (server running or stopped)
./bin/sqi-server backup --out sqi-backup-$(date +%Y%m%d).db
# Restore it, with the server stopped
./bin/sqi-server restore sqi-backup-20261013.db
# Make a database restored by other means (point-in-time recovery, a snapshot)
# safe to start a server on
./bin/sqi-server restore --in-place
restore takes the single-server lock, checks the backup (an intact sqi
backup no newer than the binary), puts it in place, applies any newer
migrations, returns every task the backup recorded as assigned or running to
ready (the fence: no retry is used), and clears the broker's messages. On
SQLite the live database and its -wal/-shm files are renamed to
<name>.pre-restore-<UTC time>; on PostgreSQL the target database must be
empty, and pg_restore must be on PATH. It chooses the store as backup
does (--db, --dsn, store.driver), and deletes nothing. Work done after
the backup runs again.
How the broker's messages are cleared depends on nats.mode:
- Embedded broker: the JetStream directory,
nats.embedded.data_diror--nats-data-dir, is renamed to<dir>.pre-restore-<UTC time>. - External broker:
restoreconnects to the cluster with the server'snats.externalconfiguration, takes the account's one-server guard before it changes anything and holds it to the end (so it refuses while a server runs on the account, and no server can start meanwhile), then purges sqi's four streams and deletes the server's three durable consumers. It refuses, with nothing changed, when it cannot reach the cluster, unless given--skip-nats-purge: it then restores without the purge and prints thenatsCLI commands to purge by hand before a server starts.--nats-data-dirdoes not apply, and--skip-nats-purgeis refused with an embedded broker.
backup is the same in both modes: it never includes the broker's state. The
runbooks, including what to clean up afterwards, are in
Backup and
Restore, with the external-mode one in
Restoring with an external broker.
External NATS cluster
# Generate the seeds and the nats-server config fragment for a cluster
./bin/sqi-server nats external init --out /etc/sqi/nats
nats external init writes four private nkey seeds (issuer.nk,
sqi-data.nk, sqi-auth.nk, sqi-sys.nk, owner-only, kept on the server
host) and sqi-accounts.conf (public keys only), which every cluster node
includes from beside its own config file. It prints the include line and the
nats: block for sqi-server.yaml, and never replaces existing files without
--force. Step by step:
Running on an external NATS cluster.
Moving a farm to PostgreSQL
# With the server stopped, the SQLite file at this binary's schema
# (migrate up --db first), and an empty PostgreSQL database
./bin/sqi-server store copy --from sqi.db --to "postgres://sqi@db.example.com/sqi?sslmode=verify-full"
store copy copies every row of the SQLite store into an empty PostgreSQL
database in one transaction and verifies each table by row count and digest
before committing. It opens the SQLite file read-only and takes the lock on
both databases. --from defaults to store.sqlite_path and --to to
store.postgres.dsn. Then set store.driver: postgres and start the server.
PostgreSQL to SQLite is not supported. Step by step:
Moving to PostgreSQL.
Package layout
cmd/sqi-server/
├── main.go Entry point: runs the cobra command tree and exits.
└── *.go One file per command group (serve, migrate, backup, restore, tls, nats, ...).
internal/
├── bus/ Broker (embedded and external), auth-callout responder, account guard, typed JetStream client, subjects.
├── config/ Config struct, layered loader, and startup validator.
├── discovery/ mDNS responder (grandcat/zeroconf).
├── health/ /healthz and /readyz handlers.
├── log/ slog-based structured logger and request-scoped middleware.
├── metrics/ Prometheus metric definitions and the /metrics handler.
├── middleware/ HTTP middleware (recovery, CORS, request ID, gzip, logger).
├── openjd/ OpenJD (jobtemplate-2023-09) parser, validator, parameter-space expansion.
├── scheduler/ Assignment loop, worker registry, heartbeat sweep, retry/failure policy.
├── server/ HTTP server wiring — router, middleware stack, boot sequence.
├── store/ Store interface, shared SQL layer, SQLite and PostgreSQL stores, migrations, task state machine.
├── ui/ Embedded web/dist assets, SPA fallback handler.
├── version/ Build metadata (version, commit, build date, Go version).
├── worker/ Worker wire protocol handlers and log ingestion.
└── ws/ WebSocket upgrade, subscription hub, NATS fanout.
Further reading
docs/architecture.md— Component layout and job lifecycle data flow.docs/configuration.md— Complete configuration reference.docs/api.md— REST API reference with worked examples.docs/tls.md— In-process TLS for the API and broker,tls init/tls issue, and worker mTLS.docs/development.md— Local setup and contribution guide.