[Feature]: runtime roles for stacks (web, worker, scheduler)
Refs #231. Code references are pinned to main at f577acbae54cbc7e64542bb191d7019825052475 (after #892 merged) unless a PR is named. The evidence table for every claim is in the first comment.
Amended 28 Sep, after @sudanese and @Farahat612 in the comments: Q9 now fails the deploy instead of warning and skipping, and a roles declaration on the services pipeline now fails validation instead of deploying with roles dropped. Both edits are marked below. #592 has since merged (c3034cf7, shipped in v0.8.0) and the release-phase code now lives at packages/platform/src/engine/modules/deployments/release-phase.ts, so references to it as an open PR are stale.
Ask: ratify "roles on StackDefinition" (TODO.md option 1) and answer the yes/no questions at the end. Co-authoring is open to @Farahat612, who holds the implementation claim from 18 Aug. @sudanese proposed the same model in #231 on 22 Sep and offered to review and re-run their end-to-end Laravel test against a branch.
Existing issue
- I have searched the existing issues and found no duplicate. #231 is the discussion this design belongs to; the maintainer asked for it as a separate feature design.
Problem or use case
A detected Laravel or Rails app deploys one process. StackDefinition has one defaultStartCommand and no role field (packages/core/src/stacks.ts:171-232). TODO.md states that queued jobs and scheduled tasks "silently never run" (TODO.md:419-422). No deploy code under apps/ or packages/ starts queue:work, schedule:run, bin/jobs, or sidekiq.
@sudanese reproduced both gaps end to end on 0.4.5 with a stock Laravel app (#231, 16 Sep). Jobs on the database driver were accepted and never ran, with no error. GET / returned 500 until migrations were run by hand.
The worker workload (#538) already deploys a container with no port and no route (packages/core/src/deployment-class.ts:19-29). It needs an explicit workloadType override, and it describes a whole project, not a second process.
In scope: a role declaration on stacks, a per-project override, Laravel and Rails presets, the single-app deploy path, and placement constraints that multi-node work can read.
Non-goals:
- The release phase itself. That is #592. This RFC only defines the order relative to it.
- Compose support. Roles on the services pipeline are a follow-up (see "Services pipeline" below).
- Docker-free roles on the bare runtime. #873 left that capability tracked in #231.
- Symfony presets (
messenger:consume). Symfony keeps web only in v1. - SQLite persistence. Stock Laravel keeps
database/database.sqliteoutsidepersistentPaths: ["storage"], so the database is lost on redeploy (reported by @sudanese). Roles do not fix this. A worker on SQLite also needs the web container's volume, which the placement table pins to the same node.
Proposed solution
// packages/core/src/stacks.ts
export type RoleKind = "web" | "worker" | "scheduler";
export interface RoleHealth {
kind: "http" | "exec" | "process";
path?: string; // http
command?: string; // exec
}
export interface StackRole {
name: string; // stable id: "web", "queue", "jobs", "sidekiq", "scheduler"
kind: RoleKind;
command: string;
replicas?: number; // default 1
singleton?: boolean; // forced true for kind "scheduler"
health?: RoleHealth; // web keeps the existing readiness gate
when?: { // preset applies only if every listed signal matches
deps?: readonly string[];
files?: readonly string[];
};
}
export interface StackDefinition {
// existing fields unchanged; defaultStartCommand stays the web command
defaultRoles?: readonly StackRole[]; // non-web presets only
}
// packages/db/src/schema/project.ts
roles: jsonb("roles").$type<StackRole[] | null>(), // null = web onlyopenship.json gains a matching roles array. The web role always derives from startCommand, so no existing config changes meaning.
The seam. executeServerDeploy builds one DeployConfig (packages/platform/src/engine/modules/deployments/build-pipeline.ts:2064-2112), and its activate step calls runtime.deploy(cfg) once (build-pipeline.ts:1908-1918). A role is one more runtime.deploy call against the same imageRef with three overrides:
startCommand= the role command. Docker turns it intoCmd(packages/adapters/src/runtime/docker.ts:3492-3493).portless: true, the existing worker flag. Docker then omitsPORTand port publishing (docker.ts:3486-3490).- A role-qualified
runtimeName. The container name isopenship-<runtimeName>-<deploymentId>(docker.ts:3474), so two roles in one deployment collide without it.
Every role reads the same deployment's imageRef and envMap (build-pipeline.ts:2083), so no rebuild and no tagImage() alias is needed.
Cutover order (proposal):
- Build once.
- Release phase, when #592 lands.
- Start new scalable roles. If one fails to start, fail the deploy. The previous version keeps serving.
- Web activation and readiness gate, unchanged.
- Stop previous scalable-role containers with SIGTERM. Solid Queue stops gracefully within the stop timeout. Laravel
queue:workfinishes the current job only with pcntl loaded. openship's generated PHP Dockerfile installs pcntl in the runtime stage (docker-build-plan.ts:211,:283). - Singleton roles: stop the previous container, then start the new one. No overlap, so no double fire.
Two facts need new plumbing. The deployment row stores one containerId (packages/db/src/schema/deployment.ts:117), so role containers need a role label or a list in deployment meta. A worker workload forces the Docker runtime (build-execution-plan.ts:64-66). On bare, roles would log a warning and skip, as #592 does.
Env-only changes. Docker fixes env at container creation, so a restart applies nothing (deployments/env-drift.ts:25-28). The existing refresh deploy recreates every role with the new env.
Presets: Laravel and Rails
| Role | Laravel | Rails |
|---|---|---|
| web | FrankenPHP run, unchanged (stacks.ts:710-711) |
bundle exec rails server -b 0.0.0.0 -p "${PORT:-3000}", unchanged (stacks.ts:668) |
| worker | php artisan queue:work --tries=3 --max-time=3600 |
bin/jobs when solid_queue resolves and bin/jobs exists. bundle exec sidekiq when sidekiq resolves. |
| worker scaling | scalable | scalable |
| scheduler | php artisan schedule:work, singleton: true |
none by default |
| release (#592, reference only) | php artisan migrate --force, optimize, storage:link (TODO.md:428-431) |
bin/rails db:prepare |
Why Laravel needs a singleton scheduler. The Laravel docs state that a scheduler on three servers runs a task three times. Only tasks marked onOneServer() take a lock, and the platform cannot see which tasks use it.
Why Rails has no scheduler role. Solid Queue runs config/recurring.yml in a scheduler process that bin/jobs supervises. Its README states that several schedulers can share one schedule. A unique index on task_key and run_at prevents duplicate enqueues while preserve_finished_jobs stays true (the default). So the scalable jobs role carries its own scheduler.
Detection signals. Rails deps come from Gemfile and Gemfile.lock on main (packages/core/src/languages/ruby.ts:57), so a transitive solid_queue resolves. When both solid_queue and sidekiq resolve, v1 proposes no worker and asks the user.
Health. Web keeps the existing readiness gate. Workers default to process (container running under its restart policy). TODO.md notes that the single-app container path cannot express a healthcheck (TODO.md:504-508), so exec health is new work, not wiring.
Multi-node plans
A project deploys to one host. project.serverId is the single durable binding (packages/db/src/schema/project.ts:410-427). The service table has no server column, and no schema models a cluster. The roadmap lists clustering as in progress and shared volumes as planned. The Scale planner models a service as N instances with ordinal, region, autoscale, minReplicas, and maxReplicas (packages/core/src/scale/topology.ts:69-80). It is at the planning stage with no provisioner. TODO.md states that volume resolution is single-host (TODO.md:491-497).
A role is the placement unit. Each role maps to one Scale service entry:
| Role property | Placement rule |
|---|---|
kind: "web" |
Spread across nodes behind the load balancer. Unchanged by this RFC. |
| scalable worker | minReplicas = maxReplicas = replicas. Instances may run on any node. |
singleton: true |
Exactly one instance across the cluster. autoscale is forced off. On node loss, the old instance stops before a new one starts. |
| role mounts a persistent path | Pinned to the node that holds the volume until shared volumes ship (#163). |
v1 places every role on project.serverId but stores replicas and singleton now. Cluster work can then read the constraint instead of guessing from a command string, with no later schema migration. The type, API validation, and a hard cap of one container at fan-out all enforce the singleton rule.
Services pipeline and release phase
Any enabled compose or monorepo service row selects the services pipeline (deployments/compose/project-services.ts:150-165), which skips the single-app path (build-pipeline.ts:186-213, :1052-1110). The merged release phase refuses project release commands on that pipeline before anything in the running deployment changes, and a test pins it ("refuses project release commands before mutating a multi-service deployment").
So roles must never be persisted as service rows. A worker row would move a Laravel app onto the services pipeline and stop its migrations in the same change. Roles live on the project and expand at fan-out time.
Projects that already have a service row stay on the services pipeline. That includes an app with a database sidecar, because the Add Service catalog stores it as an enabled compose row (services/service.service.ts:626, :729). Amended: in v1 a roles declaration on such a project fails validation at that same refusal point, before the running deployment changes. It does not deploy with the roles dropped and a log line. Supporting the combination stays a follow-up that expands roles next to the materialized app row (compose/deploy.service.ts:569-589). Whether that combination instead falls inside v1 is open question 1.
#592 edits apps/api/src/modules/deployments/, but the pipeline now lives in packages/platform. #592 needs a rebase before either change lands.
Migration for existing projects
project.roles = nullmeans web only. Every existing project keeps its behavior on redeploy.- Project creation and detection write the matching preset into
project.roles. New projects get a visible, editable worker. - Existing projects get a dashboard suggestion ("Detected:
bin/jobs. Add worker?"). Accepting it writes the column. roles: []inopenship.jsonopts out for good.roles: [...]replaces the preset completely.- Rollback runs the roles frozen on that release's snapshot, as it does for
startCommand(build.service.ts:210).
This breaks the volumes convention, where null inherits the stack default (packages/core/src/openship-config/schema.ts:174-181). A new volume is passive. A new worker starts consuming jobs queued for weeks, and it can send mail. That must not arrive on a redeploy nobody asked for.
Alternatives considered
Monorepo service rows. TODO.md offers an "add a worker" button that writes a monorepo row with its own startCommand (TODO.md:449-452). Rejected for three reasons:
- It flips the project onto the services pipeline, which disables the release phase.
- It loses zero-config. The operator assembles rows by hand, which is the complaint in
TODO.md:446-448. - A service row cannot say "singleton". Once per-service replicas exist, a scheduler row fires every due task N times.
Keep multi-process apps on the services path with framework presets there. This is the second option in TODO.md's open decision and the alternative @sudanese named. It avoids a runtime-model change, at the cost of every Laravel and Rails app hand-assembling rows and losing the release phase.
An all-in-one supervisor in the web container. Rejected: workers stop being independently observable, restartable and scalable, and a wedged worker looks healthy from outside.
Additional context
Open questions for the maintainer:
- Roles live on
StackDefinitionwith aproject.rolesoverride (option 1)? project.roles = nullmeans web only, so no existing project gains a worker on redeploy?- New projects get the detected preset written by default, not suggested?
- v1 covers only the single-app pipeline, and roles next to service rows follow with #592 compose support?
- Singleton roles swap stop-then-start, and scalable roles start before web cutover and fail the deploy on start failure?
- v1 allows
replicas > 1for scalable roles on one host? (If no, the field is stored and capped at 1.) - Laravel scheduler uses
schedule:work, although the Laravel docs describe it for local use and showschedule:runfrom cron for servers? - Rails ships no scheduler role, and the
jobsrole carries Solid Queue recurring tasks? - Amended. On a runtime that cannot run a declared role, the deploy fails with a clear message. It does not warn and skip, because skipping reproduces the failure this RFC removes: jobs accepted, never run, deploy green. The merged release phase already behaves this way ("Deployment stopped before activation"). Q2 keeps existing projects out of it.
Not yet covered here, raised by @sudanese on 22 Sep and worth folding in: Horizon as a Laravel worker option, per-process resource settings, Kubernetes execution/placement/scheduler ownership as an upfront contract constraint, and preparing environment-dependent caches in the real runtime containers rather than only in a disposable release container.
Source: oblien/openship