# moonrockz/moon-lander-hosting/moonrun

The Moonrun driver of a Moon-Lander host, on `wasm` under Moonrun or `moonx`.
`run` runs a host with `@hosting.run` on the `moonbitlang/core/async` event loop,
changes SIGINT and SIGTERM into a graceful stop, and ends the process. Hooks and
the command can wait. Use it in `async fn main`:

```mbt check
///|
/// The body of `async fn main` in a wasm program under Moonrun.
async fn moonrun_main() -> Unit {
  let host : @hosting.Host[Unit] = "acme"
    |> @lander_core.AppName::parse
    |> @hosting.builder([])
    |> @hosting.service("listener", start=_ => @async.sleep(10), stop=_ => {
      println("listener stopped")
    })
    |> @hosting.build(_ => ())
  @hosting_moonrun.run(host, ctx => {
    while !ctx.cancelled() {
      @async.sleep(100)
    }
  })
}

///|
test "the Moonrun example compiles" {
  // The driver ends the process, so this test does not call it.
  ignore(moonrun_main)
}
```

Import the package with an explicit alias, because the module name has `-` in
it:

```moonbit notest
///|
import {
  "moonrockz/moon-lander-hosting/moonrun" @hosting_moonrun,
}
```

## Select the driver explicitly

The embedded driver `@hosting.run_embedded` stays the default on wasm and the
only driver on wasm-gc. A wasm build cannot know which runtime loads it, and a
module that calls this driver imports the runtime functions of Moonrun, so other
WebAssembly runtimes cannot load it. The package exists only on wasm, so a build
for another target that imports it fails. See the driver table in the
[`moonrockz/moon-lander-hosting` guide](../README.mbt.md#drivers).

## Signals (interim)

The driver makes SIGINT and SIGTERM cancellation signals of the event loop.
`moonbitlang/core/async` gives a signal on wasm only as the cancellation of the
whole event loop, with no signal value. So, until a pinned MoonBit release has a
public signal observer (`moonbitlang/async#706`, `moonbitlang/core#5041`):

- The first signal is a stop request with the reason `Signal(Unidentified)`. The
  command task is cancelled, and every started service gets its stop hooks
  within the shutdown timeout.
- A second signal does not stop the process at once and skips no stop hook.
- After the run, `run` returns, and Moonrun ends the process by the last signal
  that arrived. A shell shows 130 or 143. `SignalKind::Unidentified` gives the
  exit code 1 in the outcome, because the real signal ends the process.

A follow-up change removes these limits when the observer lands.

## Finalize and report completion

`finalize` and `completed` have the same types and rules as in the console
driver, so a program can move between the two drivers with no other change. Each
runs one time, protected from cancellation. When one raises, a selected code 0
becomes 1 and any other code stays. A signal stop gives them the exit code 1.

```mbt check
///|
async fn moonrun_with_callbacks() -> Unit {
  let host : @hosting.Host[Unit] = "acme"
    |> @lander_core.AppName::parse
    |> @hosting.builder([])
    |> @hosting.build(_ => ())
  @hosting_moonrun.run(host, _ => (), finalize=selected => selected, completed=(
    outcome,
    code,
  ) => {
    let signal = outcome.stop_reason() is Signal(Unidentified)
    println("signal=\{signal} code=\{code.to_int()}")
  })
}

///|
test "the callback example compiles" {
  ignore(moonrun_with_callbacks)
}
```

## Exit

After a run with no signal, the driver ends the process with the exit code of
the run through `@env.exit`, which is WASI `proc_exit`. A help or version request
writes the text one time and ends the process with code 0, with no factory or
hook. Reports go to standard error, as for `@hosting.run`.

## Policy and command

Run a published program with `moonx` and a Moonrun policy:

```text
moonx <owner>/<module>/<package> --experimental-policy moonrun-policy.json
```

The minimal policy and the command for a program in a workspace are in the
[`moonrockz/moon-lander-hosting` guide](../README.mbt.md#moonrun-policy).
