Design

Positioning & Roadmap

Why moond exists, platform differences between macOS and Linux, and future directions.

Why moond

As a focused tool for the MoonBit ecosystem, moond makes complete sense; as a general-purpose PM2 replacement, it does not.

  • Where it fits: MoonBit currently cannot directly exploit multiple CPU cores within a single process, and the ecosystem lacks a native process manager. moond fills the cluster + PM2 gap for MoonBit services—spawning multiple workers, handling crash restarts, rotating logs, and exposing live metrics in a single zero-dependency native binary.
  • What it intentionally avoids: General-purpose server process supervision is already well solved by systemd, supervisord, and container orchestrators. moond stays lean and tailored to MoonBit service workflows rather than duplicating every feature of PM2.

Roadmap

Staying true to a lean, zero-dependency process manager tailored for MoonBit services, progress and upcoming work are organized across Features, Performance, and Experience & Ecosystem:

Features

Completed

  • Single-Binary Daemon & Hardened Control Plane: Auto-spawns a detached daemon on first start, protects against races with a lock file, binds the port before writing credentials, enforces constant-time 0600 Bearer-token auth, and validates app names against path traversal.

  • Multi-Instance Self-Healing & Guards: Supports -i <n>, min-uptime crash resets (--min-uptime), exponential backoff, memory limits (--max-memory-restart), 5-field --cron-restart, and 3s-timeout HTTP/TCP health probes (--health-url / --health-tcp).

  • Zero-Downtime Operations & Declarative Config: Rolling reloads (moond reload), dynamic scaling (moond scale), idempotent hot-updates (moond apply --env), targeted name:id / all controls, and full moond.json lifecycle management.

  • Built-in L4 TCP Reverse Proxy (--proxy-port): Automatically injects PORT / MOOND_WORKER_PORT per worker and load-balances connections with health-aware round-robin routing and automatic failover.

  • Persistence & Logging: Atomic state.json persistence (save / resurrect), incremental byte-offset tailing (logs <name:id> -f), --time line timestamps, and --merge-logs.

  • Graceful Connection Drain & Ready Signal: Drains in-flight proxy connections (--drain-timeout) before stopping old workers during reload and supports explicit worker POST /apps/ready signaling (--wait-ready).

  • Unix Domain Socket Control Plane: Automatically exposes a 0600-permission ~/.moond/moond.sock control channel alongside 127.0.0.1:19876.

  • Observability & Alert Hooks: Exposes a Prometheus-compatible /metrics endpoint (moond metrics) and triggers --webhook-url / --alert-cmd hooks on crash loops, memory limits, or failed probes.

  • Log Search & Multi-App Aggregation: Supports moond logs all -f with colored prefixes plus --grep and --since filtering.

Performance

Completed

  • Atomic Local Time & Per-Second Caching: Captures local time atomically via a single localtime_r call to prevent cross-second tearing, with per-second timestamp caching for --time log lines.

  • Process Metrics Caching: Uses a 50ms per-PID cache around proc_pidinfo on macOS so sampling RSS and CPU in the same pass avoids duplicate syscalls.

  • Low-Memory Streaming Log I/O: Keeps log pipe descriptors open, performs streaming chunked copytruncate rotation, and reads logs -n via a tail window without loading entire files into memory.

  • Concurrent Scheduling & Fast Change Hashing: Concurrent batch stops, per-app Semaphore locks, non-blocking async cron/watch reloads, and 64-bit FNV-1a directory hashing.

  • OS-Native File Watching (kqueue / inotify): Uses kernel-driven filesystem events on macOS and Linux to skip directory scans while idle.

  • TCP Proxy Connection Governance: Tracks per-instance active connections with --proxy-max-conns limits and --proxy-idle-ms idle timeouts.

  • Tiered Metric Sampling & Compressed Rotation: Slices CPU sampling when > 16 instances are online and supports --log-compress async .gz log archiving.

Experience & Ecosystem

Completed

  • Cross-Platform Live Monitoring: Real-time CPU%, RSS memory, and health visibility in moond ls, ls --json, describe, and moond monit (macOS / Linux / Windows metrics support).

  • System Service Integration (moond startup): Generates systemd and XML-escaped launchd boot startup templates with resolved absolute paths.

  • Distribution, Docs & Quality: Published wsafight/moond on mooncakes.io, built a bilingual Astro docs site (3-column TOC + interactive terminal demos), and backed everything with 36 unit tests and 27 E2E tests.

  • Prebuilt Binaries & One-Line Install: Multi-arch GitHub Releases matrix (macOS/Linux x86_64 & arm64), scripts/install.sh, Formula/moond.rb, and moond self-update.

  • Interactive moond monit TUI: Supports j/k/arrow selection, l inline log tailing, r/R/+/- live actions, and q graceful exit.

  • MoonBit Ecosystem Tooling: Added moond init, schema/moond.schema.json for IDE autocomplete, and the wsafight/moond/sdk helper package (worker_port, reuse_port_enabled, notify_ready).

  • Windows Process-Tree Cleanup & CI Matrix: Added recursive Windows process-tree termination and a windows-latest CI test/build matrix.

Platform Behavior Differences

  • Linux (SO_REUSEPORT): Multiple workers can bind the exact same port via TcpServer(..., reuse_port_lb=true) and let the Linux kernel distribute incoming connections across CPU cores.
  • Cross-Platform Built-in TCP Reverse Proxy: On macOS (or for services that do not enable SO_REUSEPORT), pass --proxy-port <port> (and optional --proxy-base-port <port>). moond automatically injects sequential PORT and MOOND_WORKER_PORT values into each worker and load-balances incoming TCP connections in round-robin order with automatic failover.

Risks & Considerations

  • Standard Library Evolution: moonbitlang/core/async is actively evolving; future MoonBit releases may introduce minor API adjustments.
  • Minimal C FFI Surface: Keeping all platform-specific syscalls isolated inside src/sys ensures the core supervisor remains pure MoonBit and easy to maintain across platforms.