Synchronous process management for MoonBit: spawn with env/cwd/redirection, waitpid and pidfd waits, kill, procfs sampling and platform probes
Dependencies
moon add chensuiyi/subproc| Function | Description |
|---|---|
| spawn(spec) | Start a child, return pid |
| wait_child(pid, nohang?) | Reap via waitpid |
| wait_child_timeout(pid, timeout_ms) | Deadline-bounded reap with group SIGKILL |
| wait_pidfd(fd, nohang?) | Reap via waitid(P_PIDFD) |
| drain_child(pid) | Bounded reap of a just-killed child |
| pidfd_open / pidfd_alive / pidfd_exited / wait_pidfd | pidfd lifecycle |
| kill_group(pgid, sig) / pid_alive(pid) | Signals |
| read_rss_kb(pid) / read_cpu_ticks(pid) / clk_tck() | procfs sampling |
| hostname / uname / get_uid / self_exe / probe_reuseport | Platform probes |
| instance_env_blob / base_env_blob | Environment blobs |
| exit / eprintln / sleep_ms / close | Runtime helpers |
// Spawn with log redirection and own process group
let pid = @subproc.spawn({
bun_path: "bun",
script: "app.js",
cwd: "/srv/app",
out_log: "/var/log/app.out.log",
err_log: "/var/log/app.err.log",
env_blob: @subproc.instance_env_blob("app", 0, 3000),
})
// Graceful stop with hard deadline
@subproc.kill_group(pid, @subproc.SIGTERM)
let reaped = @subproc.wait_child_timeout(pid, 5000)
// Sample
let rss = @subproc.read_rss_kb(pid)moon add chensuiyi/subproc| 函数 | 说明 |
|---|---|
| spawn(spec) | 启动子进程,返回 pid |
| wait_child(pid, nohang?) | waitpid 回收 |
| wait_child_timeout(pid, timeout_ms) | 截限回收,超时 SIGKILL 整组 |
| wait_pidfd(fd, nohang?) | waitid(P_PIDFD) 回收 |
| drain_child(pid) | 有界回收刚杀掉的子进程 |
| pidfd_open / pidfd_alive / pidfd_exited / wait_pidfd | pidfd 生命周期 |
| kill_group(pgid, sig) / pid_alive(pid) | 信号 |
| read_rss_kb(pid) / read_cpu_ticks(pid) / clk_tck() | procfs 采样 |
| hostname / uname / get_uid / self_exe / probe_reuseport | 平台探针 |
| instance_env_blob / base_env_blob | 环境 blob |
| exit / eprintln / sleep_ms / close | 运行辅助 |
// 带日志重定向与独立进程组启动
let pid = @subproc.spawn({
bun_path: "bun",
script: "app.js",
cwd: "/srv/app",
out_log: "/var/log/app.out.log",
err_log: "/var/log/app.err.log",
env_blob: @subproc.instance_env_blob("app", 0, 3000),
})
// 优雅停止(硬截止)
@subproc.kill_group(pid, @subproc.SIGTERM)
let reaped = @subproc.wait_child_timeout(pid, 5000)
// 采样
let rss = @subproc.read_rss_kb(pid)pub(all) suberror ProcessError {
Failed(op~ : String, errno~ : Int)
}impl Show for ProcessErrorpub(all) struct SpawnSpec {
bun_path : String
script : String
cwd : String
out_log : String
err_log : String
env_blob : Bytes
} derive(Debug)fn base_env_blob() -> Bytesfn clk_tck() -> Intfn disk_free_kb(path : String) -> Int64?fn drain_child(pid : Int) -> Unitfn eprintln(text : String) -> Unitfn hostname() -> Stringfn instance_env_blob(app_name : String, instance_id : Int, port : Int, extra? : Array[(String, String)]) -> Bytesfn local_parts(epoch_ms : UInt64) -> (Int, Int, Int, Int, Int, Int)?fn pid_alive(pid : Int) -> Boolfn pidfd_alive(fd : Int) -> Boolfn pidfd_exited(fd : Int, pid : Int) -> Boolfn platform_error() -> String?fn probe_reuseport(port : Int) -> Intfn read_cpu_ticks(pid : Int) -> UInt64?fn read_rss_kb(pid : Int) -> Int?Install
Download zipSynchronous process management for MoonBit: spawn with env/cwd/redirection, waitpid and pidfd waits, kill, procfs sampling and platform probes
Dependencies