# moonrockz/moon-lander-plugins/hosting

A plugin as a service of a Moon-Lander host, on native, js and wasm under Moonrun.
The contract, the order and the failures are in [the module guide](../README.mbt.md).

## Run a plugin in a host

`plugin_service` adds the plugin with its own service name. The host runs inside
`with_plugins`, which owns the task that calls the plugin between the hooks:

```mbt check
///|
async fn run_acme(
  args : Array[String],
  heartbeat : &@plugins.Plugin,
  logger : &@logging.Logger,
) -> @lander_hosting.RunOutcome {
  @plugins_hosting.with_plugins(plugins => {
    let running : Ref[@plugins_hosting.RunningPlugin?] = Ref(None)
    let host = @lander_hosting.builder(
        @lander_core.AppName::parse("acme"),
        args,
      )
      |> @plugins_hosting.plugin_service(
        plugins,
        "heartbeat",
        heartbeat,
        settings=_ => b"1s",
        call_limit=@lander_core.TimeLimit::millis(200),
        logger~,
        started=plugin => running.val = Some(plugin),
      )
      |> @lander_hosting.build(_ => ())
    @lander_hosting.run(host, _ => {
      let count = @plugins.EventName::parse("heartbeat.count")
      let reply = running.val.unwrap().send(count, b"")
      println(@utf8.decode(reply))
    })
  })
}

///|
test "the host compiles" {
  ignore(run_acme)
}
```

| Phase | What the service does |
| --- | --- |
| Start hook | Makes the settings, connects to the plugin, calls `describe` and `start`, then calls `started` |
| Between the hooks | Makes the ticks and the events, one at a time |
| Stop hook | Waits for the call in progress, calls `stop` with the stop reason, and releases the plugin |

The plugin starts after the services before it and stops before them, as each other
hosted service. `settings` gets the host context, so it can read the loaded
configuration. The bytes have the form that the plugin expects.

## Failures

`PluginServiceError` names the service. `SettingsFailed`, `ConnectFailed` and
`StartFailed` fail the start of the host. After the start, a failed plugin asks the
host to stop with the exit code 1. Its stop hook raises `RunFailed`, so the report
of the host has the reason also when there is no logger. The other services get
their stop hooks.

When the shutdown timeout of the host ends first, the plugin is released without an
answer.

## The call limit

Each call has a time limit, 5 seconds by default. `RunningPlugin::enforcement` tells
what it does:

| Value | Meaning |
| --- | --- |
| `Enforced` | The call ends at the limit, and the plugin fails |
| `NotEnforced` | Nothing can end the call. The host logs a warning when it ends late, and uses the reply |

A MoonBit plugin in the host process is `NotEnforced`. A wasm plugin is `Enforced` in a
native host and `NotEnforced` in a js host. A linked native plugin is `NotEnforced`. A
plugin of each kind in a [worker process](../process/README.mbt.md) is `Enforced`: the
host ends the worker at the limit.

## Wake requests

A transport can give a wake flag, so that a plugin thread can ask for a tick: a native
plugin does that with `wake` of its host table. The host takes the flag after each call,
and each 50 ms while no call runs. When the flag was set, the host calls `tick` one time,
also after many requests.

## Logs

`logger` gets the entries of the plugin as `{Service}: {Text}`, with the level of the
entry. An entry of a call is in the logger before the sender gets its reply. The
service also logs the name and the version of the plugin at the start, each late call
and the failure.
