# Engines on other machines

A [job engine](Jobs-Engines.md) does not have to be on the server. An engine on a second machine takes the work of the jobs off the file server; an engine in another network runs jobs where the files are, behind a firewall the server cannot reach through.

## What you need

- The CrushMFT program on the other machine, with Java, in the same version as the server.
- A way from the engine **to** the server. The engine always opens the connection; the server never connects to an engine.
- A folder for the engine on its machine.

## Two ways to connect

| Way | The engine connects to | Use it when |
|---|---|---|
| the engine port | a port of the server's that is only for engines, encrypted (TLS) | the machines are in the same network or you can open a port between them |
| the web port | the server's ordinary HTTPS port, as a WebSocket on `/JOBLINK/` | only the web port can be reached: through a DMZ, a reverse proxy, or a firewall that lets nothing but 443 out |

| Server setting | Is | Unless you change it |
|---|---|---|
| `job_engine_remote_port` | the engine port. Empty: there is none. | empty |
| `job_engine_remote_bind` | the address of the server the engine port listens on | all |
| `job_engine_web_link` | whether engines may connect through the web port | true |

The web port answers on `/JOBLINK/` only while something can connect there: an engine on another machine is known, or an enrolment code is open. On a server without engines elsewhere the address answers "not found", as if the feature were off.

## Adding an engine

1. In the **Engines** section of the Jobs app, press **Add an engine**.
2. "A name for the engine on the other machine." Give it a name, such as `east1`, and its labels if you use any.
3. The server shows **Enrol** and the name, with a command:

   ```
   java -jar CrushMFT.jar -JOB_ENGINE <engine folder> enrol <address> <code>
   ```

   and under it the **Code**, the **Address** of the engine port, and the address **Through the web port**.

   > On the other machine, in a folder of its own, run this within 30 minutes. The code works once; the engine keeps its key and connects by itself from then on.

   "A machine that reaches this server only by its web port (through a DMZ or a proxy) uses the web port address in place of the address."
4. Run the command on the other machine, with the folder for the engine in place of `<engine folder>`.
5. The engine appears in the list as online.

After that, start the engine on its machine with `-JOB_ENGINE <engine folder>` and no more: as a service, so that it comes up with the machine.

What the code does: it carries the fingerprint of the server's key, so the engine knows it is talking to the right server before it sends anything, and a secret that proves the engine to the server without ever being sent. The engine then gets a key of its own. A code that was used, or is older than 30 minutes, is refused.

## What an engine on another machine has, and has not

- **Its own disk.** Addresses in a job that begin with `file://` mean the engine's disk when the run is there. That is the point of an engine "where the files are", and a trap for a job that expects the server's folders. Use labels to send each job to the right place.
- **The job, the log and the history** travel over the link: the engine gets the job from the server, and the run's log and record are kept on the server as for any run.
- **The server's settings**, but not all: an engine elsewhere gets the variables jobs need, without the server's password for file encryption, its Java settings and its DMZ hosts.
- **Mail, users and the like**: a task that needs the server, such as sending mail through the server's mail settings or reading a user, asks the server over the link.

## Labels

A label is a word you give an engine: `linux`, `warehouse`, `gpu`. A job chooses by them in its [Setup](Jobs-Schedules.md#job-engine):

| In the job | The run goes to |
|---|---|
| **Run on engines with labels** `warehouse` | an engine that carries that label. With several labels, one that carries all of them. |
| **Run on the engine named** `east1` | that engine |
| nothing | any engine with room |

An engine with the label `exclusive` takes only runs that ask for it, by its name or one of its labels. Use it for an engine in a special place that must not be handed ordinary jobs.

A run that asks for a label no online engine has waits, and says so under **Waiting for an engine**.

## When an engine is lost

An engine that stops answering is shown as **not answering**. Its runs are not given to another engine at once: the engine may only have lost its connection and be working on. After a wait (the engine's own patience of 30 seconds, and 30 more), the runs are resumed on another engine if the job says **Resume it on another engine** and has **Save state** on. An engine that comes back after that is told to drop those runs, so that nothing runs twice.

A run on an engine with a disk of its own resumes on another engine from its beginning: the place it had saved is on the lost machine.

| Action on an engine | For |
|---|---|
| **Drain**, **Drain by a deadline** | before maintenance: no new runs; at the deadline the runs still there save their place and go on elsewhere |
| release its runs | when you know the machine is gone and do not want to wait |
| revoke | takes the engine's key away. It can never connect again; enrol it anew if it should. For a machine that was lost or given away. |

## Updating engines

An engine should be the same version as the server. Replace the files on the engine's machine, then use **Start all again, one at a time** in the Engines section: each engine is drained, started on the files now on its disk, and takes runs again before the next one goes. The **Version** column shows what each runs.

## What has limits

- Moving a run that is under way from one machine to another needs its item lists to cross too; very long lists make that slow.
- Both ways of connecting have been run on test servers, with the engine on the same machine in a folder of its own. Two separate machines, and an engine behind a real DMZ, are yet to be tried.

See also [Jobs in a cluster](Jobs-Engines-Cluster.md), where every server brings an engine without any enrolment.
