# Factory entry point

A factory's `src/index.js` starts its factory session with three calls:

```js
import 'dotenv/config';
import { startFactory, startBinWatcher, discoverStations } from '@assemblyline/lib-factory';

const stations = await discoverStations(new URL('./stations', import.meta.url));
startFactory({ stations, callerUrl: import.meta.url });
startBinWatcher();
```

How to start, stop and keep it running: [Run a factory session](/docs/guides/run-a-factory-session).

## `discoverStations(dir)`

| Argument | What it is |
|---|---|
| `dir` | The stations folder: a path, or a file URL such as `new URL('./stations', import.meta.url)` |

Returns the handlers keyed by station code. Returns `{}` when the folder doesn't exist. The folder rule and what it refuses: [Station definition](/docs/reference/station-definition#what-discovery-refuses).

## `startFactory({ stations, callerUrl })`

| Option | What it is |
|---|---|
| `stations` | Required. The handlers from `discoverStations()`. Without it, `startFactory` throws. |
| `callerUrl` | The entry point's `import.meta.url`, for the user-agent the session sends |

At start it checks the [environment variables](/docs/reference/environment-variables) and exits naming any required one that is missing, then empties every `doing/` bin. It then polls the Orchestrator every `POLL_INTERVAL_MS`, starts each run it claims without waiting for it, and reports the result. Stations run side by side; one station never runs twice at once.

| The session | Exits with |
|---|---|
| gets Ctrl-C, SIGINT or SIGTERM | `0`, after telling the Orchestrator, so the location is free at once |
| is closed by the Orchestrator | `0`, so a process manager starts a fresh one |
| finds another session serving its location | `1` |

## `startBinWatcher({ intervalMs, conveyorIntervalMs })`

Reads the lines at this session's location and their stations from the Orchestrator, retrying until it answers, and writes them to [`.factory/factory.json`](/docs/reference/factory-folder). Then it starts:

| Loop | Every | Does |
|---|---|---|
| The conveyor | `conveyorIntervalMs`, default 2000 ms | Moves each workpiece in a station's `conveyor.from` bins into its `input/`. A workpiece changed in the last 5 seconds waits for a later pass. A disabled or archived station gets nothing. |
| The bin watcher | `intervalMs`, default 10000 ms | Starts each `auto_drain_enabled` station with a conveyor whose `input/` holds a workpiece and has no run in flight |
| The intake watcher | `intervalMs`, default 10000 ms | Starts each `auto_drain_enabled` line head whose config's `watch_path` is not empty |

It warns about stations defined on the Orchestrator with no handler in `src/stations/`, and about stations nothing starts. It reads the definitions once: a change to a conveyor, a switch it acts on or a line's location takes effect when the session restarts. See [Start stations](/docs/guides/trigger-stations) and [Connect stations with a conveyor](/docs/guides/connect-stations).

Returns `{ stop }`, which stops the loops.
