moond

    Lightweight native process manager (Daemon & Cluster Supervisor) for MoonBit.

    process-manager
    daemon
    pm2
    cluster
    supervisor
    native
    async
    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    9 hours ago
    Downloads
    11

    No dependencies

    #moond

    mooncakes.io Docs License

    English | 简体中文

    A lightweight native process manager (Daemon & Cluster Supervisor) built for the MoonBit ecosystem.

    Built on MoonBit's standard library moonbitlang/core/async and Native backend, moond compiles to a single zero-dependency native binary that supervises multi-instance services with exponential backoff restarts, rolling reloads, dynamic scaling, live RSS/CPU metrics, log rotation, Bearer-token authenticated control APIs, and state persistence.

    #Features

    • Single-Binary Architecture: One executable serves as both the CLI client and the detached background daemon (moond daemon), auto-spawned on first moond start with lock-file protection.
    • Multi-Instance Supervision, Rolling Reload, Scaling & Built-in TCP Proxy: Launch multiple workers with -i <n>, perform zero-downtime rolling updates with moond reload <name>, dynamically scale with moond scale <name> <n>, optionally enable --proxy-port <port> for built-in round-robin TCP load balancing (auto-injecting PORT / MOOND_WORKER_PORT), and automatically receive MOOND_APP_NAME, MOOND_INSTANCE_ID, and MOOND_INSTANCES.
    • Min-Uptime Reset, Memory Guard, Health Probes & Cron Restarts: Supports --min-uptime, --restart-delay, --max-restarts, --kill-timeout, --max-memory-restart (e.g. 512M), HTTP/TCP health probes (--health-url / --health-tcp) with readiness-aware rolling reloads, and 5-field --cron-restart.
    • Live Metrics, monit TUI & Ops Tooling: moond ls and moond monit display live per-worker CPU%, RSS memory, health status, and uptime; supports moond ls --json, moond describe <name>, all / <name>:<id> targets, and moond startup [systemd|launchd].
    • Declarative Config Hot-Update & State Recovery: Run start, apply (startOrReload with live env/args/instances hot-update), reload, restart, stop, and delete directly on moond.json with --only <name> and --env <profile>, plus moond save / moond resurrect.
    • Incremental Log Follow, Multi-Archive Rotation & Timestamps: moond logs <name> -f tails logs incrementally by byte offset, with moond flush [name|all], --log-retain <n>, --merge-logs, and --time line timestamps.

    #Quick Start

    #Requirements

    • Latest MoonBit toolchain with moonbitlang/core/async (verified on moon 0.1.20261126)
    • Native C compiler (macOS / Linux)

    #Install / Build

    # Add as a MoonBit module from mooncakes.io moon add wsafight/moond # Or build the standalone native CLI binary from source moon build --target native # Binary: _build/native/debug/build/main/main.exe

    #Basic Usage

    BIN=./_build/native/debug/build/main/main.exe # Start 2 instances with custom env vars (auto-starts detached daemon) $BIN start --name demo -i 2 -e PORT=8080 -- /bin/sh -c 'echo "worker $MOOND_INSTANCE_ID/$MOOND_INSTANCES"; sleep 60' # List managed processes (table or JSON), live TUI monitor, & inspect details $BIN ls $BIN ls --json $BIN monit $BIN describe demo # Rolling reload & dynamic scaling $BIN reload demo --wait-ms 500 $BIN scale demo 4 # View, follow, or flush logs $BIN logs demo -n 20 $BIN logs demo -f $BIN flush demo # Restart / stop / delete an app $BIN restart demo $BIN stop demo $BIN delete demo # Persist & restore state, or generate startup template $BIN save $BIN kill $BIN resurrect $BIN startup systemd # or launchd

    #Declarative Config (moond.json)

    { "apps": [ { "name": "api", "cmd": "./my-moonbit-api", "args": ["--port", "8080"], "cwd": ".", "env": { "APP_ENV": "prod" }, "instances": 4, "max_restarts": 15, "restart_delay_ms": 1000, "min_uptime_ms": 10000, "max_log_bytes": 10485760 } ] }

    # Start all apps or a single app with an environment profile $BIN start moond.json --only api --env production # Idempotent deploy: start missing apps, rolling-reload running ones (alias: startOrReload) $BIN apply moond.json --env production # Operate full lifecycle directly on config file $BIN reload moond.json --only api $BIN stop moond.json $BIN delete moond.json

    #Environment Variables

    VariableDefaultDescription
    MOOND_HOME~/.moondStores token, state.json, daemon.pid, daemon.lock, and logs/
    MOOND_PORT19876Local HTTP control plane port (bound to 127.0.0.1)

    #Project Structure

    . ├── moon.mod # MoonBit module config (preferred_target = "native") ├── src/ │ ├── sys/ # Thin C FFI layer (setsid, getpid, kill_group, RSS & CPU, local time) │ ├── protocol/ # Shared protocol types, paths, cron/memory/config parsers │ ├── daemon/ # Supervisor, lifecycle, monitors, log rotation, watcher, HTTP API │ ├── cli/ # CLI commands, config client, formatting, and monit TUI │ └── main/ # Minimal single-binary executable entrypoint ├── examples/http-cluster/ # Multi-instance MoonBit HTTP server example ├── test_e2e.sh # End-to-end integration test suite ├── site/ # Astro website & docs (English default + /zh/, GitHub Pages) └── .github/workflows/ # CI, Release, and GitHub Pages workflows

    #Running Tests

    # MoonBit unit tests moon test --target native # End-to-end integration tests bash test_e2e.sh

    #Website & Documentation

    Full documentation (Getting Started, CLI & Config Reference, Architecture, and Positioning) lives in site/, built with Astro (English default with Chinese under /zh/):

    cd site pnpm install pnpm run dev

    #License

    MIT