Run a factory session
A factory session is the running process of a factory: npm start in the factory repo. It serves one location, the value of FACTORY_LOCATION in its .env. It asks the Orchestrator for the next queued run of any station on a line at that location, runs the station's handler, and reports the result. Runs of lines at other locations never reach it.
One session per location
At most one session per organisation and location is active. A second session started at the same location stops with:
[Factory] Location 'acme-office' is served by another factory session. Stop that session, or set a different FACTORY_LOCATION for this one. Exiting.
| You want | Do this |
|---|---|
| One machine runs every line | One location for all lines; one session |
| Two machines | Give each its own location (acme-office, acme-cloud), set each line's location to the machine that runs it, and run one session on each machine with its own FACTORY_LOCATION |
| One repo for both machines | Keep every line file in the one repo. Each session runs only the lines at its own location. |
Two organisations can use the same location name; the limit is per organisation. To see the sessions the Orchestrator knows about:
al-orc factory-sessions list --location acme-office
Start and stop
| Command | Use it for |
|---|---|
npm run dev | While you build. It restarts the session when a file it has loaded changes, and sets NODE_ENV=development. |
npm start | Running for real, in the foreground |
al-control procs start -f . | Running for real under pm2, which restarts the session if it dies. The pm2 process is named after the factory's folder. |
al-control procs restart -f ., procs stop, procs logs | Restart, stop, or tail the log of a session under pm2 |
A session that stops with Ctrl-C or a pm2 stop or restart tells the Orchestrator, so the location is free at once. A session that is killed or loses its network holds the location for up to five minutes. If the Orchestrator closes a session, the session exits so pm2 can start a fresh one.
What happens at start
- It finds your stations (below) and prints how many it found.
- It checks
ORCHESTRATOR_API_KEY,ORCHESTRATOR_API_SECRETandFACTORY_LOCATION, and exits naming any that are missing. - It empties every
doing/bin, listing what it threw away: copies left by a run that died. The originals are still ininput/. See The doing bin. - It starts the conveyor and the bin and intake watchers, when
src/index.jscallsstartBinWatcher(), and warns about any station nothing starts. The conveyor moves work into each station'sinputevery 2 seconds; see Connect stations. See Start stations. - It polls the Orchestrator every 2 seconds (
POLL_INTERVAL_MS), and printsSession establishedafter the first poll.
How a session finds your stations
The template's src/index.js finds the station handlers once, at start:
const stations = await discoverStations(new URL('./stations', import.meta.url));
startFactory({ stations, callerUrl: import.meta.url });
discoverStations imports station.js from each folder under src/stations/, and nothing else:
| The folder | What happens |
|---|---|
Its station.js default-exports a defineStation() | The handler is registered under its code |
It has no station.js | Skipped |
Its handler has enabled: false | Skipped |
Its station.js exports something else, or throws when imported | The session refuses to start |
Its handler's code differs from the folder's code, or another folder has the same code | The session refuses to start |
When the Orchestrator sends a run of a station the session has no handler for, the run fails at once with Unknown station: IN1, without the usual retries. Check the station's folder and code match its short code, and restart the session after adding a station.
Several stations at once
A session runs different stations at the same time: a long upload at one station doesn't hold up the others. It never runs two runs of the same station at once, because both would take the same workpieces from one input bin: while a station runs, the session's polls leave its other runs waiting in the queue. The conveyor isn't a station run: it runs in the session on its own, and never waits for one.
All stations share one Node.js process. A station that awaits its work lets the others run; a station that does long synchronous work, such as execFileSync or a large readdirSync walk, holds every other station up until it finishes.
Next
- Debug a run: read the session log and find out why a run failed.
- Give stations their credentials: everything else in
.env. - The lib-factory README lists
startFactoryanddiscoverStations.