Compare commits
16 commits
dev
...
ax/review-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
861c88b8e8 | ||
|
|
2a0bc19f6a | ||
| 356a644918 | |||
|
|
94b99bfd18 | ||
| 8296075202 | |||
|
|
cca45eebcc | ||
| 542bf0db32 | |||
|
|
eefcb292c4 | ||
|
|
2ccd84b87a | ||
|
|
1425023862 | ||
|
|
62623ce0d6 | ||
|
|
e9bb6a8968 | ||
|
|
1b4efe0f67 | ||
|
|
5d316650e2 | ||
|
|
5a1812bfd3 | ||
|
|
87f53ad8dd |
39 changed files with 3177 additions and 1176 deletions
|
|
@ -20,11 +20,11 @@ core go vet # Vet
|
|||
|
||||
The package has three layers, all in the root `process` package (plus a `exec` subpackage):
|
||||
|
||||
### Layer 1: Process Execution (service.go, process.go, process_global.go)
|
||||
### Layer 1: Process Execution (service.go, process.go)
|
||||
|
||||
`Service` is a Core service (`*core.ServiceRuntime[Options]`) that manages all `Process` instances. It spawns subprocesses, pipes stdout/stderr through goroutines, captures output to a `RingBuffer`, and broadcasts IPC actions (`ActionProcessStarted`, `ActionProcessOutput`, `ActionProcessExited`, `ActionProcessKilled` — defined in actions.go).
|
||||
|
||||
`process_global.go` provides package-level convenience functions (`Start`, `Run`, `Kill`, `List`) that delegate to a global `Service` singleton initialized via `Init(core)`. Follows the same pattern as Go's `i18n` package.
|
||||
During `OnStartup`, the service registers the named Core actions `process.run`, `process.start`, `process.kill`, `process.list`, and `process.get`. Core's `Process` primitive delegates to those actions, so `c.Process().Run(...)` only works when this service has been registered.
|
||||
|
||||
### Layer 2: Daemon Lifecycle (daemon.go, pidfile.go, health.go, registry.go)
|
||||
|
||||
|
|
@ -45,7 +45,7 @@ Builder-pattern wrapper around `os/exec` with structured logging via a pluggable
|
|||
|
||||
## Key Patterns
|
||||
|
||||
- **Core integration**: `Service` embeds `*core.ServiceRuntime[Options]` and uses `s.Core().ACTION(...)` to broadcast typed action messages. Tests create a Core instance via `framework.New(framework.WithName("process", NewService(...)))`.
|
||||
- **Core integration**: `Service` embeds `*core.ServiceRuntime[Options]` and uses `s.Core().ACTION(...)` to broadcast typed action messages. Tests create a Core instance via `framework.New(framework.WithService(Register))`.
|
||||
- **Output capture**: All process output goes through a fixed-size `RingBuffer` (default 1MB). Oldest data is silently overwritten when full. Set `RunOptions.DisableCapture` to skip buffering for long-running processes where output is only streamed via IPC.
|
||||
- **Process lifecycle**: Status transitions are `StatusPending → StatusRunning → StatusExited|StatusFailed|StatusKilled`. The `done` channel closes on exit; use `<-proc.Done()` or `proc.Wait()`.
|
||||
- **Detach / process group isolation**: Set `RunOptions.Detach = true` to run the subprocess in its own process group (`Setpgid`). Detached processes use `context.Background()` so they survive parent context cancellation and parent death.
|
||||
|
|
|
|||
16
buffer.go
16
buffer.go
|
|
@ -4,6 +4,8 @@ import "sync"
|
|||
|
||||
// RingBuffer is a fixed-size circular buffer that overwrites old data.
|
||||
// Thread-safe for concurrent reads and writes.
|
||||
//
|
||||
// rb := process.NewRingBuffer(1024)
|
||||
type RingBuffer struct {
|
||||
data []byte
|
||||
size int
|
||||
|
|
@ -14,6 +16,8 @@ type RingBuffer struct {
|
|||
}
|
||||
|
||||
// NewRingBuffer creates a ring buffer with the given capacity.
|
||||
//
|
||||
// rb := process.NewRingBuffer(256)
|
||||
func NewRingBuffer(size int) *RingBuffer {
|
||||
return &RingBuffer{
|
||||
data: make([]byte, size),
|
||||
|
|
@ -21,7 +25,7 @@ func NewRingBuffer(size int) *RingBuffer {
|
|||
}
|
||||
}
|
||||
|
||||
// Write appends data to the buffer, overwriting oldest data if full.
|
||||
// _, _ = rb.Write([]byte("output line\n"))
|
||||
func (rb *RingBuffer) Write(p []byte) (n int, err error) {
|
||||
rb.mu.Lock()
|
||||
defer rb.mu.Unlock()
|
||||
|
|
@ -39,7 +43,7 @@ func (rb *RingBuffer) Write(p []byte) (n int, err error) {
|
|||
return len(p), nil
|
||||
}
|
||||
|
||||
// String returns the buffer contents as a string.
|
||||
// output := rb.String() // returns all buffered output as a string
|
||||
func (rb *RingBuffer) String() string {
|
||||
rb.mu.RLock()
|
||||
defer rb.mu.RUnlock()
|
||||
|
|
@ -58,7 +62,7 @@ func (rb *RingBuffer) String() string {
|
|||
return string(rb.data[rb.start:rb.end])
|
||||
}
|
||||
|
||||
// Bytes returns a copy of the buffer contents.
|
||||
// data := rb.Bytes() // returns nil if empty
|
||||
func (rb *RingBuffer) Bytes() []byte {
|
||||
rb.mu.RLock()
|
||||
defer rb.mu.RUnlock()
|
||||
|
|
@ -79,7 +83,7 @@ func (rb *RingBuffer) Bytes() []byte {
|
|||
return result
|
||||
}
|
||||
|
||||
// Len returns the current length of data in the buffer.
|
||||
// byteCount := rb.Len() // 0 when empty, Cap() when full
|
||||
func (rb *RingBuffer) Len() int {
|
||||
rb.mu.RLock()
|
||||
defer rb.mu.RUnlock()
|
||||
|
|
@ -93,12 +97,12 @@ func (rb *RingBuffer) Len() int {
|
|||
return rb.size - rb.start + rb.end
|
||||
}
|
||||
|
||||
// Cap returns the buffer capacity.
|
||||
// capacity := rb.Cap() // fixed at construction time
|
||||
func (rb *RingBuffer) Cap() int {
|
||||
return rb.size
|
||||
}
|
||||
|
||||
// Reset clears the buffer.
|
||||
// rb.Reset() // discard all buffered output
|
||||
func (rb *RingBuffer) Reset() {
|
||||
rb.mu.Lock()
|
||||
defer rb.mu.Unlock()
|
||||
|
|
|
|||
108
buffer_test.go
108
buffer_test.go
|
|
@ -1,18 +1,19 @@
|
|||
package process
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRingBuffer(t *testing.T) {
|
||||
func TestBuffer_Write_Good(t *testing.T) {
|
||||
t.Run("write and read", func(t *testing.T) {
|
||||
rb := NewRingBuffer(10)
|
||||
|
||||
n, err := rb.Write([]byte("hello"))
|
||||
itemCount, err := rb.Write([]byte("hello"))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5, n)
|
||||
assert.Equal(t, 5, itemCount)
|
||||
assert.Equal(t, "hello", rb.String())
|
||||
assert.Equal(t, 5, rb.Len())
|
||||
})
|
||||
|
|
@ -38,14 +39,79 @@ func TestRingBuffer(t *testing.T) {
|
|||
assert.Equal(t, 10, rb.Len())
|
||||
})
|
||||
|
||||
t.Run("empty buffer", func(t *testing.T) {
|
||||
t.Run("bytes returns copy", func(t *testing.T) {
|
||||
rb := NewRingBuffer(10)
|
||||
_, _ = rb.Write([]byte("hello"))
|
||||
|
||||
contents := rb.Bytes()
|
||||
assert.Equal(t, []byte("hello"), contents)
|
||||
|
||||
// Modifying returned bytes shouldn't affect buffer
|
||||
contents[0] = 'x'
|
||||
assert.Equal(t, "hello", rb.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuffer_Write_Bad(t *testing.T) {
|
||||
t.Run("empty write is a no-op", func(t *testing.T) {
|
||||
rb := NewRingBuffer(10)
|
||||
itemCount, err := rb.Write([]byte{})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, itemCount)
|
||||
assert.Equal(t, "", rb.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuffer_Write_Ugly(t *testing.T) {
|
||||
t.Run("concurrent writes do not race", func(t *testing.T) {
|
||||
rb := NewRingBuffer(64)
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_, _ = rb.Write([]byte("data"))
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
// Buffer should not panic and length should be bounded by capacity
|
||||
assert.LessOrEqual(t, rb.Len(), rb.Cap())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuffer_String_Good(t *testing.T) {
|
||||
t.Run("empty buffer returns empty string", func(t *testing.T) {
|
||||
rb := NewRingBuffer(10)
|
||||
assert.Equal(t, "", rb.String())
|
||||
assert.Equal(t, 0, rb.Len())
|
||||
assert.Nil(t, rb.Bytes())
|
||||
})
|
||||
|
||||
t.Run("reset", func(t *testing.T) {
|
||||
t.Run("full buffer wraps correctly", func(t *testing.T) {
|
||||
rb := NewRingBuffer(5)
|
||||
_, _ = rb.Write([]byte("abcde"))
|
||||
assert.Equal(t, "abcde", rb.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuffer_String_Bad(t *testing.T) {
|
||||
t.Run("overflowed buffer reflects newest data", func(t *testing.T) {
|
||||
rb := NewRingBuffer(5)
|
||||
_, _ = rb.Write([]byte("hello"))
|
||||
_, _ = rb.Write([]byte("world"))
|
||||
// Oldest bytes ("hello") have been overwritten
|
||||
assert.Equal(t, "world", rb.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuffer_String_Ugly(t *testing.T) {
|
||||
t.Run("size-1 buffer holds only last byte", func(t *testing.T) {
|
||||
rb := NewRingBuffer(1)
|
||||
_, _ = rb.Write([]byte("abc"))
|
||||
assert.Equal(t, "c", rb.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuffer_Reset_Good(t *testing.T) {
|
||||
t.Run("clears all data", func(t *testing.T) {
|
||||
rb := NewRingBuffer(10)
|
||||
_, _ = rb.Write([]byte("hello"))
|
||||
rb.Reset()
|
||||
|
|
@ -53,20 +119,28 @@ func TestRingBuffer(t *testing.T) {
|
|||
assert.Equal(t, 0, rb.Len())
|
||||
})
|
||||
|
||||
t.Run("cap", func(t *testing.T) {
|
||||
t.Run("cap returns buffer capacity", func(t *testing.T) {
|
||||
rb := NewRingBuffer(42)
|
||||
assert.Equal(t, 42, rb.Cap())
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("bytes returns copy", func(t *testing.T) {
|
||||
func TestBuffer_Reset_Bad(t *testing.T) {
|
||||
t.Run("reset on empty buffer is a no-op", func(t *testing.T) {
|
||||
rb := NewRingBuffer(10)
|
||||
_, _ = rb.Write([]byte("hello"))
|
||||
|
||||
bytes := rb.Bytes()
|
||||
assert.Equal(t, []byte("hello"), bytes)
|
||||
|
||||
// Modifying returned bytes shouldn't affect buffer
|
||||
bytes[0] = 'x'
|
||||
assert.Equal(t, "hello", rb.String())
|
||||
rb.Reset()
|
||||
assert.Equal(t, "", rb.String())
|
||||
assert.Equal(t, 0, rb.Len())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuffer_Reset_Ugly(t *testing.T) {
|
||||
t.Run("reset after overflow allows fresh writes", func(t *testing.T) {
|
||||
rb := NewRingBuffer(5)
|
||||
_, _ = rb.Write([]byte("hello"))
|
||||
_, _ = rb.Write([]byte("world"))
|
||||
rb.Reset()
|
||||
_, _ = rb.Write([]byte("new"))
|
||||
assert.Equal(t, "new", rb.String())
|
||||
})
|
||||
}
|
||||
|
|
|
|||
45
daemon.go
45
daemon.go
|
|
@ -2,15 +2,15 @@ package process
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
"dappco.re/go/core"
|
||||
)
|
||||
|
||||
// DaemonOptions configures daemon mode execution.
|
||||
//
|
||||
// opts := process.DaemonOptions{PIDFile: "/tmp/process.pid", HealthAddr: "127.0.0.1:0"}
|
||||
type DaemonOptions struct {
|
||||
// PIDFile path for single-instance enforcement.
|
||||
// Leave empty to skip PID file management.
|
||||
|
|
@ -37,6 +37,8 @@ type DaemonOptions struct {
|
|||
}
|
||||
|
||||
// Daemon manages daemon lifecycle: PID file, health server, graceful shutdown.
|
||||
//
|
||||
// daemon := process.NewDaemon(process.DaemonOptions{HealthAddr: "127.0.0.1:0"})
|
||||
type Daemon struct {
|
||||
opts DaemonOptions
|
||||
pid *PIDFile
|
||||
|
|
@ -46,6 +48,8 @@ type Daemon struct {
|
|||
}
|
||||
|
||||
// NewDaemon creates a daemon runner with the given options.
|
||||
//
|
||||
// daemon := process.NewDaemon(process.DaemonOptions{PIDFile: "/tmp/process.pid"})
|
||||
func NewDaemon(opts DaemonOptions) *Daemon {
|
||||
if opts.ShutdownTimeout == 0 {
|
||||
opts.ShutdownTimeout = 30 * time.Second
|
||||
|
|
@ -68,12 +72,14 @@ func NewDaemon(opts DaemonOptions) *Daemon {
|
|||
}
|
||||
|
||||
// Start initialises the daemon (PID file, health server).
|
||||
//
|
||||
// err := daemon.Start()
|
||||
func (d *Daemon) Start() error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
if d.running {
|
||||
return coreerr.E("Daemon.Start", "daemon already running", nil)
|
||||
return core.E("daemon.start", "daemon already running", nil)
|
||||
}
|
||||
|
||||
if d.pid != nil {
|
||||
|
|
@ -96,12 +102,21 @@ func (d *Daemon) Start() error {
|
|||
// Auto-register if registry is set
|
||||
if d.opts.Registry != nil {
|
||||
entry := d.opts.RegistryEntry
|
||||
entry.PID = os.Getpid()
|
||||
entry.PID = currentPID()
|
||||
if d.health != nil {
|
||||
entry.Health = d.health.Addr()
|
||||
}
|
||||
if err := d.opts.Registry.Register(entry); err != nil {
|
||||
return coreerr.E("Daemon.Start", "registry", err)
|
||||
if d.health != nil {
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), d.opts.ShutdownTimeout)
|
||||
_ = d.health.Stop(shutdownCtx)
|
||||
cancel()
|
||||
}
|
||||
if d.pid != nil {
|
||||
_ = d.pid.Release()
|
||||
}
|
||||
d.running = false
|
||||
return core.E("daemon.start", "registry", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -109,11 +124,13 @@ func (d *Daemon) Start() error {
|
|||
}
|
||||
|
||||
// Run blocks until the context is cancelled.
|
||||
//
|
||||
// err := daemon.Run(ctx)
|
||||
func (d *Daemon) Run(ctx context.Context) error {
|
||||
d.mu.Lock()
|
||||
if !d.running {
|
||||
d.mu.Unlock()
|
||||
return coreerr.E("Daemon.Run", "daemon not started - call Start() first", nil)
|
||||
return core.E("daemon.run", "daemon not started - call Start() first", nil)
|
||||
}
|
||||
d.mu.Unlock()
|
||||
|
||||
|
|
@ -123,6 +140,8 @@ func (d *Daemon) Run(ctx context.Context) error {
|
|||
}
|
||||
|
||||
// Stop performs graceful shutdown.
|
||||
//
|
||||
// err := daemon.Stop()
|
||||
func (d *Daemon) Stop() error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
|
@ -139,13 +158,13 @@ func (d *Daemon) Stop() error {
|
|||
if d.health != nil {
|
||||
d.health.SetReady(false)
|
||||
if err := d.health.Stop(shutdownCtx); err != nil {
|
||||
errs = append(errs, coreerr.E("Daemon.Stop", "health server", err))
|
||||
errs = append(errs, core.E("daemon.stop", "health server", err))
|
||||
}
|
||||
}
|
||||
|
||||
if d.pid != nil {
|
||||
if err := d.pid.Release(); err != nil && !os.IsNotExist(err) {
|
||||
errs = append(errs, coreerr.E("Daemon.Stop", "pid file", err))
|
||||
if err := d.pid.Release(); err != nil && !isNotExist(err) {
|
||||
errs = append(errs, core.E("daemon.stop", "pid file", err))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -157,12 +176,14 @@ func (d *Daemon) Stop() error {
|
|||
d.running = false
|
||||
|
||||
if len(errs) > 0 {
|
||||
return errors.Join(errs...)
|
||||
return core.ErrorJoin(errs...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetReady sets the daemon readiness status for health checks.
|
||||
//
|
||||
// daemon.SetReady(true)
|
||||
func (d *Daemon) SetReady(ready bool) {
|
||||
if d.health != nil {
|
||||
d.health.SetReady(ready)
|
||||
|
|
@ -170,6 +191,8 @@ func (d *Daemon) SetReady(ready bool) {
|
|||
}
|
||||
|
||||
// HealthAddr returns the health server address, or empty if disabled.
|
||||
//
|
||||
// addr := daemon.HealthAddr() // e.g. "127.0.0.1:9090"
|
||||
func (d *Daemon) HealthAddr() string {
|
||||
if d.health != nil {
|
||||
return d.health.Addr()
|
||||
|
|
|
|||
|
|
@ -4,16 +4,16 @@ import (
|
|||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDaemon_StartAndStop(t *testing.T) {
|
||||
pidPath := filepath.Join(t.TempDir(), "test.pid")
|
||||
func TestDaemon_Lifecycle_Good(t *testing.T) {
|
||||
pidPath := core.JoinPath(t.TempDir(), "test.pid")
|
||||
|
||||
d := NewDaemon(DaemonOptions{
|
||||
PIDFile: pidPath,
|
||||
|
|
@ -36,7 +36,7 @@ func TestDaemon_StartAndStop(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDaemon_DoubleStartFails(t *testing.T) {
|
||||
func TestDaemon_AlreadyRunning_Bad(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{
|
||||
HealthAddr: "127.0.0.1:0",
|
||||
})
|
||||
|
|
@ -50,7 +50,7 @@ func TestDaemon_DoubleStartFails(t *testing.T) {
|
|||
assert.Contains(t, err.Error(), "already running")
|
||||
}
|
||||
|
||||
func TestDaemon_RunWithoutStartFails(t *testing.T) {
|
||||
func TestDaemon_RunUnstarted_Bad(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{})
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
|
@ -61,7 +61,7 @@ func TestDaemon_RunWithoutStartFails(t *testing.T) {
|
|||
assert.Contains(t, err.Error(), "not started")
|
||||
}
|
||||
|
||||
func TestDaemon_SetReady(t *testing.T) {
|
||||
func TestDaemon_SetReady_Good(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{
|
||||
HealthAddr: "127.0.0.1:0",
|
||||
})
|
||||
|
|
@ -83,17 +83,17 @@ func TestDaemon_SetReady(t *testing.T) {
|
|||
_ = resp.Body.Close()
|
||||
}
|
||||
|
||||
func TestDaemon_NoHealthAddrReturnsEmpty(t *testing.T) {
|
||||
func TestDaemon_HealthAddrDisabled_Good(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{})
|
||||
assert.Empty(t, d.HealthAddr())
|
||||
}
|
||||
|
||||
func TestDaemon_DefaultShutdownTimeout(t *testing.T) {
|
||||
func TestDaemon_DefaultTimeout_Good(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{})
|
||||
assert.Equal(t, 30*time.Second, d.opts.ShutdownTimeout)
|
||||
}
|
||||
|
||||
func TestDaemon_RunBlocksUntilCancelled(t *testing.T) {
|
||||
func TestDaemon_RunBlocking_Good(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{
|
||||
HealthAddr: "127.0.0.1:0",
|
||||
})
|
||||
|
|
@ -126,7 +126,7 @@ func TestDaemon_RunBlocksUntilCancelled(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDaemon_StopIdempotent(t *testing.T) {
|
||||
func TestDaemon_StopIdempotent_Good(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{})
|
||||
|
||||
// Stop without Start should be a no-op
|
||||
|
|
@ -134,9 +134,9 @@ func TestDaemon_StopIdempotent(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDaemon_AutoRegisters(t *testing.T) {
|
||||
func TestDaemon_AutoRegister_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
reg := NewRegistry(filepath.Join(dir, "daemons"))
|
||||
reg := NewRegistry(core.JoinPath(dir, "daemons"))
|
||||
|
||||
d := NewDaemon(DaemonOptions{
|
||||
HealthAddr: "127.0.0.1:0",
|
||||
|
|
@ -163,3 +163,28 @@ func TestDaemon_AutoRegisters(t *testing.T) {
|
|||
_, ok = reg.Get("test-app", "serve")
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestDaemon_Lifecycle_Ugly(t *testing.T) {
|
||||
t.Run("stop called twice is safe", func(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{
|
||||
HealthAddr: "127.0.0.1:0",
|
||||
})
|
||||
|
||||
err := d.Start()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = d.Stop()
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Second stop should be a no-op
|
||||
err = d.Stop()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("set ready with no health server is a no-op", func(t *testing.T) {
|
||||
d := NewDaemon(DaemonOptions{})
|
||||
// Should not panic
|
||||
d.SetReady(true)
|
||||
d.SetReady(false)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
302
docs/RFC.md
Normal file
302
docs/RFC.md
Normal file
|
|
@ -0,0 +1,302 @@
|
|||
# go-process API Contract — RFC Specification
|
||||
|
||||
> `dappco.re/go/core/process` — Managed process execution for the Core ecosystem.
|
||||
> This package is the ONLY package that imports `os/exec`. Everything else uses
|
||||
> `c.Process()` which delegates to Actions registered by this package.
|
||||
|
||||
**Status:** v0.8.0
|
||||
**Module:** `dappco.re/go/core/process`
|
||||
**Depends on:** core/go v0.8.0
|
||||
|
||||
---
|
||||
|
||||
## 1. Purpose
|
||||
|
||||
go-process provides the implementation behind `c.Process()`. Core defines the primitive (Section 17). go-process registers the Action handlers that make it work.
|
||||
|
||||
```
|
||||
core/go defines: c.Process().Run(ctx, "git", "log")
|
||||
→ calls c.Action("process.run").Run(ctx, opts)
|
||||
|
||||
go-process provides: c.Action("process.run", s.handleRun)
|
||||
→ actually executes the command via os/exec
|
||||
```
|
||||
|
||||
Without go-process registered, `c.Process().Run()` returns `Result{OK: false}`. Permission-by-registration.
|
||||
|
||||
### Current State (2026-03-25)
|
||||
|
||||
The codebase is PRE-migration. The RFC describes the v0.8.0 target. What exists today:
|
||||
|
||||
- `service.go` — `NewService(opts) func(*Core) (any, error)` — **old factory signature**. Change to `Register(c *Core) core.Result`
|
||||
- `OnStartup() error` / `OnShutdown() error` — **Change** to return `core.Result`
|
||||
- `process.SetDefault(svc)` global singleton — **Remove**. Service registers in Core conclave
|
||||
- Own ID generation `fmt.Sprintf("proc-%d", ...)` — **Replace** with `core.ID()`
|
||||
- Custom `map[string]*ManagedProcess` — **Replace** with `core.Registry[*ManagedProcess]`
|
||||
- No named Actions registered — **Add** `process.run/start/kill/list/get` during OnStartup
|
||||
|
||||
### File Layout
|
||||
|
||||
```
|
||||
service.go — main service (factory, lifecycle, process execution)
|
||||
registry.go — daemon registry (PID files, health, restart)
|
||||
daemon.go — DaemonEntry, managed daemon lifecycle
|
||||
health.go — health check endpoints
|
||||
pidfile.go — PID file management
|
||||
buffer.go — output buffering
|
||||
actions.go — WILL CONTAIN Action handlers after migration
|
||||
global.go — global Default() singleton — DELETE after migration
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. Registration
|
||||
|
||||
```go
|
||||
// Register is the WithService factory.
|
||||
//
|
||||
// core.New(core.WithService(process.Register))
|
||||
func Register(c *core.Core) core.Result {
|
||||
svc := &Service{
|
||||
ServiceRuntime: core.NewServiceRuntime(c, Options{}),
|
||||
managed: core.NewRegistry[*ManagedProcess](),
|
||||
}
|
||||
return core.Result{Value: svc, OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
### OnStartup — Register Actions
|
||||
|
||||
```go
|
||||
func (s *Service) OnStartup(ctx context.Context) core.Result {
|
||||
c := s.Core()
|
||||
c.Action("process.run", s.handleRun)
|
||||
c.Action("process.start", s.handleStart)
|
||||
c.Action("process.kill", s.handleKill)
|
||||
c.Action("process.list", s.handleList)
|
||||
c.Action("process.get", s.handleGet)
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
### OnShutdown — Kill Managed Processes
|
||||
|
||||
```go
|
||||
func (s *Service) OnShutdown(ctx context.Context) core.Result {
|
||||
s.managed.Each(func(id string, p *ManagedProcess) {
|
||||
p.Kill()
|
||||
})
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Action Handlers
|
||||
|
||||
### process.run — Synchronous Execution
|
||||
|
||||
```go
|
||||
func (s *Service) handleRun(ctx context.Context, opts core.Options) core.Result {
|
||||
command := opts.String("command")
|
||||
args, _ := opts.Get("args").Value.([]string)
|
||||
dir := opts.String("dir")
|
||||
env, _ := opts.Get("env").Value.([]string)
|
||||
|
||||
cmd := exec.CommandContext(ctx, command, args...)
|
||||
if dir != "" { cmd.Dir = dir }
|
||||
if len(env) > 0 { cmd.Env = append(os.Environ(), env...) }
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return core.Result{Value: err, OK: false}
|
||||
}
|
||||
return core.Result{Value: string(output), OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
> Note: go-process is the ONLY package allowed to import `os` and `os/exec`.
|
||||
|
||||
### process.start — Detached/Background
|
||||
|
||||
```go
|
||||
func (s *Service) handleStart(ctx context.Context, opts core.Options) core.Result {
|
||||
command := opts.String("command")
|
||||
args, _ := opts.Get("args").Value.([]string)
|
||||
|
||||
cmd := exec.Command(command, args...)
|
||||
cmd.Dir = opts.String("dir")
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return core.Result{Value: err, OK: false}
|
||||
}
|
||||
|
||||
id := core.ID()
|
||||
managed := &ManagedProcess{
|
||||
ID: id, PID: cmd.Process.Pid, Command: command,
|
||||
cmd: cmd, done: make(chan struct{}),
|
||||
}
|
||||
s.managed.Set(id, managed)
|
||||
|
||||
go func() {
|
||||
cmd.Wait()
|
||||
close(managed.done)
|
||||
managed.ExitCode = cmd.ProcessState.ExitCode()
|
||||
}()
|
||||
|
||||
return core.Result{Value: id, OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
### process.kill — Terminate by ID or PID
|
||||
|
||||
```go
|
||||
func (s *Service) handleKill(ctx context.Context, opts core.Options) core.Result {
|
||||
id := opts.String("id")
|
||||
if id != "" {
|
||||
r := s.managed.Get(id)
|
||||
if !r.OK {
|
||||
return core.Result{Value: core.E("process.kill", core.Concat("not found: ", id), nil), OK: false}
|
||||
}
|
||||
r.Value.(*ManagedProcess).Kill()
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
|
||||
pid := opts.Int("pid")
|
||||
if pid > 0 {
|
||||
proc, err := os.FindProcess(pid)
|
||||
if err != nil { return core.Result{Value: err, OK: false} }
|
||||
proc.Signal(syscall.SIGTERM)
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
|
||||
return core.Result{Value: core.E("process.kill", "need id or pid", nil), OK: false}
|
||||
}
|
||||
```
|
||||
|
||||
### process.list / process.get
|
||||
|
||||
```go
|
||||
func (s *Service) handleList(ctx context.Context, opts core.Options) core.Result {
|
||||
return core.Result{Value: s.managed.Names(), OK: true}
|
||||
}
|
||||
|
||||
func (s *Service) handleGet(ctx context.Context, opts core.Options) core.Result {
|
||||
id := opts.String("id")
|
||||
r := s.managed.Get(id)
|
||||
if !r.OK { return r }
|
||||
return core.Result{Value: r.Value.(*ManagedProcess).Info(), OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. ManagedProcess
|
||||
|
||||
```go
|
||||
type ManagedProcess struct {
|
||||
ID string
|
||||
PID int
|
||||
Command string
|
||||
ExitCode int
|
||||
StartedAt time.Time
|
||||
cmd *exec.Cmd
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func (p *ManagedProcess) IsRunning() bool {
|
||||
select {
|
||||
case <-p.done: return false
|
||||
default: return true
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ManagedProcess) Kill() {
|
||||
if p.cmd != nil && p.cmd.Process != nil {
|
||||
p.cmd.Process.Signal(syscall.SIGTERM)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ManagedProcess) Done() <-chan struct{} { return p.done }
|
||||
|
||||
func (p *ManagedProcess) Info() ProcessInfo {
|
||||
return ProcessInfo{
|
||||
ID: p.ID, PID: p.PID, Command: p.Command,
|
||||
Running: p.IsRunning(), ExitCode: p.ExitCode, StartedAt: p.StartedAt,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Daemon Registry
|
||||
|
||||
Higher-level abstraction over `process.start`:
|
||||
|
||||
```
|
||||
process.start → low level: start a command, get a handle
|
||||
daemon.Start → high level: PID file, health endpoint, restart policy, signals
|
||||
```
|
||||
|
||||
Daemon registry uses `core.Registry[*DaemonEntry]`.
|
||||
|
||||
---
|
||||
|
||||
## 6. Error Handling
|
||||
|
||||
All errors via `core.E()`. String building via `core.Concat()`.
|
||||
|
||||
```go
|
||||
return core.Result{Value: core.E("process.run", core.Concat("command failed: ", command), err), OK: false}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. Test Strategy
|
||||
|
||||
AX-7: `TestFile_Function_{Good,Bad,Ugly}`
|
||||
|
||||
```
|
||||
TestService_Register_Good — factory returns Result
|
||||
TestService_OnStartup_Good — registers 5 Actions
|
||||
TestService_HandleRun_Good — runs command, returns output
|
||||
TestService_HandleRun_Bad — command not found
|
||||
TestService_HandleRun_Ugly — timeout via context
|
||||
TestService_HandleStart_Good — starts detached, returns ID
|
||||
TestService_HandleStart_Bad — invalid command
|
||||
TestService_HandleKill_Good — kills by ID
|
||||
TestService_HandleKill_Bad — unknown ID
|
||||
TestService_HandleList_Good — returns managed process IDs
|
||||
TestService_OnShutdown_Good — kills all managed processes
|
||||
TestService_Ugly_PermissionModel — no go-process = c.Process().Run() fails
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. Quality Gates
|
||||
|
||||
go-process is the ONE exception — it imports `os` and `os/exec` because it IS the process primitive. All other disallowed imports still apply:
|
||||
|
||||
```bash
|
||||
# Should only find os/exec in service.go, os in service.go
|
||||
grep -rn '"os"\|"os/exec"' *.go | grep -v _test.go
|
||||
|
||||
# No other disallowed imports
|
||||
grep -rn '"io"\|"fmt"\|"errors"\|"log"\|"encoding/json"\|"path/filepath"\|"unsafe"\|"strings"' *.go \
|
||||
| grep -v _test.go
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Consumer RFCs
|
||||
|
||||
| Package | RFC | Role |
|
||||
|---------|-----|------|
|
||||
| core/go | `core/go/docs/RFC.md` | Primitives — Process primitive (Section 17) |
|
||||
| core/agent | `core/agent/docs/RFC.md` | Consumer — `c.Process().RunIn()` for git/build ops |
|
||||
|
||||
---
|
||||
|
||||
## Changelog
|
||||
|
||||
- 2026-03-25: v0.8.0 spec — written with full core/go domain context.
|
||||
|
|
@ -60,32 +60,28 @@ participate in the Core DI container and implements both `Startable` and
|
|||
```go
|
||||
type Service struct {
|
||||
*core.ServiceRuntime[Options]
|
||||
processes map[string]*Process
|
||||
mu sync.RWMutex
|
||||
managed *core.Registry[*ManagedProcess]
|
||||
bufSize int
|
||||
idCounter atomic.Uint64
|
||||
}
|
||||
```
|
||||
|
||||
Key behaviours:
|
||||
|
||||
- **OnStartup** — currently a no-op; reserved for future initialisation.
|
||||
- **OnStartup** — registers the named Core actions `process.run`, `process.start`, `process.kill`, `process.list`, and `process.get`.
|
||||
- **OnShutdown** — iterates all running processes and calls `Kill()` on each,
|
||||
ensuring no orphaned child processes when the application exits.
|
||||
- Process IDs are generated as `proc-N` using an atomic counter, guaranteeing
|
||||
uniqueness without locks.
|
||||
- Process IDs are generated with `core.ID()` and stored in a Core registry.
|
||||
|
||||
#### Registration
|
||||
|
||||
The service is registered with Core via a factory function:
|
||||
|
||||
```go
|
||||
process.NewService(process.Options{BufferSize: 2 * 1024 * 1024})
|
||||
core.New(core.WithService(process.Register))
|
||||
```
|
||||
|
||||
`NewService` returns a `func(*core.Core) (any, error)` closure — the standard
|
||||
Core service factory signature. The `Options` struct is captured by the closure
|
||||
and applied when Core instantiates the service.
|
||||
`Register` returns `core.Result{Value: *Service, OK: true}` — the standard
|
||||
Core `WithService` factory signature used by the v0.8.0 contract.
|
||||
|
||||
### Process
|
||||
|
||||
|
|
@ -163,12 +159,12 @@ const (
|
|||
When `Service.StartWithOptions()` is called:
|
||||
|
||||
```
|
||||
1. Generate unique ID (atomic counter)
|
||||
1. Generate a unique ID with `core.ID()`
|
||||
2. Create context with cancel
|
||||
3. Build os/exec.Cmd with dir, env, pipes
|
||||
4. Create RingBuffer (unless DisableCapture is set)
|
||||
5. cmd.Start()
|
||||
6. Store process in map
|
||||
6. Store process in the Core registry
|
||||
7. Broadcast ActionProcessStarted via Core.ACTION
|
||||
8. Spawn 2 goroutines to stream stdout and stderr
|
||||
- Each line is written to the RingBuffer
|
||||
|
|
@ -176,8 +172,9 @@ When `Service.StartWithOptions()` is called:
|
|||
9. Spawn 1 goroutine to wait for process exit
|
||||
- Waits for output goroutines to finish first
|
||||
- Calls cmd.Wait()
|
||||
- Updates process status and exit code
|
||||
- Classifies the exit as exited, failed, or killed
|
||||
- Closes the done channel
|
||||
- Broadcasts ActionProcessKilled when the process died from a signal
|
||||
- Broadcasts ActionProcessExited
|
||||
```
|
||||
|
||||
|
|
@ -296,12 +293,12 @@ File naming convention: `{code}-{daemon}.json` (slashes replaced with dashes).
|
|||
|
||||
## exec Sub-Package
|
||||
|
||||
The `exec` package (`forge.lthn.ai/core/go-process/exec`) provides a fluent
|
||||
The `exec` package (`dappco.re/go/core/process/exec`) provides a fluent
|
||||
wrapper around `os/exec` for simple, one-shot commands that do not need Core
|
||||
integration:
|
||||
|
||||
```go
|
||||
import "forge.lthn.ai/core/go-process/exec"
|
||||
import "dappco.re/go/core/process/exec"
|
||||
|
||||
// Fluent API
|
||||
err := exec.Command(ctx, "go", "build", "./...").
|
||||
|
|
|
|||
|
|
@ -101,9 +101,7 @@ go-process/
|
|||
pidfile.go # PID file single-instance lock
|
||||
pidfile_test.go # PID file tests
|
||||
process.go # Process type and methods
|
||||
process_global.go # Global singleton and convenience API
|
||||
process_test.go # Process tests
|
||||
global_test.go # Global API tests (concurrency)
|
||||
registry.go # Daemon registry (JSON file store)
|
||||
registry_test.go # Registry tests
|
||||
runner.go # Pipeline runner (sequential, parallel, DAG)
|
||||
|
|
@ -142,8 +140,6 @@ go-process/
|
|||
| `ErrProcessNotFound` | No process with the given ID exists in the service |
|
||||
| `ErrProcessNotRunning` | Operation requires a running process (e.g. SendInput, Signal) |
|
||||
| `ErrStdinNotAvailable` | Stdin pipe is nil (already closed or never created) |
|
||||
| `ErrServiceNotInitialized` | Global convenience function called before `process.Init()` |
|
||||
| `ServiceError` | Wraps service-level errors with a message string |
|
||||
|
||||
## Build Configuration
|
||||
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ description: Process management with Core IPC integration for Go applications.
|
|||
|
||||
# go-process
|
||||
|
||||
`forge.lthn.ai/core/go-process` is a process management library that provides
|
||||
`dappco.re/go/core/process` is a process management library that provides
|
||||
spawning, monitoring, and controlling external processes with real-time output
|
||||
streaming via the Core ACTION (IPC) system. It integrates directly with the
|
||||
[Core DI framework](https://forge.lthn.ai/core/go) as a first-class service.
|
||||
[Core DI framework](https://dappco.re/go/core) as a first-class service.
|
||||
|
||||
## Features
|
||||
|
||||
|
|
@ -28,22 +28,17 @@ streaming via the Core ACTION (IPC) system. It integrates directly with the
|
|||
```go
|
||||
import (
|
||||
"context"
|
||||
framework "forge.lthn.ai/core/go/pkg/core"
|
||||
"forge.lthn.ai/core/go-process"
|
||||
"dappco.re/go/core"
|
||||
"dappco.re/go/core/process"
|
||||
)
|
||||
|
||||
// Create a Core instance with the process service
|
||||
c, err := framework.New(
|
||||
framework.WithName("process", process.NewService(process.Options{})),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Create a Core instance with the process service registered.
|
||||
c := core.New(core.WithService(process.Register))
|
||||
|
||||
// Retrieve the typed service
|
||||
svc, err := framework.ServiceFor[*process.Service](c, "process")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
svc, ok := core.ServiceFor[*process.Service](c, "process")
|
||||
if !ok {
|
||||
panic("process service not registered")
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -51,15 +46,19 @@ if err != nil {
|
|||
|
||||
```go
|
||||
// Fire-and-forget (async)
|
||||
proc, err := svc.Start(ctx, "go", "test", "./...")
|
||||
if err != nil {
|
||||
return err
|
||||
start := svc.Start(ctx, "go", "test", "./...")
|
||||
if !start.OK {
|
||||
return start.Value.(error)
|
||||
}
|
||||
proc := start.Value.(*process.Process)
|
||||
<-proc.Done()
|
||||
fmt.Println(proc.Output())
|
||||
|
||||
// Synchronous convenience
|
||||
output, err := svc.Run(ctx, "echo", "hello world")
|
||||
run := svc.Run(ctx, "echo", "hello world")
|
||||
if run.OK {
|
||||
fmt.Println(run.Value.(string))
|
||||
}
|
||||
```
|
||||
|
||||
### Listen for Events
|
||||
|
|
@ -67,7 +66,7 @@ output, err := svc.Run(ctx, "echo", "hello world")
|
|||
Process lifecycle events are broadcast through Core's ACTION system:
|
||||
|
||||
```go
|
||||
c.RegisterAction(func(c *framework.Core, msg framework.Message) error {
|
||||
c.RegisterAction(func(c *core.Core, msg core.Message) core.Result {
|
||||
switch m := msg.(type) {
|
||||
case process.ActionProcessStarted:
|
||||
fmt.Printf("Started: %s (PID %d)\n", m.Command, m.PID)
|
||||
|
|
@ -78,24 +77,24 @@ c.RegisterAction(func(c *framework.Core, msg framework.Message) error {
|
|||
case process.ActionProcessKilled:
|
||||
fmt.Printf("Killed with %s\n", m.Signal)
|
||||
}
|
||||
return nil
|
||||
return core.Result{OK: true}
|
||||
})
|
||||
```
|
||||
|
||||
### Global Convenience API
|
||||
### Permission Model
|
||||
|
||||
For applications that only need a single process service, a global singleton
|
||||
is available:
|
||||
Core's process primitive delegates to named actions registered by this module.
|
||||
Without `process.Register`, `c.Process().Run(...)` fails with `OK: false`.
|
||||
|
||||
```go
|
||||
// Initialise once at startup
|
||||
process.Init(coreInstance)
|
||||
c := core.New()
|
||||
r := c.Process().Run(ctx, "echo", "blocked")
|
||||
fmt.Println(r.OK) // false
|
||||
|
||||
// Then use package-level functions anywhere
|
||||
proc, _ := process.Start(ctx, "ls", "-la")
|
||||
output, _ := process.Run(ctx, "date")
|
||||
procs := process.List()
|
||||
running := process.Running()
|
||||
c = core.New(core.WithService(process.Register))
|
||||
_ = c.ServiceStartup(ctx, nil)
|
||||
r = c.Process().Run(ctx, "echo", "allowed")
|
||||
fmt.Println(r.OK) // true
|
||||
```
|
||||
|
||||
## Package Layout
|
||||
|
|
@ -109,7 +108,7 @@ running := process.Running()
|
|||
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| Module path | `forge.lthn.ai/core/go-process` |
|
||||
| Module path | `dappco.re/go/core/process` |
|
||||
| Go version | 1.26.0 |
|
||||
| Licence | EUPL-1.2 |
|
||||
|
||||
|
|
@ -117,7 +116,7 @@ running := process.Running()
|
|||
|
||||
| Module | Purpose |
|
||||
|--------|---------|
|
||||
| `forge.lthn.ai/core/go` | Core DI framework (`ServiceRuntime`, `Core.ACTION`, lifecycle interfaces) |
|
||||
| `dappco.re/go/core` | Core DI framework (`ServiceRuntime`, `Core.ACTION`, lifecycle interfaces) |
|
||||
| `github.com/stretchr/testify` | Test assertions (test-only) |
|
||||
|
||||
The package has no other runtime dependencies beyond the Go standard library
|
||||
|
|
|
|||
151
docs/plans/2026-03-25-v0.7.0-core-alignment.md
Normal file
151
docs/plans/2026-03-25-v0.7.0-core-alignment.md
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
# go-process v0.7.0 — Core Alignment
|
||||
|
||||
> Written by Cladius with full core/go domain context (2026-03-25).
|
||||
> Read core/go docs/RFC.md Section 17 for the full Process primitive spec.
|
||||
|
||||
## What Changed in core/go
|
||||
|
||||
core/go v0.8.0 added:
|
||||
- `c.Process()` — primitive that delegates to `c.Action("process.*")`
|
||||
- `c.Action("name")` — named action registry with panic recovery
|
||||
- `Startable.OnStartup()` returns `core.Result` (not `error`)
|
||||
- `Registry[T]` — universal thread-safe named collection
|
||||
- `core.ID()` — unique identifier primitive
|
||||
|
||||
go-process needs to align its factory signature and register process Actions.
|
||||
|
||||
## Step 1: Fix Factory Signature
|
||||
|
||||
Current (`service.go`):
|
||||
```go
|
||||
func NewService(opts Options) func(*core.Core) (any, error) {
|
||||
```
|
||||
|
||||
Target:
|
||||
```go
|
||||
func Register(c *core.Core) core.Result {
|
||||
svc := &Service{
|
||||
ServiceRuntime: core.NewServiceRuntime(c, Options{}),
|
||||
processes: make(map[string]*ManagedProcess),
|
||||
}
|
||||
return core.Result{Value: svc, OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
This matches `core.WithService(process.Register)` — the standard pattern.
|
||||
|
||||
## Step 2: Register Process Actions During OnStartup
|
||||
|
||||
```go
|
||||
func (s *Service) OnStartup(ctx context.Context) core.Result {
|
||||
c := s.Core()
|
||||
|
||||
// Register named actions — these are what c.Process() calls
|
||||
c.Action("process.run", s.handleRun)
|
||||
c.Action("process.start", s.handleStart)
|
||||
c.Action("process.kill", s.handleKill)
|
||||
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
Note: `OnStartup` now returns `core.Result` not `error`.
|
||||
|
||||
## Step 3: Implement Action Handlers
|
||||
|
||||
```go
|
||||
func (s *Service) handleRun(ctx context.Context, opts core.Options) core.Result {
|
||||
command := opts.String("command")
|
||||
args, _ := opts.Get("args").Value.([]string)
|
||||
dir := opts.String("dir")
|
||||
env, _ := opts.Get("env").Value.([]string)
|
||||
|
||||
// Use existing RunWithOptions internally
|
||||
out, err := s.RunWithOptions(ctx, RunOptions{
|
||||
Command: command,
|
||||
Args: args,
|
||||
Dir: dir,
|
||||
Env: env,
|
||||
})
|
||||
if err != nil {
|
||||
return core.Result{Value: err, OK: false}
|
||||
}
|
||||
return core.Result{Value: out, OK: true}
|
||||
}
|
||||
|
||||
func (s *Service) handleStart(ctx context.Context, opts core.Options) core.Result {
|
||||
// Detached process — returns handle ID
|
||||
command := opts.String("command")
|
||||
args, _ := opts.Get("args").Value.([]string)
|
||||
|
||||
handle, err := s.Start(ctx, StartOptions{
|
||||
Command: command,
|
||||
Args: args,
|
||||
Dir: opts.String("dir"),
|
||||
Detach: opts.Bool("detach"),
|
||||
})
|
||||
if err != nil {
|
||||
return core.Result{Value: err, OK: false}
|
||||
}
|
||||
return core.Result{Value: handle.ID, OK: true}
|
||||
}
|
||||
|
||||
func (s *Service) handleKill(ctx context.Context, opts core.Options) core.Result {
|
||||
id := opts.String("id")
|
||||
pid := opts.Int("pid")
|
||||
|
||||
if id != "" {
|
||||
return s.KillByID(id)
|
||||
}
|
||||
return s.KillByPID(pid)
|
||||
}
|
||||
```
|
||||
|
||||
## Step 4: Remove Global Singleton Pattern
|
||||
|
||||
Current: `process.SetDefault(svc)` and `process.Default()` global state.
|
||||
|
||||
Target: Service registered in Core's conclave. No global state.
|
||||
|
||||
The `ensureProcess()` hack in core/agent exists because go-process doesn't register properly. Once this is done, that bridge can be deleted.
|
||||
|
||||
## Step 5: Update OnShutdown
|
||||
|
||||
```go
|
||||
func (s *Service) OnShutdown(ctx context.Context) core.Result {
|
||||
// Kill all managed processes
|
||||
for _, p := range s.processes {
|
||||
p.Kill()
|
||||
}
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
```
|
||||
|
||||
## Step 6: Use core.ID() for Process IDs
|
||||
|
||||
Current: `fmt.Sprintf("proc-%d", s.idCounter.Add(1))`
|
||||
|
||||
Target: `core.ID()` — consistent format across ecosystem.
|
||||
|
||||
## Step 7: AX-7 Tests
|
||||
|
||||
All tests renamed to `TestFile_Function_{Good,Bad,Ugly}`:
|
||||
- `TestService_Register_Good` — factory returns Result
|
||||
- `TestService_HandleRun_Good` — runs command via Action
|
||||
- `TestService_HandleRun_Bad` — command not found
|
||||
- `TestService_HandleKill_Good` — kills by ID
|
||||
- `TestService_OnStartup_Good` — registers Actions
|
||||
- `TestService_OnShutdown_Good` — kills all processes
|
||||
|
||||
## What This Unlocks
|
||||
|
||||
Once go-process v0.7.0 ships:
|
||||
- `core.New(core.WithService(process.Register))` — standard registration
|
||||
- `c.Process().Run(ctx, "git", "log")` — works end-to-end
|
||||
- core/agent deletes `proc.go`, `ensureProcess()`, `ProcessRegister`
|
||||
- Tests can mock process execution by registering a fake handler
|
||||
|
||||
## Dependencies
|
||||
|
||||
- core/go v0.8.0 (already done — Action system, Process primitive, Result lifecycle)
|
||||
- No other deps change
|
||||
6
exec/doc.go
Normal file
6
exec/doc.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Package exec provides a small command wrapper around `os/exec` with
|
||||
// structured logging hooks.
|
||||
//
|
||||
// ctx := context.Background()
|
||||
// out, err := exec.Command(ctx, "echo", "hello").Output()
|
||||
package exec
|
||||
47
exec/exec.go
47
exec/exec.go
|
|
@ -3,16 +3,16 @@ package exec
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
"dappco.re/go/core"
|
||||
)
|
||||
|
||||
// Options configuration for command execution
|
||||
// Options configures command execution.
|
||||
//
|
||||
// opts := exec.Options{Dir: "/workspace", Env: []string{"CI=1"}}
|
||||
type Options struct {
|
||||
Dir string
|
||||
Env []string
|
||||
|
|
@ -23,7 +23,9 @@ type Options struct {
|
|||
// Background bool
|
||||
}
|
||||
|
||||
// Command wraps os/exec.Command with logging and context
|
||||
// Command wraps `os/exec.Command` with logging and context.
|
||||
//
|
||||
// cmd := exec.Command(ctx, "git", "status").WithDir("/workspace")
|
||||
func Command(ctx context.Context, name string, args ...string) *Cmd {
|
||||
return &Cmd{
|
||||
name: name,
|
||||
|
|
@ -32,7 +34,7 @@ func Command(ctx context.Context, name string, args ...string) *Cmd {
|
|||
}
|
||||
}
|
||||
|
||||
// Cmd represents a wrapped command
|
||||
// Cmd represents a wrapped command.
|
||||
type Cmd struct {
|
||||
name string
|
||||
args []string
|
||||
|
|
@ -42,31 +44,31 @@ type Cmd struct {
|
|||
logger Logger
|
||||
}
|
||||
|
||||
// WithDir sets the working directory
|
||||
// WithDir sets the working directory.
|
||||
func (c *Cmd) WithDir(dir string) *Cmd {
|
||||
c.opts.Dir = dir
|
||||
return c
|
||||
}
|
||||
|
||||
// WithEnv sets the environment variables
|
||||
// WithEnv sets the environment variables.
|
||||
func (c *Cmd) WithEnv(env []string) *Cmd {
|
||||
c.opts.Env = env
|
||||
return c
|
||||
}
|
||||
|
||||
// WithStdin sets stdin
|
||||
// WithStdin sets stdin.
|
||||
func (c *Cmd) WithStdin(r io.Reader) *Cmd {
|
||||
c.opts.Stdin = r
|
||||
return c
|
||||
}
|
||||
|
||||
// WithStdout sets stdout
|
||||
// WithStdout sets stdout.
|
||||
func (c *Cmd) WithStdout(w io.Writer) *Cmd {
|
||||
c.opts.Stdout = w
|
||||
return c
|
||||
}
|
||||
|
||||
// WithStderr sets stderr
|
||||
// WithStderr sets stderr.
|
||||
func (c *Cmd) WithStderr(w io.Writer) *Cmd {
|
||||
c.opts.Stderr = w
|
||||
return c
|
||||
|
|
@ -144,22 +146,23 @@ func (c *Cmd) prepare() {
|
|||
|
||||
// RunQuiet executes the command suppressing stdout unless there is an error.
|
||||
// Useful for internal commands.
|
||||
//
|
||||
// _ = exec.RunQuiet(ctx, "go", "test", "./...")
|
||||
func RunQuiet(ctx context.Context, name string, args ...string) error {
|
||||
var stderr bytes.Buffer
|
||||
cmd := Command(ctx, name, args...).WithStderr(&stderr)
|
||||
if err := cmd.Run(); err != nil {
|
||||
// Include stderr in error message
|
||||
return coreerr.E("RunQuiet", strings.TrimSpace(stderr.String()), err)
|
||||
return core.E("RunQuiet", core.Trim(stderr.String()), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func wrapError(caller string, err error, name string, args []string) error {
|
||||
cmdStr := name + " " + strings.Join(args, " ")
|
||||
cmdStr := commandString(name, args)
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
return coreerr.E(caller, fmt.Sprintf("command %q failed with exit code %d", cmdStr, exitErr.ExitCode()), err)
|
||||
return core.E(caller, core.Sprintf("command %q failed with exit code %d", cmdStr, exitErr.ExitCode()), err)
|
||||
}
|
||||
return coreerr.E(caller, fmt.Sprintf("failed to execute %q", cmdStr), err)
|
||||
return core.E(caller, core.Sprintf("failed to execute %q", cmdStr), err)
|
||||
}
|
||||
|
||||
func (c *Cmd) getLogger() Logger {
|
||||
|
|
@ -170,9 +173,17 @@ func (c *Cmd) getLogger() Logger {
|
|||
}
|
||||
|
||||
func (c *Cmd) logDebug(msg string) {
|
||||
c.getLogger().Debug(msg, "cmd", c.name, "args", strings.Join(c.args, " "))
|
||||
c.getLogger().Debug(msg, "cmd", c.name, "args", core.Join(" ", c.args...))
|
||||
}
|
||||
|
||||
func (c *Cmd) logError(msg string, err error) {
|
||||
c.getLogger().Error(msg, "cmd", c.name, "args", strings.Join(c.args, " "), "err", err)
|
||||
c.getLogger().Error(msg, "cmd", c.name, "args", core.Join(" ", c.args...), "err", err)
|
||||
}
|
||||
|
||||
func commandString(name string, args []string) string {
|
||||
if len(args) == 0 {
|
||||
return name
|
||||
}
|
||||
parts := append([]string{name}, args...)
|
||||
return core.Join(" ", parts...)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ package exec_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"dappco.re/go/core"
|
||||
"dappco.re/go/core/process/exec"
|
||||
)
|
||||
|
||||
|
|
@ -27,7 +27,7 @@ func (m *mockLogger) Error(msg string, keyvals ...any) {
|
|||
m.errorCalls = append(m.errorCalls, logCall{msg, keyvals})
|
||||
}
|
||||
|
||||
func TestCommand_Run_Good_LogsDebug(t *testing.T) {
|
||||
func TestCommand_Run_Good(t *testing.T) {
|
||||
logger := &mockLogger{}
|
||||
ctx := context.Background()
|
||||
|
||||
|
|
@ -49,7 +49,7 @@ func TestCommand_Run_Good_LogsDebug(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCommand_Run_Bad_LogsError(t *testing.T) {
|
||||
func TestCommand_Run_Bad(t *testing.T) {
|
||||
logger := &mockLogger{}
|
||||
ctx := context.Background()
|
||||
|
||||
|
|
@ -81,7 +81,7 @@ func TestCommand_Output_Good(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if strings.TrimSpace(string(out)) != "test" {
|
||||
if core.Trim(string(out)) != "test" {
|
||||
t.Errorf("expected 'test', got %q", string(out))
|
||||
}
|
||||
if len(logger.debugCalls) != 1 {
|
||||
|
|
@ -99,7 +99,7 @@ func TestCommand_CombinedOutput_Good(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if strings.TrimSpace(string(out)) != "combined" {
|
||||
if core.Trim(string(out)) != "combined" {
|
||||
t.Errorf("expected 'combined', got %q", string(out))
|
||||
}
|
||||
if len(logger.debugCalls) != 1 {
|
||||
|
|
@ -107,14 +107,14 @@ func TestCommand_CombinedOutput_Good(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNopLogger(t *testing.T) {
|
||||
func TestNopLogger_Methods_Good(t *testing.T) {
|
||||
// Verify NopLogger doesn't panic
|
||||
var nop exec.NopLogger
|
||||
nop.Debug("msg", "key", "val")
|
||||
nop.Error("msg", "key", "val")
|
||||
}
|
||||
|
||||
func TestSetDefaultLogger(t *testing.T) {
|
||||
func TestLogger_SetDefault_Good(t *testing.T) {
|
||||
original := exec.DefaultLogger()
|
||||
defer exec.SetDefaultLogger(original)
|
||||
|
||||
|
|
@ -132,7 +132,7 @@ func TestSetDefaultLogger(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCommand_UsesDefaultLogger(t *testing.T) {
|
||||
func TestCommand_UsesDefaultLogger_Good(t *testing.T) {
|
||||
original := exec.DefaultLogger()
|
||||
defer exec.SetDefaultLogger(original)
|
||||
|
||||
|
|
@ -147,7 +147,7 @@ func TestCommand_UsesDefaultLogger(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCommand_WithDir(t *testing.T) {
|
||||
func TestCommand_WithDir_Good(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
out, err := exec.Command(ctx, "pwd").
|
||||
WithDir("/tmp").
|
||||
|
|
@ -156,13 +156,13 @@ func TestCommand_WithDir(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
trimmed := strings.TrimSpace(string(out))
|
||||
trimmed := core.Trim(string(out))
|
||||
if trimmed != "/tmp" && trimmed != "/private/tmp" {
|
||||
t.Errorf("expected /tmp or /private/tmp, got %q", trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommand_WithEnv(t *testing.T) {
|
||||
func TestCommand_WithEnv_Good(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
out, err := exec.Command(ctx, "sh", "-c", "echo $TEST_EXEC_VAR").
|
||||
WithEnv([]string{"TEST_EXEC_VAR=exec_val"}).
|
||||
|
|
@ -171,31 +171,32 @@ func TestCommand_WithEnv(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if strings.TrimSpace(string(out)) != "exec_val" {
|
||||
if core.Trim(string(out)) != "exec_val" {
|
||||
t.Errorf("expected 'exec_val', got %q", string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommand_WithStdinStdoutStderr(t *testing.T) {
|
||||
func TestCommand_WithStdinStdoutStderr_Good(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
input := strings.NewReader("piped input\n")
|
||||
var stdout, stderr strings.Builder
|
||||
input := core.NewReader("piped input\n")
|
||||
stdout := core.NewBuilder()
|
||||
stderr := core.NewBuilder()
|
||||
|
||||
err := exec.Command(ctx, "cat").
|
||||
WithStdin(input).
|
||||
WithStdout(&stdout).
|
||||
WithStderr(&stderr).
|
||||
WithStdout(stdout).
|
||||
WithStderr(stderr).
|
||||
WithLogger(&mockLogger{}).
|
||||
Run()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if strings.TrimSpace(stdout.String()) != "piped input" {
|
||||
if core.Trim(stdout.String()) != "piped input" {
|
||||
t.Errorf("expected 'piped input', got %q", stdout.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunQuiet_Good(t *testing.T) {
|
||||
func TestRunQuiet_Command_Good(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
err := exec.RunQuiet(ctx, "echo", "quiet")
|
||||
if err != nil {
|
||||
|
|
@ -203,10 +204,88 @@ func TestRunQuiet_Good(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRunQuiet_Bad(t *testing.T) {
|
||||
func TestRunQuiet_Command_Bad(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
err := exec.RunQuiet(ctx, "sh", "-c", "echo fail >&2; exit 1")
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommand_Run_Ugly(t *testing.T) {
|
||||
t.Run("cancelled context terminates command", func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
err := exec.Command(ctx, "sleep", "10").
|
||||
WithLogger(&mockLogger{}).
|
||||
Run()
|
||||
if err == nil {
|
||||
t.Fatal("expected error from cancelled context")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommand_Output_Bad(t *testing.T) {
|
||||
t.Run("non-zero exit returns error", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, err := exec.Command(ctx, "sh", "-c", "exit 1").
|
||||
WithLogger(&mockLogger{}).
|
||||
Output()
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommand_Output_Ugly(t *testing.T) {
|
||||
t.Run("non-existent command returns error", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, err := exec.Command(ctx, "nonexistent_command_xyz_abc").
|
||||
WithLogger(&mockLogger{}).
|
||||
Output()
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-existent command")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommand_CombinedOutput_Bad(t *testing.T) {
|
||||
t.Run("non-zero exit returns output and error", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
out, err := exec.Command(ctx, "sh", "-c", "echo stderr >&2; exit 1").
|
||||
WithLogger(&mockLogger{}).
|
||||
CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
if string(out) == "" {
|
||||
t.Error("expected combined output even on failure")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommand_CombinedOutput_Ugly(t *testing.T) {
|
||||
t.Run("command with no output returns empty bytes", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
out, err := exec.Command(ctx, "true").
|
||||
WithLogger(&mockLogger{}).
|
||||
CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(out) != 0 {
|
||||
t.Errorf("expected empty output, got %q", string(out))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunQuiet_Command_Ugly(t *testing.T) {
|
||||
t.Run("non-existent command returns error", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
err := exec.RunQuiet(ctx, "nonexistent_command_xyz_abc")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-existent command")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package exec
|
|||
|
||||
// Logger interface for command execution logging.
|
||||
// Compatible with pkg/log.Logger and other structured loggers.
|
||||
//
|
||||
// exec.SetDefaultLogger(myLogger)
|
||||
type Logger interface {
|
||||
// Debug logs a debug-level message with optional key-value pairs.
|
||||
Debug(msg string, keyvals ...any)
|
||||
|
|
@ -10,6 +12,8 @@ type Logger interface {
|
|||
}
|
||||
|
||||
// NopLogger is a no-op logger that discards all messages.
|
||||
//
|
||||
// var logger exec.NopLogger
|
||||
type NopLogger struct{}
|
||||
|
||||
// Debug discards the message (no-op implementation).
|
||||
|
|
@ -22,6 +26,8 @@ var defaultLogger Logger = NopLogger{}
|
|||
|
||||
// SetDefaultLogger sets the package-level default logger.
|
||||
// Commands without an explicit logger will use this.
|
||||
//
|
||||
// exec.SetDefaultLogger(myLogger)
|
||||
func SetDefaultLogger(l Logger) {
|
||||
if l == nil {
|
||||
l = NopLogger{}
|
||||
|
|
@ -30,6 +36,8 @@ func SetDefaultLogger(l Logger) {
|
|||
}
|
||||
|
||||
// DefaultLogger returns the current default logger.
|
||||
//
|
||||
// logger := exec.DefaultLogger()
|
||||
func DefaultLogger() Logger {
|
||||
return defaultLogger
|
||||
}
|
||||
|
|
|
|||
267
global_test.go
267
global_test.go
|
|
@ -1,267 +0,0 @@
|
|||
package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
framework "dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGlobal_DefaultNotInitialized(t *testing.T) {
|
||||
// Reset global state for this test
|
||||
old := defaultService.Swap(nil)
|
||||
defer func() {
|
||||
if old != nil {
|
||||
defaultService.Store(old)
|
||||
}
|
||||
}()
|
||||
|
||||
assert.Nil(t, Default())
|
||||
|
||||
_, err := Start(context.Background(), "echo", "test")
|
||||
assert.ErrorIs(t, err, ErrServiceNotInitialized)
|
||||
|
||||
_, err = Run(context.Background(), "echo", "test")
|
||||
assert.ErrorIs(t, err, ErrServiceNotInitialized)
|
||||
|
||||
_, err = Get("proc-1")
|
||||
assert.ErrorIs(t, err, ErrServiceNotInitialized)
|
||||
|
||||
assert.Nil(t, List())
|
||||
assert.Nil(t, Running())
|
||||
|
||||
err = Kill("proc-1")
|
||||
assert.ErrorIs(t, err, ErrServiceNotInitialized)
|
||||
|
||||
_, err = StartWithOptions(context.Background(), RunOptions{Command: "echo"})
|
||||
assert.ErrorIs(t, err, ErrServiceNotInitialized)
|
||||
|
||||
_, err = RunWithOptions(context.Background(), RunOptions{Command: "echo"})
|
||||
assert.ErrorIs(t, err, ErrServiceNotInitialized)
|
||||
}
|
||||
|
||||
func newGlobalTestService(t *testing.T) *Service {
|
||||
t.Helper()
|
||||
c := framework.New()
|
||||
factory := NewService(Options{})
|
||||
raw, err := factory(c)
|
||||
require.NoError(t, err)
|
||||
return raw.(*Service)
|
||||
}
|
||||
|
||||
func TestGlobal_SetDefault(t *testing.T) {
|
||||
t.Run("sets and retrieves service", func(t *testing.T) {
|
||||
old := defaultService.Swap(nil)
|
||||
defer func() {
|
||||
if old != nil {
|
||||
defaultService.Store(old)
|
||||
}
|
||||
}()
|
||||
|
||||
svc := newGlobalTestService(t)
|
||||
|
||||
err := SetDefault(svc)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, svc, Default())
|
||||
})
|
||||
|
||||
t.Run("errors on nil", func(t *testing.T) {
|
||||
err := SetDefault(nil)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGlobal_ConcurrentDefault(t *testing.T) {
|
||||
old := defaultService.Swap(nil)
|
||||
defer func() {
|
||||
if old != nil {
|
||||
defaultService.Store(old)
|
||||
}
|
||||
}()
|
||||
|
||||
svc := newGlobalTestService(t)
|
||||
|
||||
err := SetDefault(svc)
|
||||
require.NoError(t, err)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 100; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
s := Default()
|
||||
assert.NotNil(t, s)
|
||||
assert.Equal(t, svc, s)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestGlobal_ConcurrentSetDefault(t *testing.T) {
|
||||
old := defaultService.Swap(nil)
|
||||
defer func() {
|
||||
if old != nil {
|
||||
defaultService.Store(old)
|
||||
}
|
||||
}()
|
||||
|
||||
var services []*Service
|
||||
for i := 0; i < 10; i++ {
|
||||
svc := newGlobalTestService(t)
|
||||
services = append(services, svc)
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for _, svc := range services {
|
||||
wg.Add(1)
|
||||
go func(s *Service) {
|
||||
defer wg.Done()
|
||||
_ = SetDefault(s)
|
||||
}(svc)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
final := Default()
|
||||
assert.NotNil(t, final)
|
||||
|
||||
found := false
|
||||
for _, svc := range services {
|
||||
if svc == final {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.True(t, found, "Default should be one of the set services")
|
||||
}
|
||||
|
||||
func TestGlobal_ConcurrentOperations(t *testing.T) {
|
||||
old := defaultService.Swap(nil)
|
||||
defer func() {
|
||||
if old != nil {
|
||||
defaultService.Store(old)
|
||||
}
|
||||
}()
|
||||
|
||||
svc := newGlobalTestService(t)
|
||||
|
||||
err := SetDefault(svc)
|
||||
require.NoError(t, err)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var processes []*Process
|
||||
var procMu sync.Mutex
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
proc, err := Start(context.Background(), "echo", "concurrent")
|
||||
if err == nil {
|
||||
procMu.Lock()
|
||||
processes = append(processes, proc)
|
||||
procMu.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_ = List()
|
||||
_ = Running()
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
procMu.Lock()
|
||||
for _, p := range processes {
|
||||
<-p.Done()
|
||||
}
|
||||
procMu.Unlock()
|
||||
|
||||
assert.Len(t, processes, 20)
|
||||
|
||||
var wg2 sync.WaitGroup
|
||||
for _, p := range processes {
|
||||
wg2.Add(1)
|
||||
go func(id string) {
|
||||
defer wg2.Done()
|
||||
got, err := Get(id)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, got)
|
||||
}(p.ID)
|
||||
}
|
||||
wg2.Wait()
|
||||
}
|
||||
|
||||
func TestGlobal_StartWithOptions(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
old := defaultService.Swap(svc)
|
||||
defer func() {
|
||||
if old != nil {
|
||||
defaultService.Store(old)
|
||||
}
|
||||
}()
|
||||
|
||||
proc, err := StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "echo",
|
||||
Args: []string{"with", "options"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
assert.Equal(t, 0, proc.ExitCode)
|
||||
assert.Contains(t, proc.Output(), "with options")
|
||||
}
|
||||
|
||||
func TestGlobal_RunWithOptions(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
old := defaultService.Swap(svc)
|
||||
defer func() {
|
||||
if old != nil {
|
||||
defaultService.Store(old)
|
||||
}
|
||||
}()
|
||||
|
||||
output, err := RunWithOptions(context.Background(), RunOptions{
|
||||
Command: "echo",
|
||||
Args: []string{"run", "options"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, output, "run options")
|
||||
}
|
||||
|
||||
func TestGlobal_Running(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
old := defaultService.Swap(svc)
|
||||
defer func() {
|
||||
if old != nil {
|
||||
defaultService.Store(old)
|
||||
}
|
||||
}()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc, err := Start(ctx, "sleep", "60")
|
||||
require.NoError(t, err)
|
||||
|
||||
running := Running()
|
||||
assert.Len(t, running, 1)
|
||||
assert.Equal(t, proc.ID, running[0].ID)
|
||||
|
||||
cancel()
|
||||
<-proc.Done()
|
||||
|
||||
running = Running()
|
||||
assert.Len(t, running, 0)
|
||||
}
|
||||
15
go.mod
15
go.mod
|
|
@ -3,16 +3,16 @@ module dappco.re/go/core/process
|
|||
go 1.26.0
|
||||
|
||||
require (
|
||||
dappco.re/go/core v0.4.7
|
||||
dappco.re/go/core/io v0.1.7
|
||||
dappco.re/go/core/log v0.0.4
|
||||
dappco.re/go/core/ws v0.2.4
|
||||
dappco.re/go/core v0.8.0-alpha.1
|
||||
dappco.re/go/core/io v0.2.0
|
||||
dappco.re/go/core/ws v0.3.0
|
||||
forge.lthn.ai/core/api v0.1.5
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/stretchr/testify v1.11.1
|
||||
)
|
||||
|
||||
require (
|
||||
dappco.re/go/core/log v0.1.0 // indirect
|
||||
forge.lthn.ai/core/go-io v0.1.5 // indirect
|
||||
forge.lthn.ai/core/go-log v0.0.4 // indirect
|
||||
github.com/99designs/gqlgen v0.17.88 // indirect
|
||||
|
|
@ -108,10 +108,3 @@ require (
|
|||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
replace (
|
||||
dappco.re/go/core => ../go
|
||||
dappco.re/go/core/io => ../go-io
|
||||
dappco.re/go/core/log => ../go-log
|
||||
dappco.re/go/core/ws => ../go-ws
|
||||
)
|
||||
|
|
|
|||
8
go.sum
8
go.sum
|
|
@ -1,3 +1,11 @@
|
|||
dappco.re/go/core v0.8.0-alpha.1 h1:gj7+Scv+L63Z7wMxbJYHhaRFkHJo2u4MMPuUSv/Dhtk=
|
||||
dappco.re/go/core v0.8.0-alpha.1/go.mod h1:f2/tBZ3+3IqDrg2F5F598llv0nmb/4gJVCFzM5geE4A=
|
||||
dappco.re/go/core/io v0.2.0 h1:zuudgIiTsQQ5ipVt97saWdGLROovbEB/zdVyy9/l+I4=
|
||||
dappco.re/go/core/io v0.2.0/go.mod h1:1QnQV6X9LNgFKfm8SkOtR9LLaj3bDcsOIeJOOyjbL5E=
|
||||
dappco.re/go/core/log v0.1.0 h1:pa71Vq2TD2aoEUQWFKwNcaJ3GBY8HbaNGqtE688Unyc=
|
||||
dappco.re/go/core/log v0.1.0/go.mod h1:Nkqb8gsXhZAO8VLpx7B8i1iAmohhzqA20b9Zr8VUcJs=
|
||||
dappco.re/go/core/ws v0.3.0 h1:ZxR8y5pfrWvnCHVN7qExXz7fdP5a063uNqyqE0Ab8pQ=
|
||||
dappco.re/go/core/ws v0.3.0/go.mod h1:aLyXrJnbCOGL0SW9rC1EHAAIS83w3djO374gHIz4Nic=
|
||||
forge.lthn.ai/core/api v0.1.5 h1:NwZrcOyBjaiz5/cn0n0tnlMUodi8Or6FHMx59C7Kv2o=
|
||||
forge.lthn.ai/core/api v0.1.5/go.mod h1:PBnaWyOVXSOGy+0x2XAPUFMYJxQ2CNhppia/D06ZPII=
|
||||
forge.lthn.ai/core/go-io v0.1.5 h1:+XJ1YhaGGFLGtcNbPtVlndTjk+pO0Ydi2hRDj5/cHOM=
|
||||
|
|
|
|||
34
health.go
34
health.go
|
|
@ -2,19 +2,22 @@ package process
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
"dappco.re/go/core"
|
||||
)
|
||||
|
||||
// HealthCheck is a function that returns nil if healthy.
|
||||
//
|
||||
// check := process.HealthCheck(func() error { return nil })
|
||||
type HealthCheck func() error
|
||||
|
||||
// HealthServer provides HTTP /health and /ready endpoints for process monitoring.
|
||||
//
|
||||
// hs := process.NewHealthServer("127.0.0.1:0")
|
||||
type HealthServer struct {
|
||||
addr string
|
||||
server *http.Server
|
||||
|
|
@ -25,6 +28,8 @@ type HealthServer struct {
|
|||
}
|
||||
|
||||
// NewHealthServer creates a health check server on the given address.
|
||||
//
|
||||
// hs := process.NewHealthServer("127.0.0.1:0")
|
||||
func NewHealthServer(addr string) *HealthServer {
|
||||
return &HealthServer{
|
||||
addr: addr,
|
||||
|
|
@ -33,13 +38,16 @@ func NewHealthServer(addr string) *HealthServer {
|
|||
}
|
||||
|
||||
// AddCheck registers a health check function.
|
||||
//
|
||||
// health.AddCheck(func() error { return db.Ping() })
|
||||
func (h *HealthServer) AddCheck(check HealthCheck) {
|
||||
h.mu.Lock()
|
||||
h.checks = append(h.checks, check)
|
||||
h.mu.Unlock()
|
||||
}
|
||||
|
||||
// SetReady sets the readiness status.
|
||||
// health.SetReady(true) // mark ready for traffic
|
||||
// health.SetReady(false) // mark not-ready during shutdown
|
||||
func (h *HealthServer) SetReady(ready bool) {
|
||||
h.mu.Lock()
|
||||
h.ready = ready
|
||||
|
|
@ -47,6 +55,8 @@ func (h *HealthServer) SetReady(ready bool) {
|
|||
}
|
||||
|
||||
// Start begins serving health check endpoints.
|
||||
//
|
||||
// err := health.Start()
|
||||
func (h *HealthServer) Start() error {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
|
|
@ -58,13 +68,13 @@ func (h *HealthServer) Start() error {
|
|||
for _, check := range checks {
|
||||
if err := check(); err != nil {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, _ = fmt.Fprintf(w, "unhealthy: %v\n", err)
|
||||
_, _ = w.Write([]byte("unhealthy: " + err.Error() + "\n"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = fmt.Fprintln(w, "ok")
|
||||
_, _ = w.Write([]byte("ok\n"))
|
||||
})
|
||||
|
||||
mux.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -74,17 +84,17 @@ func (h *HealthServer) Start() error {
|
|||
|
||||
if !ready {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, _ = fmt.Fprintln(w, "not ready")
|
||||
_, _ = w.Write([]byte("not ready\n"))
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = fmt.Fprintln(w, "ready")
|
||||
_, _ = w.Write([]byte("ready\n"))
|
||||
})
|
||||
|
||||
listener, err := net.Listen("tcp", h.addr)
|
||||
if err != nil {
|
||||
return coreerr.E("HealthServer.Start", fmt.Sprintf("failed to listen on %s", h.addr), err)
|
||||
return core.E("health.start", core.Concat("failed to listen on ", h.addr), err)
|
||||
}
|
||||
|
||||
h.listener = listener
|
||||
|
|
@ -98,6 +108,8 @@ func (h *HealthServer) Start() error {
|
|||
}
|
||||
|
||||
// Stop gracefully shuts down the health server.
|
||||
//
|
||||
// err := health.Stop(ctx)
|
||||
func (h *HealthServer) Stop(ctx context.Context) error {
|
||||
if h.server == nil {
|
||||
return nil
|
||||
|
|
@ -106,6 +118,8 @@ func (h *HealthServer) Stop(ctx context.Context) error {
|
|||
}
|
||||
|
||||
// Addr returns the actual address the server is listening on.
|
||||
//
|
||||
// addr := health.Addr() // e.g. "127.0.0.1:9090"
|
||||
func (h *HealthServer) Addr() string {
|
||||
if h.listener != nil {
|
||||
return h.listener.Addr().String()
|
||||
|
|
@ -115,9 +129,11 @@ func (h *HealthServer) Addr() string {
|
|||
|
||||
// WaitForHealth polls a health endpoint until it responds 200 or the timeout
|
||||
// (in milliseconds) expires. Returns true if healthy, false on timeout.
|
||||
//
|
||||
// ok := process.WaitForHealth("127.0.0.1:9000", 2_000)
|
||||
func WaitForHealth(addr string, timeoutMs int) bool {
|
||||
deadline := time.Now().Add(time.Duration(timeoutMs) * time.Millisecond)
|
||||
url := fmt.Sprintf("http://%s/health", addr)
|
||||
url := core.Concat("http://", addr, "/health")
|
||||
|
||||
client := &http.Client{Timeout: 2 * time.Second}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHealthServer_Endpoints(t *testing.T) {
|
||||
func TestHealthServer_Endpoints_Good(t *testing.T) {
|
||||
hs := NewHealthServer("127.0.0.1:0")
|
||||
err := hs.Start()
|
||||
require.NoError(t, err)
|
||||
|
|
@ -36,7 +36,7 @@ func TestHealthServer_Endpoints(t *testing.T) {
|
|||
_ = resp.Body.Close()
|
||||
}
|
||||
|
||||
func TestHealthServer_WithChecks(t *testing.T) {
|
||||
func TestHealthServer_WithChecks_Good(t *testing.T) {
|
||||
hs := NewHealthServer("127.0.0.1:0")
|
||||
|
||||
healthy := true
|
||||
|
|
@ -66,7 +66,7 @@ func TestHealthServer_WithChecks(t *testing.T) {
|
|||
_ = resp.Body.Close()
|
||||
}
|
||||
|
||||
func TestWaitForHealth_Reachable(t *testing.T) {
|
||||
func TestWaitForHealth_Reachable_Good(t *testing.T) {
|
||||
hs := NewHealthServer("127.0.0.1:0")
|
||||
require.NoError(t, hs.Start())
|
||||
defer func() { _ = hs.Stop(context.Background()) }()
|
||||
|
|
@ -75,7 +75,37 @@ func TestWaitForHealth_Reachable(t *testing.T) {
|
|||
assert.True(t, ok)
|
||||
}
|
||||
|
||||
func TestWaitForHealth_Unreachable(t *testing.T) {
|
||||
func TestWaitForHealth_Unreachable_Bad(t *testing.T) {
|
||||
ok := WaitForHealth("127.0.0.1:19999", 500)
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestHealthServer_Endpoints_Bad(t *testing.T) {
|
||||
t.Run("listen fails on invalid address", func(t *testing.T) {
|
||||
hs := NewHealthServer("invalid-addr-xyz:99999")
|
||||
err := hs.Start()
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestHealthServer_Endpoints_Ugly(t *testing.T) {
|
||||
t.Run("addr before start returns configured address", func(t *testing.T) {
|
||||
hs := NewHealthServer("127.0.0.1:0")
|
||||
// Before Start, Addr() returns the configured address (not yet bound)
|
||||
assert.Equal(t, "127.0.0.1:0", hs.Addr())
|
||||
})
|
||||
|
||||
t.Run("stop before start is a no-op", func(t *testing.T) {
|
||||
hs := NewHealthServer("127.0.0.1:0")
|
||||
err := hs.Stop(context.Background())
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWaitForHealth_Reachable_Ugly(t *testing.T) {
|
||||
t.Run("zero timeout returns false immediately", func(t *testing.T) {
|
||||
// With 0ms timeout, should return false without waiting
|
||||
ok := WaitForHealth("127.0.0.1:19998", 0)
|
||||
assert.False(t, ok)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
28
pidfile.go
28
pidfile.go
|
|
@ -1,16 +1,14 @@
|
|||
package process
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"bytes"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"dappco.re/go/core"
|
||||
coreio "dappco.re/go/core/io"
|
||||
coreerr "dappco.re/go/core/log"
|
||||
)
|
||||
|
||||
// PIDFile manages a process ID file for single-instance enforcement.
|
||||
|
|
@ -31,26 +29,26 @@ func (p *PIDFile) Acquire() error {
|
|||
defer p.mu.Unlock()
|
||||
|
||||
if data, err := coreio.Local.Read(p.path); err == nil {
|
||||
pid, err := strconv.Atoi(strings.TrimSpace(data))
|
||||
pid, err := strconv.Atoi(string(bytes.TrimSpace([]byte(data))))
|
||||
if err == nil && pid > 0 {
|
||||
if proc, err := os.FindProcess(pid); err == nil {
|
||||
if proc, err := processHandle(pid); err == nil {
|
||||
if err := proc.Signal(syscall.Signal(0)); err == nil {
|
||||
return coreerr.E("PIDFile.Acquire", fmt.Sprintf("another instance is running (PID %d)", pid), nil)
|
||||
return core.E("pidfile.acquire", core.Concat("another instance is running (PID ", strconv.Itoa(pid), ")"), nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = coreio.Local.Delete(p.path)
|
||||
}
|
||||
|
||||
if dir := filepath.Dir(p.path); dir != "." {
|
||||
if dir := path.Dir(p.path); dir != "." {
|
||||
if err := coreio.Local.EnsureDir(dir); err != nil {
|
||||
return coreerr.E("PIDFile.Acquire", "failed to create PID directory", err)
|
||||
return core.E("pidfile.acquire", "failed to create PID directory", err)
|
||||
}
|
||||
}
|
||||
|
||||
pid := os.Getpid()
|
||||
pid := currentPID()
|
||||
if err := coreio.Local.Write(p.path, strconv.Itoa(pid)); err != nil {
|
||||
return coreerr.E("PIDFile.Acquire", "failed to write PID file", err)
|
||||
return core.E("pidfile.acquire", "failed to write PID file", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -61,7 +59,7 @@ func (p *PIDFile) Release() error {
|
|||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if err := coreio.Local.Delete(p.path); err != nil {
|
||||
return coreerr.E("PIDFile.Release", "failed to remove PID file", err)
|
||||
return core.E("pidfile.release", "failed to remove PID file", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -80,12 +78,12 @@ func ReadPID(path string) (int, bool) {
|
|||
return 0, false
|
||||
}
|
||||
|
||||
pid, err := strconv.Atoi(strings.TrimSpace(data))
|
||||
pid, err := strconv.Atoi(string(bytes.TrimSpace([]byte(data))))
|
||||
if err != nil || pid <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
proc, err := os.FindProcess(pid)
|
||||
proc, err := processHandle(pid)
|
||||
if err != nil {
|
||||
return pid, false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,15 +2,15 @@ package process
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPIDFile_AcquireAndRelease(t *testing.T) {
|
||||
pidPath := filepath.Join(t.TempDir(), "test.pid")
|
||||
func TestPIDFile_Acquire_Good(t *testing.T) {
|
||||
pidPath := core.JoinPath(t.TempDir(), "test.pid")
|
||||
pid := NewPIDFile(pidPath)
|
||||
err := pid.Acquire()
|
||||
require.NoError(t, err)
|
||||
|
|
@ -23,8 +23,8 @@ func TestPIDFile_AcquireAndRelease(t *testing.T) {
|
|||
assert.True(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestPIDFile_StalePID(t *testing.T) {
|
||||
pidPath := filepath.Join(t.TempDir(), "stale.pid")
|
||||
func TestPIDFile_AcquireStale_Good(t *testing.T) {
|
||||
pidPath := core.JoinPath(t.TempDir(), "stale.pid")
|
||||
require.NoError(t, os.WriteFile(pidPath, []byte("999999999"), 0644))
|
||||
pid := NewPIDFile(pidPath)
|
||||
err := pid.Acquire()
|
||||
|
|
@ -33,8 +33,8 @@ func TestPIDFile_StalePID(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestPIDFile_CreatesParentDirectory(t *testing.T) {
|
||||
pidPath := filepath.Join(t.TempDir(), "subdir", "nested", "test.pid")
|
||||
func TestPIDFile_CreateDirectory_Good(t *testing.T) {
|
||||
pidPath := core.JoinPath(t.TempDir(), "subdir", "nested", "test.pid")
|
||||
pid := NewPIDFile(pidPath)
|
||||
err := pid.Acquire()
|
||||
require.NoError(t, err)
|
||||
|
|
@ -42,29 +42,63 @@ func TestPIDFile_CreatesParentDirectory(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestPIDFile_Path(t *testing.T) {
|
||||
func TestPIDFile_Path_Good(t *testing.T) {
|
||||
pid := NewPIDFile("/tmp/test.pid")
|
||||
assert.Equal(t, "/tmp/test.pid", pid.Path())
|
||||
}
|
||||
|
||||
func TestReadPID_Missing(t *testing.T) {
|
||||
func TestReadPID_Missing_Bad(t *testing.T) {
|
||||
pid, running := ReadPID("/nonexistent/path.pid")
|
||||
assert.Equal(t, 0, pid)
|
||||
assert.False(t, running)
|
||||
}
|
||||
|
||||
func TestReadPID_InvalidContent(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "bad.pid")
|
||||
func TestReadPID_Invalid_Bad(t *testing.T) {
|
||||
path := core.JoinPath(t.TempDir(), "bad.pid")
|
||||
require.NoError(t, os.WriteFile(path, []byte("notanumber"), 0644))
|
||||
pid, running := ReadPID(path)
|
||||
assert.Equal(t, 0, pid)
|
||||
assert.False(t, running)
|
||||
}
|
||||
|
||||
func TestReadPID_StalePID(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "stale.pid")
|
||||
func TestReadPID_Stale_Bad(t *testing.T) {
|
||||
path := core.JoinPath(t.TempDir(), "stale.pid")
|
||||
require.NoError(t, os.WriteFile(path, []byte("999999999"), 0644))
|
||||
pid, running := ReadPID(path)
|
||||
assert.Equal(t, 999999999, pid)
|
||||
assert.False(t, running)
|
||||
}
|
||||
|
||||
func TestPIDFile_Acquire_Ugly(t *testing.T) {
|
||||
t.Run("double acquire from same instance returns error", func(t *testing.T) {
|
||||
pidPath := core.JoinPath(t.TempDir(), "double.pid")
|
||||
pid := NewPIDFile(pidPath)
|
||||
|
||||
err := pid.Acquire()
|
||||
require.NoError(t, err)
|
||||
defer func() { _ = pid.Release() }()
|
||||
|
||||
// Second acquire should fail — the current process is running
|
||||
err = pid.Acquire()
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "another instance is running")
|
||||
})
|
||||
|
||||
t.Run("release of non-existent file returns error", func(t *testing.T) {
|
||||
pidPath := core.JoinPath(t.TempDir(), "gone.pid")
|
||||
pid := NewPIDFile(pidPath)
|
||||
// Release without acquire — file doesn't exist
|
||||
err := pid.Release()
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestReadPID_Missing_Ugly(t *testing.T) {
|
||||
t.Run("zero byte pid file is invalid", func(t *testing.T) {
|
||||
pidPath := core.JoinPath(t.TempDir(), "empty.pid")
|
||||
require.NoError(t, os.WriteFile(pidPath, []byte(""), 0644))
|
||||
pid, running := ReadPID(pidPath)
|
||||
assert.Equal(t, 0, pid)
|
||||
assert.False(t, running)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,7 @@ package api
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"forge.lthn.ai/core/api"
|
||||
"forge.lthn.ai/core/api/pkg/provider"
|
||||
|
|
@ -189,13 +187,8 @@ func (p *ProcessProvider) stopDaemon(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
// Send SIGTERM to the process
|
||||
proc, err := os.FindProcess(entry.PID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, api.Fail("signal_failed", err.Error()))
|
||||
return
|
||||
}
|
||||
if err := proc.Signal(syscall.SIGTERM); err != nil {
|
||||
// Send SIGTERM to the process via the process package abstraction
|
||||
if err := process.KillPID(entry.PID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, api.Fail("signal_failed", err.Error()))
|
||||
return
|
||||
}
|
||||
|
|
@ -266,15 +259,10 @@ func (p *ProcessProvider) emitEvent(channel string, data any) {
|
|||
|
||||
// PIDAlive checks whether a PID is still running. Exported for use by
|
||||
// consumers that need to verify daemon liveness outside the REST API.
|
||||
//
|
||||
// alive := api.PIDAlive(entry.PID)
|
||||
func PIDAlive(pid int) bool {
|
||||
if pid <= 0 {
|
||||
return false
|
||||
}
|
||||
proc, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return proc.Signal(syscall.Signal(0)) == nil
|
||||
return process.IsPIDAlive(pid)
|
||||
}
|
||||
|
||||
// intParam parses a URL param as int, returning 0 on failure.
|
||||
|
|
|
|||
|
|
@ -3,14 +3,13 @@
|
|||
package api_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
goapi "forge.lthn.ai/core/api"
|
||||
process "dappco.re/go/core/process"
|
||||
processapi "dappco.re/go/core/process/pkg/api"
|
||||
goapi "forge.lthn.ai/core/api"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -65,10 +64,8 @@ func TestProcessProvider_ListDaemons_Good(t *testing.T) {
|
|||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
var resp goapi.Response[[]any]
|
||||
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, resp.Success)
|
||||
body := w.Body.String()
|
||||
assert.NotEmpty(t, body)
|
||||
}
|
||||
|
||||
func TestProcessProvider_GetDaemon_Bad(t *testing.T) {
|
||||
|
|
@ -95,7 +92,7 @@ func TestProcessProvider_RegistersAsRouteGroup_Good(t *testing.T) {
|
|||
assert.Equal(t, "process", engine.Groups()[0].Name())
|
||||
}
|
||||
|
||||
func TestProcessProvider_Channels_RegisterAsStreamGroup_Good(t *testing.T) {
|
||||
func TestProcessProvider_StreamGroup_Good(t *testing.T) {
|
||||
p := processapi.NewProvider(nil, nil)
|
||||
|
||||
engine, err := goapi.New()
|
||||
|
|
@ -108,6 +105,71 @@ func TestProcessProvider_Channels_RegisterAsStreamGroup_Good(t *testing.T) {
|
|||
assert.Contains(t, channels, "process.daemon.started")
|
||||
}
|
||||
|
||||
func TestProcessProvider_ListDaemons_Bad(t *testing.T) {
|
||||
t.Run("get non-existent daemon returns 404", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
registry := newTestRegistry(dir)
|
||||
p := processapi.NewProvider(registry, nil)
|
||||
|
||||
r := setupRouter(p)
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/process/daemons/nope/missing", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessProvider_ListDaemons_Ugly(t *testing.T) {
|
||||
t.Run("nil registry falls back to default", func(t *testing.T) {
|
||||
p := processapi.NewProvider(nil, nil)
|
||||
r := setupRouter(p)
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/process/daemons", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
// Should succeed — default registry returns empty list
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessProvider_Element_Good(t *testing.T) {
|
||||
p := processapi.NewProvider(nil, nil)
|
||||
element := p.Element()
|
||||
assert.Equal(t, "core-process-panel", element.Tag)
|
||||
assert.NotEmpty(t, element.Source)
|
||||
}
|
||||
|
||||
func TestProcessProvider_Element_Bad(t *testing.T) {
|
||||
t.Run("stop non-existent daemon returns 404", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
registry := newTestRegistry(dir)
|
||||
p := processapi.NewProvider(registry, nil)
|
||||
|
||||
r := setupRouter(p)
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/process/daemons/nope/missing/stop", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessProvider_Element_Ugly(t *testing.T) {
|
||||
t.Run("health check on non-existent daemon returns 404", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
registry := newTestRegistry(dir)
|
||||
p := processapi.NewProvider(registry, nil)
|
||||
|
||||
r := setupRouter(p)
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/process/daemons/nope/missing/health", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
})
|
||||
}
|
||||
|
||||
// -- Test helpers -------------------------------------------------------------
|
||||
|
||||
func setupRouter(p *processapi.ProcessProvider) *gin.Engine {
|
||||
|
|
|
|||
115
process.go
115
process.go
|
|
@ -2,20 +2,23 @@ package process
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
"dappco.re/go/core"
|
||||
)
|
||||
|
||||
// Process represents a managed external process.
|
||||
type Process struct {
|
||||
type processStdin interface {
|
||||
Write(p []byte) (n int, err error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// ManagedProcess represents a tracked external process started by the service.
|
||||
type ManagedProcess struct {
|
||||
ID string
|
||||
PID int
|
||||
Command string
|
||||
Args []string
|
||||
Dir string
|
||||
|
|
@ -25,42 +28,43 @@ type Process struct {
|
|||
ExitCode int
|
||||
Duration time.Duration
|
||||
|
||||
cmd *exec.Cmd
|
||||
cmd *execCmd
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
output *RingBuffer
|
||||
stdin io.WriteCloser
|
||||
stdin processStdin
|
||||
done chan struct{}
|
||||
mu sync.RWMutex
|
||||
gracePeriod time.Duration
|
||||
killGroup bool
|
||||
lastSignal string
|
||||
}
|
||||
|
||||
// Info returns a snapshot of process state.
|
||||
func (p *Process) Info() Info {
|
||||
// Process is kept as a compatibility alias for ManagedProcess.
|
||||
type Process = ManagedProcess
|
||||
|
||||
// info := proc.Info()
|
||||
// fmt.Println(info.Status, info.ExitCode)
|
||||
func (p *ManagedProcess) Info() ProcessInfo {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
|
||||
pid := 0
|
||||
if p.cmd != nil && p.cmd.Process != nil {
|
||||
pid = p.cmd.Process.Pid
|
||||
}
|
||||
|
||||
return Info{
|
||||
return ProcessInfo{
|
||||
ID: p.ID,
|
||||
Command: p.Command,
|
||||
Args: p.Args,
|
||||
Args: append([]string(nil), p.Args...),
|
||||
Dir: p.Dir,
|
||||
StartedAt: p.StartedAt,
|
||||
Running: p.Status == StatusRunning,
|
||||
Status: p.Status,
|
||||
ExitCode: p.ExitCode,
|
||||
Duration: p.Duration,
|
||||
PID: pid,
|
||||
PID: p.PID,
|
||||
}
|
||||
}
|
||||
|
||||
// Output returns the captured output as a string.
|
||||
func (p *Process) Output() string {
|
||||
// output := proc.Output() // returns combined stdout+stderr
|
||||
func (p *ManagedProcess) Output() string {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
if p.output == nil {
|
||||
|
|
@ -69,8 +73,8 @@ func (p *Process) Output() string {
|
|||
return p.output.String()
|
||||
}
|
||||
|
||||
// OutputBytes returns the captured output as bytes.
|
||||
func (p *Process) OutputBytes() []byte {
|
||||
// data := proc.OutputBytes() // nil if capture is disabled
|
||||
func (p *ManagedProcess) OutputBytes() []byte {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
if p.output == nil {
|
||||
|
|
@ -79,38 +83,41 @@ func (p *Process) OutputBytes() []byte {
|
|||
return p.output.Bytes()
|
||||
}
|
||||
|
||||
// IsRunning returns true if the process is still executing.
|
||||
func (p *Process) IsRunning() bool {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
return p.Status == StatusRunning
|
||||
// if proc.IsRunning() { log.Println("still running") }
|
||||
func (p *ManagedProcess) IsRunning() bool {
|
||||
select {
|
||||
case <-p.done:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Wait blocks until the process exits.
|
||||
func (p *Process) Wait() error {
|
||||
// if err := proc.Wait(); err != nil { /* non-zero exit or killed */ }
|
||||
func (p *ManagedProcess) Wait() error {
|
||||
<-p.done
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
if p.Status == StatusFailed {
|
||||
return coreerr.E("Process.Wait", fmt.Sprintf("process failed to start: %s", p.ID), nil)
|
||||
return core.E("process.wait", core.Concat("process failed to start: ", p.ID), nil)
|
||||
}
|
||||
if p.Status == StatusKilled {
|
||||
return coreerr.E("Process.Wait", fmt.Sprintf("process was killed: %s", p.ID), nil)
|
||||
return core.E("process.wait", core.Concat("process was killed: ", p.ID), nil)
|
||||
}
|
||||
if p.ExitCode != 0 {
|
||||
return coreerr.E("Process.Wait", fmt.Sprintf("process exited with code %d", p.ExitCode), nil)
|
||||
return core.E("process.wait", core.Concat("process exited with code ", strconv.Itoa(p.ExitCode)), nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Done returns a channel that closes when the process exits.
|
||||
func (p *Process) Done() <-chan struct{} {
|
||||
// <-proc.Done() // blocks until process exits
|
||||
func (p *ManagedProcess) Done() <-chan struct{} {
|
||||
return p.done
|
||||
}
|
||||
|
||||
// Kill forcefully terminates the process.
|
||||
// If KillGroup is set, kills the entire process group.
|
||||
func (p *Process) Kill() error {
|
||||
func (p *ManagedProcess) Kill() error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
|
|
@ -122,6 +129,7 @@ func (p *Process) Kill() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
p.lastSignal = "SIGKILL"
|
||||
if p.killGroup {
|
||||
// Kill entire process group (negative PID)
|
||||
return syscall.Kill(-p.cmd.Process.Pid, syscall.SIGKILL)
|
||||
|
|
@ -132,7 +140,7 @@ func (p *Process) Kill() error {
|
|||
// Shutdown gracefully stops the process: SIGTERM, then SIGKILL after grace period.
|
||||
// If GracePeriod was not set (zero), falls back to immediate Kill().
|
||||
// If KillGroup is set, signals are sent to the entire process group.
|
||||
func (p *Process) Shutdown() error {
|
||||
func (p *ManagedProcess) Shutdown() error {
|
||||
p.mu.RLock()
|
||||
grace := p.gracePeriod
|
||||
p.mu.RUnlock()
|
||||
|
|
@ -156,7 +164,7 @@ func (p *Process) Shutdown() error {
|
|||
}
|
||||
|
||||
// terminate sends SIGTERM to the process (or process group if KillGroup is set).
|
||||
func (p *Process) terminate() error {
|
||||
func (p *ManagedProcess) terminate() error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
|
|
@ -172,27 +180,12 @@ func (p *Process) terminate() error {
|
|||
if p.killGroup {
|
||||
pid = -pid
|
||||
}
|
||||
p.lastSignal = "SIGTERM"
|
||||
return syscall.Kill(pid, syscall.SIGTERM)
|
||||
}
|
||||
|
||||
// Signal sends a signal to the process.
|
||||
func (p *Process) Signal(sig os.Signal) error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
if p.Status != StatusRunning {
|
||||
return ErrProcessNotRunning
|
||||
}
|
||||
|
||||
if p.cmd == nil || p.cmd.Process == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return p.cmd.Process.Signal(sig)
|
||||
}
|
||||
|
||||
// SendInput writes to the process stdin.
|
||||
func (p *Process) SendInput(input string) error {
|
||||
// _ = proc.SendInput("yes\n") // write to process stdin
|
||||
func (p *ManagedProcess) SendInput(input string) error {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
|
||||
|
|
@ -208,8 +201,8 @@ func (p *Process) SendInput(input string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// CloseStdin closes the process stdin pipe.
|
||||
func (p *Process) CloseStdin() error {
|
||||
// _ = proc.CloseStdin() // signals EOF to the subprocess
|
||||
func (p *ManagedProcess) CloseStdin() error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
|
|
@ -221,3 +214,9 @@ func (p *Process) CloseStdin() error {
|
|||
p.stdin = nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *ManagedProcess) requestedSignal() string {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
return p.lastSignal
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,130 +0,0 @@
|
|||
package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"dappco.re/go/core"
|
||||
coreerr "dappco.re/go/core/log"
|
||||
)
|
||||
|
||||
// Global default service (follows i18n pattern).
|
||||
var (
|
||||
defaultService atomic.Pointer[Service]
|
||||
defaultOnce sync.Once
|
||||
defaultErr error
|
||||
)
|
||||
|
||||
// Default returns the global process service.
|
||||
// Returns nil if not initialized.
|
||||
func Default() *Service {
|
||||
return defaultService.Load()
|
||||
}
|
||||
|
||||
// SetDefault sets the global process service.
|
||||
// Thread-safe: can be called concurrently with Default().
|
||||
func SetDefault(s *Service) error {
|
||||
if s == nil {
|
||||
return ErrSetDefaultNil
|
||||
}
|
||||
defaultService.Store(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Init initializes the default global service with a Core instance.
|
||||
// This is typically called during application startup.
|
||||
func Init(c *core.Core) error {
|
||||
defaultOnce.Do(func() {
|
||||
factory := NewService(Options{})
|
||||
svc, err := factory(c)
|
||||
if err != nil {
|
||||
defaultErr = err
|
||||
return
|
||||
}
|
||||
defaultService.Store(svc.(*Service))
|
||||
})
|
||||
return defaultErr
|
||||
}
|
||||
|
||||
// --- Global convenience functions ---
|
||||
|
||||
// Start spawns a new process using the default service.
|
||||
func Start(ctx context.Context, command string, args ...string) (*Process, error) {
|
||||
svc := Default()
|
||||
if svc == nil {
|
||||
return nil, ErrServiceNotInitialized
|
||||
}
|
||||
return svc.Start(ctx, command, args...)
|
||||
}
|
||||
|
||||
// Run executes a command and waits for completion using the default service.
|
||||
func Run(ctx context.Context, command string, args ...string) (string, error) {
|
||||
svc := Default()
|
||||
if svc == nil {
|
||||
return "", ErrServiceNotInitialized
|
||||
}
|
||||
return svc.Run(ctx, command, args...)
|
||||
}
|
||||
|
||||
// Get returns a process by ID from the default service.
|
||||
func Get(id string) (*Process, error) {
|
||||
svc := Default()
|
||||
if svc == nil {
|
||||
return nil, ErrServiceNotInitialized
|
||||
}
|
||||
return svc.Get(id)
|
||||
}
|
||||
|
||||
// List returns all processes from the default service.
|
||||
func List() []*Process {
|
||||
svc := Default()
|
||||
if svc == nil {
|
||||
return nil
|
||||
}
|
||||
return svc.List()
|
||||
}
|
||||
|
||||
// Kill terminates a process by ID using the default service.
|
||||
func Kill(id string) error {
|
||||
svc := Default()
|
||||
if svc == nil {
|
||||
return ErrServiceNotInitialized
|
||||
}
|
||||
return svc.Kill(id)
|
||||
}
|
||||
|
||||
// StartWithOptions spawns a process with full configuration using the default service.
|
||||
func StartWithOptions(ctx context.Context, opts RunOptions) (*Process, error) {
|
||||
svc := Default()
|
||||
if svc == nil {
|
||||
return nil, ErrServiceNotInitialized
|
||||
}
|
||||
return svc.StartWithOptions(ctx, opts)
|
||||
}
|
||||
|
||||
// RunWithOptions executes a command with options and waits using the default service.
|
||||
func RunWithOptions(ctx context.Context, opts RunOptions) (string, error) {
|
||||
svc := Default()
|
||||
if svc == nil {
|
||||
return "", ErrServiceNotInitialized
|
||||
}
|
||||
return svc.RunWithOptions(ctx, opts)
|
||||
}
|
||||
|
||||
// Running returns all currently running processes from the default service.
|
||||
func Running() []*Process {
|
||||
svc := Default()
|
||||
if svc == nil {
|
||||
return nil
|
||||
}
|
||||
return svc.Running()
|
||||
}
|
||||
|
||||
// Errors
|
||||
var (
|
||||
// ErrServiceNotInitialized is returned when the service is not initialized.
|
||||
ErrServiceNotInitialized = coreerr.E("", "process: service not initialized; call process.Init(core) first", nil)
|
||||
// ErrSetDefaultNil is returned when SetDefault is called with nil.
|
||||
ErrSetDefaultNil = coreerr.E("", "process: SetDefault called with nil service", nil)
|
||||
)
|
||||
313
process_test.go
313
process_test.go
|
|
@ -10,11 +10,10 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestProcess_Info(t *testing.T) {
|
||||
func TestProcess_Info_Good(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "hello")
|
||||
require.NoError(t, err)
|
||||
proc := startProc(t, svc, context.Background(), "echo", "hello")
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
|
|
@ -27,216 +26,163 @@ func TestProcess_Info(t *testing.T) {
|
|||
assert.Greater(t, info.Duration, time.Duration(0))
|
||||
}
|
||||
|
||||
func TestProcess_Output(t *testing.T) {
|
||||
func TestProcess_Output_Good(t *testing.T) {
|
||||
t.Run("captures stdout", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "hello world")
|
||||
require.NoError(t, err)
|
||||
|
||||
proc := startProc(t, svc, context.Background(), "echo", "hello world")
|
||||
<-proc.Done()
|
||||
|
||||
output := proc.Output()
|
||||
assert.Contains(t, output, "hello world")
|
||||
assert.Contains(t, proc.Output(), "hello world")
|
||||
})
|
||||
|
||||
t.Run("OutputBytes returns copy", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "test")
|
||||
require.NoError(t, err)
|
||||
|
||||
proc := startProc(t, svc, context.Background(), "echo", "test")
|
||||
<-proc.Done()
|
||||
|
||||
bytes := proc.OutputBytes()
|
||||
assert.NotNil(t, bytes)
|
||||
assert.Contains(t, string(bytes), "test")
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_IsRunning(t *testing.T) {
|
||||
func TestProcess_IsRunning_Good(t *testing.T) {
|
||||
t.Run("true while running", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc, err := svc.Start(ctx, "sleep", "10")
|
||||
require.NoError(t, err)
|
||||
|
||||
proc := startProc(t, svc, ctx, "sleep", "10")
|
||||
assert.True(t, proc.IsRunning())
|
||||
|
||||
cancel()
|
||||
<-proc.Done()
|
||||
|
||||
assert.False(t, proc.IsRunning())
|
||||
})
|
||||
|
||||
t.Run("false after completion", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "done")
|
||||
require.NoError(t, err)
|
||||
|
||||
proc := startProc(t, svc, context.Background(), "echo", "done")
|
||||
<-proc.Done()
|
||||
|
||||
assert.False(t, proc.IsRunning())
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Wait(t *testing.T) {
|
||||
func TestProcess_Wait_Good(t *testing.T) {
|
||||
t.Run("returns nil on success", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "ok")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = proc.Wait()
|
||||
proc := startProc(t, svc, context.Background(), "echo", "ok")
|
||||
err := proc.Wait()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("returns error on failure", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "sh", "-c", "exit 1")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = proc.Wait()
|
||||
proc := startProc(t, svc, context.Background(), "sh", "-c", "exit 1")
|
||||
err := proc.Wait()
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Done(t *testing.T) {
|
||||
func TestProcess_Done_Good(t *testing.T) {
|
||||
t.Run("channel closes on completion", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "test")
|
||||
require.NoError(t, err)
|
||||
proc := startProc(t, svc, context.Background(), "echo", "test")
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Success - channel closed
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("Done channel should have closed")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Kill(t *testing.T) {
|
||||
func TestProcess_Kill_Good(t *testing.T) {
|
||||
t.Run("terminates running process", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc, err := svc.Start(ctx, "sleep", "60")
|
||||
require.NoError(t, err)
|
||||
|
||||
proc := startProc(t, svc, ctx, "sleep", "60")
|
||||
assert.True(t, proc.IsRunning())
|
||||
|
||||
err = proc.Kill()
|
||||
err := proc.Kill()
|
||||
assert.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Good - process terminated
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("process should have been killed")
|
||||
}
|
||||
assert.Equal(t, StatusKilled, proc.Status)
|
||||
assert.Equal(t, -1, proc.ExitCode)
|
||||
})
|
||||
|
||||
t.Run("noop on completed process", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "done")
|
||||
require.NoError(t, err)
|
||||
|
||||
proc := startProc(t, svc, context.Background(), "echo", "done")
|
||||
<-proc.Done()
|
||||
|
||||
err = proc.Kill()
|
||||
err := proc.Kill()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_SendInput(t *testing.T) {
|
||||
func TestProcess_SendInput_Good(t *testing.T) {
|
||||
t.Run("writes to stdin", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
proc := startProc(t, svc, context.Background(), "cat")
|
||||
|
||||
// Use cat to echo back stdin
|
||||
proc, err := svc.Start(context.Background(), "cat")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = proc.SendInput("hello\n")
|
||||
err := proc.SendInput("hello\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = proc.CloseStdin()
|
||||
assert.NoError(t, err)
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
assert.Contains(t, proc.Output(), "hello")
|
||||
})
|
||||
|
||||
t.Run("error on completed process", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "done")
|
||||
require.NoError(t, err)
|
||||
|
||||
proc := startProc(t, svc, context.Background(), "echo", "done")
|
||||
<-proc.Done()
|
||||
|
||||
err = proc.SendInput("test")
|
||||
err := proc.SendInput("test")
|
||||
assert.ErrorIs(t, err, ErrProcessNotRunning)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Signal(t *testing.T) {
|
||||
func TestProcess_Signal_Good(t *testing.T) {
|
||||
t.Run("sends signal to running process", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc, err := svc.Start(ctx, "sleep", "60")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = proc.Signal(os.Interrupt)
|
||||
proc := startProc(t, svc, ctx, "sleep", "60")
|
||||
err := proc.Signal(os.Interrupt)
|
||||
assert.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Process terminated by signal
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("process should have been terminated by signal")
|
||||
}
|
||||
assert.Equal(t, StatusKilled, proc.Status)
|
||||
})
|
||||
|
||||
t.Run("error on completed process", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "done")
|
||||
require.NoError(t, err)
|
||||
proc := startProc(t, svc, context.Background(), "echo", "done")
|
||||
<-proc.Done()
|
||||
|
||||
err = proc.Signal(os.Interrupt)
|
||||
err := proc.Signal(os.Interrupt)
|
||||
assert.ErrorIs(t, err, ErrProcessNotRunning)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_CloseStdin(t *testing.T) {
|
||||
func TestProcess_CloseStdin_Good(t *testing.T) {
|
||||
t.Run("closes stdin pipe", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "cat")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = proc.CloseStdin()
|
||||
proc := startProc(t, svc, context.Background(), "cat")
|
||||
err := proc.CloseStdin()
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Process should exit now that stdin is closed
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Good
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("cat should exit when stdin is closed")
|
||||
}
|
||||
|
|
@ -244,78 +190,66 @@ func TestProcess_CloseStdin(t *testing.T) {
|
|||
|
||||
t.Run("double close is safe", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "cat")
|
||||
require.NoError(t, err)
|
||||
|
||||
// First close
|
||||
err = proc.CloseStdin()
|
||||
proc := startProc(t, svc, context.Background(), "cat")
|
||||
err := proc.CloseStdin()
|
||||
assert.NoError(t, err)
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
// Second close should be safe (stdin already nil)
|
||||
err = proc.CloseStdin()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Timeout(t *testing.T) {
|
||||
func TestProcess_Timeout_Good(t *testing.T) {
|
||||
t.Run("kills process after timeout", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "sleep",
|
||||
Args: []string{"60"},
|
||||
Timeout: 200 * time.Millisecond,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Good — process was killed by timeout
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("process should have been killed by timeout")
|
||||
}
|
||||
|
||||
assert.False(t, proc.IsRunning())
|
||||
assert.Equal(t, StatusKilled, proc.Status)
|
||||
})
|
||||
|
||||
t.Run("no timeout when zero", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "echo",
|
||||
Args: []string{"fast"},
|
||||
Timeout: 0,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
<-proc.Done()
|
||||
assert.Equal(t, 0, proc.ExitCode)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Shutdown(t *testing.T) {
|
||||
func TestProcess_Shutdown_Good(t *testing.T) {
|
||||
t.Run("graceful with grace period", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
// Use a process that traps SIGTERM
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "sleep",
|
||||
Args: []string{"60"},
|
||||
GracePeriod: 100 * time.Millisecond,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
|
||||
assert.True(t, proc.IsRunning())
|
||||
|
||||
err = proc.Shutdown()
|
||||
err := proc.Shutdown()
|
||||
assert.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Good
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("shutdown should have completed")
|
||||
}
|
||||
|
|
@ -323,70 +257,175 @@ func TestProcess_Shutdown(t *testing.T) {
|
|||
|
||||
t.Run("immediate kill without grace period", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "sleep",
|
||||
Args: []string{"60"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
|
||||
err = proc.Shutdown()
|
||||
err := proc.Shutdown()
|
||||
assert.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Good
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("kill should be immediate")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_KillGroup(t *testing.T) {
|
||||
func TestProcess_KillGroup_Good(t *testing.T) {
|
||||
t.Run("kills child processes", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
// Spawn a parent that spawns a child — KillGroup should kill both
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "sh",
|
||||
Args: []string{"-c", "sleep 60 & wait"},
|
||||
Detach: true,
|
||||
KillGroup: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
|
||||
// Give child time to spawn
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
err = proc.Kill()
|
||||
err := proc.Kill()
|
||||
assert.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Good — whole group killed
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("process group should have been killed")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_TimeoutWithGrace(t *testing.T) {
|
||||
func TestProcess_TimeoutWithGrace_Good(t *testing.T) {
|
||||
t.Run("timeout triggers graceful shutdown", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "sleep",
|
||||
Args: []string{"60"},
|
||||
Timeout: 200 * time.Millisecond,
|
||||
GracePeriod: 100 * time.Millisecond,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Good — timeout + grace triggered
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("process should have been killed by timeout")
|
||||
}
|
||||
assert.Equal(t, StatusKilled, proc.Status)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Info_Bad(t *testing.T) {
|
||||
t.Run("failed process has StatusFailed", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
r := svc.Start(context.Background(), "nonexistent_command_xyz")
|
||||
assert.False(t, r.OK)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Info_Ugly(t *testing.T) {
|
||||
t.Run("info is safe to call concurrently", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
proc := startProc(t, svc, context.Background(), "sleep", "1")
|
||||
defer func() { _ = proc.Kill() }()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
for i := 0; i < 50; i++ {
|
||||
_ = proc.Info()
|
||||
}
|
||||
}()
|
||||
for i := 0; i < 50; i++ {
|
||||
_ = proc.Info()
|
||||
}
|
||||
<-done
|
||||
_ = proc.Kill()
|
||||
<-proc.Done()
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Wait_Bad(t *testing.T) {
|
||||
t.Run("returns error for non-zero exit code", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
proc := startProc(t, svc, context.Background(), "sh", "-c", "exit 2")
|
||||
err := proc.Wait()
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Wait_Ugly(t *testing.T) {
|
||||
t.Run("wait on killed process returns error", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
proc := startProc(t, svc, context.Background(), "sleep", "60")
|
||||
_ = proc.Kill()
|
||||
err := proc.Wait()
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Kill_Bad(t *testing.T) {
|
||||
t.Run("kill after kill is idempotent", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc := startProc(t, svc, ctx, "sleep", "60")
|
||||
_ = proc.Kill()
|
||||
<-proc.Done()
|
||||
|
||||
// Second kill should be a no-op (process not running)
|
||||
err := proc.Kill()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Kill_Ugly(t *testing.T) {
|
||||
t.Run("shutdown immediate when grace period is zero", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
proc := startProc(t, svc, context.Background(), "sleep", "60")
|
||||
|
||||
err := proc.Shutdown()
|
||||
assert.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("should have been killed immediately")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_SendInput_Ugly(t *testing.T) {
|
||||
t.Run("send to nil stdin returns error", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "echo",
|
||||
Args: []string{"hi"},
|
||||
})
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
<-proc.Done()
|
||||
|
||||
err := proc.SendInput("data")
|
||||
assert.ErrorIs(t, err, ErrProcessNotRunning)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcess_Signal_Ugly(t *testing.T) {
|
||||
t.Run("multiple signals to completed process return error", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
proc := startProc(t, svc, context.Background(), "echo", "done")
|
||||
<-proc.Done()
|
||||
|
||||
err := proc.Signal(os.Interrupt)
|
||||
assert.ErrorIs(t, err, ErrProcessNotRunning)
|
||||
err = proc.Signal(os.Interrupt)
|
||||
assert.ErrorIs(t, err, ErrProcessNotRunning)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
30
program.go
30
program.go
|
|
@ -3,19 +3,19 @@ package process
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"strconv"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
"dappco.re/go/core"
|
||||
)
|
||||
|
||||
// ErrProgramNotFound is returned when Find cannot locate the binary on PATH.
|
||||
// Callers may use errors.Is to detect this condition.
|
||||
var ErrProgramNotFound = coreerr.E("", "program: binary not found in PATH", nil)
|
||||
// Callers may use core.Is to detect this condition.
|
||||
var ErrProgramNotFound = core.E("", "program: binary not found in PATH", nil)
|
||||
|
||||
// Program represents a named executable located on the system PATH.
|
||||
// Create one with a Name, call Find to resolve its path, then Run or RunDir.
|
||||
//
|
||||
// p := &process.Program{Name: "go"}
|
||||
type Program struct {
|
||||
// Name is the binary name (e.g. "go", "node", "git").
|
||||
Name string
|
||||
|
|
@ -26,13 +26,15 @@ type Program struct {
|
|||
|
||||
// Find resolves the program's absolute path using exec.LookPath.
|
||||
// Returns ErrProgramNotFound (wrapped) if the binary is not on PATH.
|
||||
//
|
||||
// err := p.Find()
|
||||
func (p *Program) Find() error {
|
||||
if p.Name == "" {
|
||||
return coreerr.E("Program.Find", "program name is empty", nil)
|
||||
return core.E("program.find", "program name is empty", nil)
|
||||
}
|
||||
path, err := exec.LookPath(p.Name)
|
||||
path, err := execLookPath(p.Name)
|
||||
if err != nil {
|
||||
return coreerr.E("Program.Find", fmt.Sprintf("%q: not found in PATH", p.Name), ErrProgramNotFound)
|
||||
return core.E("program.find", core.Concat(strconv.Quote(p.Name), ": not found in PATH"), ErrProgramNotFound)
|
||||
}
|
||||
p.Path = path
|
||||
return nil
|
||||
|
|
@ -40,6 +42,8 @@ func (p *Program) Find() error {
|
|||
|
||||
// Run executes the program with args in the current working directory.
|
||||
// Returns trimmed combined stdout+stderr output and any error.
|
||||
//
|
||||
// out, err := p.Run(ctx, "version")
|
||||
func (p *Program) Run(ctx context.Context, args ...string) (string, error) {
|
||||
return p.RunDir(ctx, "", args...)
|
||||
}
|
||||
|
|
@ -47,6 +51,8 @@ func (p *Program) Run(ctx context.Context, args ...string) (string, error) {
|
|||
// RunDir executes the program with args in dir.
|
||||
// Returns trimmed combined stdout+stderr output and any error.
|
||||
// If dir is empty, the process inherits the caller's working directory.
|
||||
//
|
||||
// out, err := p.RunDir(ctx, "/workspace", "test", "./...")
|
||||
func (p *Program) RunDir(ctx context.Context, dir string, args ...string) (string, error) {
|
||||
binary := p.Path
|
||||
if binary == "" {
|
||||
|
|
@ -54,7 +60,7 @@ func (p *Program) RunDir(ctx context.Context, dir string, args ...string) (strin
|
|||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd := exec.CommandContext(ctx, binary, args...)
|
||||
cmd := execCommandContext(ctx, binary, args...)
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &out
|
||||
if dir != "" {
|
||||
|
|
@ -62,7 +68,7 @@ func (p *Program) RunDir(ctx context.Context, dir string, args ...string) (strin
|
|||
}
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return strings.TrimSpace(out.String()), coreerr.E("Program.RunDir", fmt.Sprintf("%q: command failed", p.Name), err)
|
||||
return string(bytes.TrimSpace(out.Bytes())), core.E("program.run", core.Concat(strconv.Quote(p.Name), ": command failed"), err)
|
||||
}
|
||||
return strings.TrimSpace(out.String()), nil
|
||||
return string(bytes.TrimSpace(out.Bytes())), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,11 @@ package process_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
|
|
@ -19,25 +20,25 @@ func testCtx(t *testing.T) context.Context {
|
|||
return ctx
|
||||
}
|
||||
|
||||
func TestProgram_Find_KnownBinary(t *testing.T) {
|
||||
func TestProgram_Find_Good(t *testing.T) {
|
||||
p := &process.Program{Name: "echo"}
|
||||
require.NoError(t, p.Find())
|
||||
assert.NotEmpty(t, p.Path)
|
||||
}
|
||||
|
||||
func TestProgram_Find_UnknownBinary(t *testing.T) {
|
||||
func TestProgram_FindUnknown_Bad(t *testing.T) {
|
||||
p := &process.Program{Name: "no-such-binary-xyzzy-42"}
|
||||
err := p.Find()
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, process.ErrProgramNotFound)
|
||||
}
|
||||
|
||||
func TestProgram_Find_EmptyName(t *testing.T) {
|
||||
func TestProgram_FindEmpty_Bad(t *testing.T) {
|
||||
p := &process.Program{}
|
||||
require.Error(t, p.Find())
|
||||
}
|
||||
|
||||
func TestProgram_Run_ReturnsOutput(t *testing.T) {
|
||||
func TestProgram_Run_Good(t *testing.T) {
|
||||
p := &process.Program{Name: "echo"}
|
||||
require.NoError(t, p.Find())
|
||||
|
||||
|
|
@ -46,7 +47,7 @@ func TestProgram_Run_ReturnsOutput(t *testing.T) {
|
|||
assert.Equal(t, "hello", out)
|
||||
}
|
||||
|
||||
func TestProgram_Run_WithoutFind_FallsBackToName(t *testing.T) {
|
||||
func TestProgram_RunFallback_Good(t *testing.T) {
|
||||
// Path is empty; RunDir should fall back to Name for OS PATH resolution.
|
||||
p := &process.Program{Name: "echo"}
|
||||
|
||||
|
|
@ -55,7 +56,7 @@ func TestProgram_Run_WithoutFind_FallsBackToName(t *testing.T) {
|
|||
assert.Equal(t, "fallback", out)
|
||||
}
|
||||
|
||||
func TestProgram_RunDir_UsesDirectory(t *testing.T) {
|
||||
func TestProgram_RunDir_Good(t *testing.T) {
|
||||
p := &process.Program{Name: "pwd"}
|
||||
require.NoError(t, p.Find())
|
||||
|
||||
|
|
@ -63,18 +64,36 @@ func TestProgram_RunDir_UsesDirectory(t *testing.T) {
|
|||
|
||||
out, err := p.RunDir(testCtx(t), dir)
|
||||
require.NoError(t, err)
|
||||
// Resolve symlinks on both sides for portability (macOS uses /private/ prefix).
|
||||
canonicalDir, err := filepath.EvalSymlinks(dir)
|
||||
dirInfo, err := os.Stat(dir)
|
||||
require.NoError(t, err)
|
||||
canonicalOut, err := filepath.EvalSymlinks(out)
|
||||
outInfo, err := os.Stat(core.Trim(out))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, canonicalDir, canonicalOut)
|
||||
assert.True(t, os.SameFile(dirInfo, outInfo))
|
||||
}
|
||||
|
||||
func TestProgram_Run_FailingCommand(t *testing.T) {
|
||||
func TestProgram_RunFailure_Bad(t *testing.T) {
|
||||
p := &process.Program{Name: "false"}
|
||||
require.NoError(t, p.Find())
|
||||
|
||||
_, err := p.Run(testCtx(t))
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestProgram_Find_Ugly(t *testing.T) {
|
||||
t.Run("find then run in non-existent dir returns error", func(t *testing.T) {
|
||||
p := &process.Program{Name: "echo"}
|
||||
require.NoError(t, p.Find())
|
||||
|
||||
_, err := p.RunDir(testCtx(t), "/nonexistent-dir-xyz-abc")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("path set directly skips PATH lookup", func(t *testing.T) {
|
||||
p := &process.Program{Name: "echo", Path: "/bin/echo"}
|
||||
out, err := p.Run(testCtx(t), "direct")
|
||||
// Only assert no panic; binary may be at different location on some systems
|
||||
if err == nil {
|
||||
assert.Equal(t, "direct", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
326
registry.go
326
registry.go
|
|
@ -1,18 +1,18 @@
|
|||
package process
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"path"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"dappco.re/go/core"
|
||||
coreio "dappco.re/go/core/io"
|
||||
coreerr "dappco.re/go/core/log"
|
||||
)
|
||||
|
||||
// DaemonEntry records a running daemon in the registry.
|
||||
//
|
||||
// entry := process.DaemonEntry{Code: "myapp", Daemon: "serve", PID: 1234}
|
||||
type DaemonEntry struct {
|
||||
Code string `json:"code"`
|
||||
Daemon string `json:"daemon"`
|
||||
|
|
@ -24,57 +24,67 @@ type DaemonEntry struct {
|
|||
}
|
||||
|
||||
// Registry tracks running daemons via JSON files in a directory.
|
||||
//
|
||||
// reg := process.NewRegistry("/tmp/process-daemons")
|
||||
type Registry struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
// NewRegistry creates a registry backed by the given directory.
|
||||
//
|
||||
// reg := process.NewRegistry("/tmp/process-daemons")
|
||||
func NewRegistry(dir string) *Registry {
|
||||
return &Registry{dir: dir}
|
||||
}
|
||||
|
||||
// DefaultRegistry returns a registry using ~/.core/daemons/.
|
||||
//
|
||||
// reg := process.DefaultRegistry()
|
||||
func DefaultRegistry() *Registry {
|
||||
home, err := os.UserHomeDir()
|
||||
home, err := userHomeDir()
|
||||
if err != nil {
|
||||
home = os.TempDir()
|
||||
home = tempDir()
|
||||
}
|
||||
return NewRegistry(filepath.Join(home, ".core", "daemons"))
|
||||
return NewRegistry(path.Join(home, ".core", "daemons"))
|
||||
}
|
||||
|
||||
// Register writes a daemon entry to the registry directory.
|
||||
// If Started is zero, it is set to the current time.
|
||||
// The directory is created if it does not exist.
|
||||
//
|
||||
// err := registry.Register(process.DaemonEntry{Code: "agent", Daemon: "core-agent", PID: os.Getpid()})
|
||||
func (r *Registry) Register(entry DaemonEntry) error {
|
||||
if entry.Started.IsZero() {
|
||||
entry.Started = time.Now()
|
||||
}
|
||||
|
||||
if err := coreio.Local.EnsureDir(r.dir); err != nil {
|
||||
return coreerr.E("Registry.Register", "failed to create registry directory", err)
|
||||
return core.E("registry.register", "failed to create registry directory", err)
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(entry, "", " ")
|
||||
data, err := marshalDaemonEntry(entry)
|
||||
if err != nil {
|
||||
return coreerr.E("Registry.Register", "failed to marshal entry", err)
|
||||
return core.E("registry.register", "failed to marshal entry", err)
|
||||
}
|
||||
|
||||
if err := coreio.Local.Write(r.entryPath(entry.Code, entry.Daemon), string(data)); err != nil {
|
||||
return coreerr.E("Registry.Register", "failed to write entry file", err)
|
||||
if err := coreio.Local.Write(r.entryPath(entry.Code, entry.Daemon), data); err != nil {
|
||||
return core.E("registry.register", "failed to write entry file", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unregister removes a daemon entry from the registry.
|
||||
//
|
||||
// err := registry.Unregister("agent", "core-agent")
|
||||
func (r *Registry) Unregister(code, daemon string) error {
|
||||
if err := coreio.Local.Delete(r.entryPath(code, daemon)); err != nil {
|
||||
return coreerr.E("Registry.Unregister", "failed to delete entry file", err)
|
||||
return core.E("registry.unregister", "failed to delete entry file", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get reads a single daemon entry and checks whether its process is alive.
|
||||
// If the process is dead, the stale file is removed and (nil, false) is returned.
|
||||
// entry, alive := registry.Get("agent", "core-agent")
|
||||
// if !alive { fmt.Println("daemon not running") }
|
||||
func (r *Registry) Get(code, daemon string) (*DaemonEntry, bool) {
|
||||
path := r.entryPath(code, daemon)
|
||||
|
||||
|
|
@ -83,8 +93,8 @@ func (r *Registry) Get(code, daemon string) (*DaemonEntry, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
var entry DaemonEntry
|
||||
if err := json.Unmarshal([]byte(data), &entry); err != nil {
|
||||
entry, err := unmarshalDaemonEntry(data)
|
||||
if err != nil {
|
||||
_ = coreio.Local.Delete(path)
|
||||
return nil, false
|
||||
}
|
||||
|
|
@ -98,21 +108,31 @@ func (r *Registry) Get(code, daemon string) (*DaemonEntry, bool) {
|
|||
}
|
||||
|
||||
// List returns all alive daemon entries, pruning any with dead PIDs.
|
||||
//
|
||||
// entries, err := registry.List()
|
||||
func (r *Registry) List() ([]DaemonEntry, error) {
|
||||
matches, err := filepath.Glob(filepath.Join(r.dir, "*.json"))
|
||||
if !coreio.Local.Exists(r.dir) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
entries, err := coreio.Local.List(r.dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, core.E("registry.list", "failed to list registry directory", err)
|
||||
}
|
||||
|
||||
var alive []DaemonEntry
|
||||
for _, path := range matches {
|
||||
for _, entryFile := range entries {
|
||||
if entryFile.IsDir() || !core.HasSuffix(entryFile.Name(), ".json") {
|
||||
continue
|
||||
}
|
||||
path := path.Join(r.dir, entryFile.Name())
|
||||
data, err := coreio.Local.Read(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var entry DaemonEntry
|
||||
if err := json.Unmarshal([]byte(data), &entry); err != nil {
|
||||
entry, err := unmarshalDaemonEntry(data)
|
||||
if err != nil {
|
||||
_ = coreio.Local.Delete(path)
|
||||
continue
|
||||
}
|
||||
|
|
@ -130,8 +150,8 @@ func (r *Registry) List() ([]DaemonEntry, error) {
|
|||
|
||||
// entryPath returns the filesystem path for a daemon entry.
|
||||
func (r *Registry) entryPath(code, daemon string) string {
|
||||
name := strings.ReplaceAll(code, "/", "-") + "-" + strings.ReplaceAll(daemon, "/", "-") + ".json"
|
||||
return filepath.Join(r.dir, name)
|
||||
name := sanitizeRegistryComponent(code) + "-" + sanitizeRegistryComponent(daemon) + ".json"
|
||||
return path.Join(r.dir, name)
|
||||
}
|
||||
|
||||
// isAlive checks whether a process with the given PID is running.
|
||||
|
|
@ -139,9 +159,263 @@ func isAlive(pid int) bool {
|
|||
if pid <= 0 {
|
||||
return false
|
||||
}
|
||||
proc, err := os.FindProcess(pid)
|
||||
proc, err := processHandle(pid)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return proc.Signal(syscall.Signal(0)) == nil
|
||||
}
|
||||
|
||||
func sanitizeRegistryComponent(value string) string {
|
||||
buf := make([]byte, len(value))
|
||||
for i := 0; i < len(value); i++ {
|
||||
if value[i] == '/' {
|
||||
buf[i] = '-'
|
||||
continue
|
||||
}
|
||||
buf[i] = value[i]
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func marshalDaemonEntry(entry DaemonEntry) (string, error) {
|
||||
fields := []struct {
|
||||
key string
|
||||
value string
|
||||
}{
|
||||
{key: "code", value: quoteJSONString(entry.Code)},
|
||||
{key: "daemon", value: quoteJSONString(entry.Daemon)},
|
||||
{key: "pid", value: strconv.Itoa(entry.PID)},
|
||||
}
|
||||
|
||||
if entry.Health != "" {
|
||||
fields = append(fields, struct {
|
||||
key string
|
||||
value string
|
||||
}{key: "health", value: quoteJSONString(entry.Health)})
|
||||
}
|
||||
if entry.Project != "" {
|
||||
fields = append(fields, struct {
|
||||
key string
|
||||
value string
|
||||
}{key: "project", value: quoteJSONString(entry.Project)})
|
||||
}
|
||||
if entry.Binary != "" {
|
||||
fields = append(fields, struct {
|
||||
key string
|
||||
value string
|
||||
}{key: "binary", value: quoteJSONString(entry.Binary)})
|
||||
}
|
||||
|
||||
fields = append(fields, struct {
|
||||
key string
|
||||
value string
|
||||
}{
|
||||
key: "started",
|
||||
value: quoteJSONString(entry.Started.Format(time.RFC3339Nano)),
|
||||
})
|
||||
|
||||
builder := core.NewBuilder()
|
||||
builder.WriteString("{\n")
|
||||
for i, field := range fields {
|
||||
builder.WriteString(core.Concat(" ", quoteJSONString(field.key), ": ", field.value))
|
||||
if i < len(fields)-1 {
|
||||
builder.WriteString(",")
|
||||
}
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
builder.WriteString("}")
|
||||
return builder.String(), nil
|
||||
}
|
||||
|
||||
func unmarshalDaemonEntry(data string) (DaemonEntry, error) {
|
||||
values, err := parseJSONObject(data)
|
||||
if err != nil {
|
||||
return DaemonEntry{}, err
|
||||
}
|
||||
|
||||
entry := DaemonEntry{
|
||||
Code: values["code"],
|
||||
Daemon: values["daemon"],
|
||||
Health: values["health"],
|
||||
Project: values["project"],
|
||||
Binary: values["binary"],
|
||||
}
|
||||
|
||||
pidValue, ok := values["pid"]
|
||||
if !ok {
|
||||
return DaemonEntry{}, core.E("Registry.unmarshalDaemonEntry", "missing pid", nil)
|
||||
}
|
||||
entry.PID, err = strconv.Atoi(pidValue)
|
||||
if err != nil {
|
||||
return DaemonEntry{}, core.E("Registry.unmarshalDaemonEntry", "invalid pid", err)
|
||||
}
|
||||
|
||||
startedValue, ok := values["started"]
|
||||
if !ok {
|
||||
return DaemonEntry{}, core.E("Registry.unmarshalDaemonEntry", "missing started", nil)
|
||||
}
|
||||
entry.Started, err = time.Parse(time.RFC3339Nano, startedValue)
|
||||
if err != nil {
|
||||
return DaemonEntry{}, core.E("Registry.unmarshalDaemonEntry", "invalid started timestamp", err)
|
||||
}
|
||||
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func parseJSONObject(data string) (map[string]string, error) {
|
||||
trimmed := core.Trim(data)
|
||||
if trimmed == "" {
|
||||
return nil, core.E("Registry.parseJSONObject", "empty JSON object", nil)
|
||||
}
|
||||
if trimmed[0] != '{' || trimmed[len(trimmed)-1] != '}' {
|
||||
return nil, core.E("Registry.parseJSONObject", "invalid JSON object", nil)
|
||||
}
|
||||
|
||||
values := make(map[string]string)
|
||||
index := skipJSONSpace(trimmed, 1)
|
||||
for index < len(trimmed) {
|
||||
if trimmed[index] == '}' {
|
||||
return values, nil
|
||||
}
|
||||
|
||||
key, next, err := parseJSONString(trimmed, index)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
index = skipJSONSpace(trimmed, next)
|
||||
if index >= len(trimmed) || trimmed[index] != ':' {
|
||||
return nil, core.E("Registry.parseJSONObject", "missing key separator", nil)
|
||||
}
|
||||
|
||||
index = skipJSONSpace(trimmed, index+1)
|
||||
if index >= len(trimmed) {
|
||||
return nil, core.E("Registry.parseJSONObject", "missing value", nil)
|
||||
}
|
||||
|
||||
var value string
|
||||
if trimmed[index] == '"' {
|
||||
value, index, err = parseJSONString(trimmed, index)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
start := index
|
||||
for index < len(trimmed) && trimmed[index] != ',' && trimmed[index] != '}' {
|
||||
index++
|
||||
}
|
||||
value = core.Trim(trimmed[start:index])
|
||||
}
|
||||
values[key] = value
|
||||
|
||||
index = skipJSONSpace(trimmed, index)
|
||||
if index >= len(trimmed) {
|
||||
break
|
||||
}
|
||||
if trimmed[index] == ',' {
|
||||
index = skipJSONSpace(trimmed, index+1)
|
||||
continue
|
||||
}
|
||||
if trimmed[index] == '}' {
|
||||
return values, nil
|
||||
}
|
||||
return nil, core.E("Registry.parseJSONObject", "invalid object separator", nil)
|
||||
}
|
||||
|
||||
return nil, core.E("Registry.parseJSONObject", "unterminated JSON object", nil)
|
||||
}
|
||||
|
||||
func parseJSONString(data string, start int) (string, int, error) {
|
||||
if start >= len(data) || data[start] != '"' {
|
||||
return "", 0, core.E("Registry.parseJSONString", "expected quoted string", nil)
|
||||
}
|
||||
|
||||
builder := core.NewBuilder()
|
||||
for index := start + 1; index < len(data); index++ {
|
||||
ch := data[index]
|
||||
if ch == '"' {
|
||||
return builder.String(), index + 1, nil
|
||||
}
|
||||
if ch != '\\' {
|
||||
builder.WriteByte(ch)
|
||||
continue
|
||||
}
|
||||
|
||||
index++
|
||||
if index >= len(data) {
|
||||
return "", 0, core.E("Registry.parseJSONString", "unterminated escape sequence", nil)
|
||||
}
|
||||
|
||||
switch data[index] {
|
||||
case '"', '\\', '/':
|
||||
builder.WriteByte(data[index])
|
||||
case 'b':
|
||||
builder.WriteByte('\b')
|
||||
case 'f':
|
||||
builder.WriteByte('\f')
|
||||
case 'n':
|
||||
builder.WriteByte('\n')
|
||||
case 'r':
|
||||
builder.WriteByte('\r')
|
||||
case 't':
|
||||
builder.WriteByte('\t')
|
||||
case 'u':
|
||||
if index+4 >= len(data) {
|
||||
return "", 0, core.E("Registry.parseJSONString", "short unicode escape", nil)
|
||||
}
|
||||
r, err := strconv.ParseInt(data[index+1:index+5], 16, 32)
|
||||
if err != nil {
|
||||
return "", 0, core.E("Registry.parseJSONString", "invalid unicode escape", err)
|
||||
}
|
||||
builder.WriteRune(rune(r))
|
||||
index += 4
|
||||
default:
|
||||
return "", 0, core.E("Registry.parseJSONString", "invalid escape sequence", nil)
|
||||
}
|
||||
}
|
||||
|
||||
return "", 0, core.E("Registry.parseJSONString", "unterminated string", nil)
|
||||
}
|
||||
|
||||
func skipJSONSpace(data string, index int) int {
|
||||
for index < len(data) {
|
||||
switch data[index] {
|
||||
case ' ', '\n', '\r', '\t':
|
||||
index++
|
||||
default:
|
||||
return index
|
||||
}
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
func quoteJSONString(value string) string {
|
||||
builder := core.NewBuilder()
|
||||
builder.WriteByte('"')
|
||||
for i := 0; i < len(value); i++ {
|
||||
switch value[i] {
|
||||
case '\\', '"':
|
||||
builder.WriteByte('\\')
|
||||
builder.WriteByte(value[i])
|
||||
case '\b':
|
||||
builder.WriteString(`\b`)
|
||||
case '\f':
|
||||
builder.WriteString(`\f`)
|
||||
case '\n':
|
||||
builder.WriteString(`\n`)
|
||||
case '\r':
|
||||
builder.WriteString(`\r`)
|
||||
case '\t':
|
||||
builder.WriteString(`\t`)
|
||||
default:
|
||||
if value[i] < 0x20 {
|
||||
builder.WriteString(core.Sprintf("\\u%04x", value[i]))
|
||||
continue
|
||||
}
|
||||
builder.WriteByte(value[i])
|
||||
}
|
||||
}
|
||||
builder.WriteByte('"')
|
||||
return builder.String()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,15 +2,15 @@ package process
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRegistry_RegisterAndGet(t *testing.T) {
|
||||
func TestRegistry_Register_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ func TestRegistry_RegisterAndGet(t *testing.T) {
|
|||
assert.Equal(t, started, got.Started)
|
||||
}
|
||||
|
||||
func TestRegistry_Unregister(t *testing.T) {
|
||||
func TestRegistry_Unregister_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
|
|
@ -53,7 +53,7 @@ func TestRegistry_Unregister(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
// File should exist
|
||||
path := filepath.Join(dir, "myapp-server.json")
|
||||
path := core.JoinPath(dir, "myapp-server.json")
|
||||
_, err = os.Stat(path)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ func TestRegistry_Unregister(t *testing.T) {
|
|||
assert.True(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestRegistry_List(t *testing.T) {
|
||||
func TestRegistry_List_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ func TestRegistry_List(t *testing.T) {
|
|||
assert.Len(t, entries, 2)
|
||||
}
|
||||
|
||||
func TestRegistry_List_PrunesStale(t *testing.T) {
|
||||
func TestRegistry_PruneStale_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
|
|
@ -87,7 +87,7 @@ func TestRegistry_List_PrunesStale(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
// File should exist before listing
|
||||
path := filepath.Join(dir, "dead-proc.json")
|
||||
path := core.JoinPath(dir, "dead-proc.json")
|
||||
_, err = os.Stat(path)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
|
@ -100,7 +100,7 @@ func TestRegistry_List_PrunesStale(t *testing.T) {
|
|||
assert.True(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestRegistry_Get_NotFound(t *testing.T) {
|
||||
func TestRegistry_GetMissing_Bad(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
|
|
@ -109,8 +109,8 @@ func TestRegistry_Get_NotFound(t *testing.T) {
|
|||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestRegistry_CreatesDirectory(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "nested", "deep", "daemons")
|
||||
func TestRegistry_CreateDirectory_Good(t *testing.T) {
|
||||
dir := core.JoinPath(t.TempDir(), "nested", "deep", "daemons")
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
err := reg.Register(DaemonEntry{Code: "app", Daemon: "srv", PID: os.Getpid()})
|
||||
|
|
@ -121,7 +121,44 @@ func TestRegistry_CreatesDirectory(t *testing.T) {
|
|||
assert.True(t, info.IsDir())
|
||||
}
|
||||
|
||||
func TestDefaultRegistry(t *testing.T) {
|
||||
func TestRegistry_Default_Good(t *testing.T) {
|
||||
reg := DefaultRegistry()
|
||||
assert.NotNil(t, reg)
|
||||
}
|
||||
|
||||
func TestRegistry_Register_Bad(t *testing.T) {
|
||||
t.Run("unregister non-existent entry returns error", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
err := reg.Unregister("ghost", "proc")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRegistry_Register_Ugly(t *testing.T) {
|
||||
t.Run("code with slashes is sanitised in filename", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
err := reg.Register(DaemonEntry{
|
||||
Code: "org/app",
|
||||
Daemon: "serve",
|
||||
PID: os.Getpid(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
entry, ok := reg.Get("org/app", "serve")
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "org/app", entry.Code)
|
||||
})
|
||||
|
||||
t.Run("list on empty directory returns nil", func(t *testing.T) {
|
||||
dir := core.JoinPath(t.TempDir(), "nonexistent-registry")
|
||||
reg := NewRegistry(dir)
|
||||
|
||||
entries, err := reg.List()
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, entries)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
58
runner.go
58
runner.go
|
|
@ -5,7 +5,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
"dappco.re/go/core"
|
||||
)
|
||||
|
||||
// Runner orchestrates multiple processes with dependencies.
|
||||
|
|
@ -13,7 +13,8 @@ type Runner struct {
|
|||
service *Service
|
||||
}
|
||||
|
||||
// NewRunner creates a runner for the given service.
|
||||
// runner := process.NewRunner(svc)
|
||||
// result, _ := runner.RunAll(ctx, specs)
|
||||
func NewRunner(svc *Service) *Runner {
|
||||
return &Runner{service: svc}
|
||||
}
|
||||
|
|
@ -47,7 +48,7 @@ type RunResult struct {
|
|||
Skipped bool
|
||||
}
|
||||
|
||||
// Passed returns true if the process succeeded.
|
||||
// if result.Passed() { fmt.Println("ok:", result.Name) }
|
||||
func (r RunResult) Passed() bool {
|
||||
return !r.Skipped && r.Error == nil && r.ExitCode == 0
|
||||
}
|
||||
|
|
@ -61,12 +62,12 @@ type RunAllResult struct {
|
|||
Skipped int
|
||||
}
|
||||
|
||||
// Success returns true if all non-skipped specs passed.
|
||||
// if !result.Success() { fmt.Println("failed:", result.Failed) }
|
||||
func (r RunAllResult) Success() bool {
|
||||
return r.Failed == 0
|
||||
}
|
||||
|
||||
// RunAll executes specs respecting dependencies, parallelising where possible.
|
||||
// result, err := runner.RunAll(ctx, []process.RunSpec{{Name: "build"}, {Name: "test", After: []string{"build"}}})
|
||||
func (r *Runner) RunAll(ctx context.Context, specs []RunSpec) (*RunAllResult, error) {
|
||||
start := time.Now()
|
||||
|
||||
|
|
@ -105,7 +106,7 @@ func (r *Runner) RunAll(ctx context.Context, specs []RunSpec) (*RunAllResult, er
|
|||
Name: name,
|
||||
Spec: remaining[name],
|
||||
ExitCode: 1,
|
||||
Error: coreerr.E("Runner.RunAll", "circular dependency or missing dependency", nil),
|
||||
Error: core.E("runner.run_all", "circular dependency or missing dependency", nil),
|
||||
})
|
||||
}
|
||||
break
|
||||
|
|
@ -137,7 +138,7 @@ func (r *Runner) RunAll(ctx context.Context, specs []RunSpec) (*RunAllResult, er
|
|||
Name: spec.Name,
|
||||
Spec: spec,
|
||||
Skipped: true,
|
||||
Error: coreerr.E("Runner.RunAll", "skipped due to dependency failure", nil),
|
||||
Error: core.E("runner.run_all", "skipped due to dependency failure", nil),
|
||||
}
|
||||
} else {
|
||||
result = r.runSpec(ctx, spec)
|
||||
|
|
@ -161,22 +162,22 @@ func (r *Runner) RunAll(ctx context.Context, specs []RunSpec) (*RunAllResult, er
|
|||
}
|
||||
|
||||
// Build aggregate result
|
||||
aggResult := &RunAllResult{
|
||||
aggregate := &RunAllResult{
|
||||
Results: results,
|
||||
Duration: time.Since(start),
|
||||
}
|
||||
|
||||
for _, res := range results {
|
||||
if res.Skipped {
|
||||
aggResult.Skipped++
|
||||
aggregate.Skipped++
|
||||
} else if res.Passed() {
|
||||
aggResult.Passed++
|
||||
aggregate.Passed++
|
||||
} else {
|
||||
aggResult.Failed++
|
||||
aggregate.Failed++
|
||||
}
|
||||
}
|
||||
|
||||
return aggResult, nil
|
||||
return aggregate, nil
|
||||
}
|
||||
|
||||
// canRun checks if all dependencies are completed.
|
||||
|
|
@ -193,13 +194,17 @@ func (r *Runner) canRun(spec RunSpec, completed map[string]*RunResult) bool {
|
|||
func (r *Runner) runSpec(ctx context.Context, spec RunSpec) RunResult {
|
||||
start := time.Now()
|
||||
|
||||
proc, err := r.service.StartWithOptions(ctx, RunOptions{
|
||||
sr := r.service.StartWithOptions(ctx, RunOptions{
|
||||
Command: spec.Command,
|
||||
Args: spec.Args,
|
||||
Dir: spec.Dir,
|
||||
Env: spec.Env,
|
||||
})
|
||||
if err != nil {
|
||||
if !sr.OK {
|
||||
err, _ := sr.Value.(error)
|
||||
if err == nil {
|
||||
err = core.E("runner.run_spec", core.Concat("failed to start: ", spec.Name), nil)
|
||||
}
|
||||
return RunResult{
|
||||
Name: spec.Name,
|
||||
Spec: spec,
|
||||
|
|
@ -208,6 +213,7 @@ func (r *Runner) runSpec(ctx context.Context, spec RunSpec) RunResult {
|
|||
}
|
||||
}
|
||||
|
||||
proc := sr.Value.(*Process)
|
||||
<-proc.Done()
|
||||
|
||||
return RunResult{
|
||||
|
|
@ -220,7 +226,7 @@ func (r *Runner) runSpec(ctx context.Context, spec RunSpec) RunResult {
|
|||
}
|
||||
}
|
||||
|
||||
// RunSequential executes specs one after another, stopping on first failure.
|
||||
// result, _ := runner.RunSequential(ctx, []process.RunSpec{{Name: "lint"}, {Name: "test"}})
|
||||
func (r *Runner) RunSequential(ctx context.Context, specs []RunSpec) (*RunAllResult, error) {
|
||||
start := time.Now()
|
||||
results := make([]RunResult, 0, len(specs))
|
||||
|
|
@ -242,25 +248,25 @@ func (r *Runner) RunSequential(ctx context.Context, specs []RunSpec) (*RunAllRes
|
|||
}
|
||||
}
|
||||
|
||||
aggResult := &RunAllResult{
|
||||
aggregate := &RunAllResult{
|
||||
Results: results,
|
||||
Duration: time.Since(start),
|
||||
}
|
||||
|
||||
for _, res := range results {
|
||||
if res.Skipped {
|
||||
aggResult.Skipped++
|
||||
aggregate.Skipped++
|
||||
} else if res.Passed() {
|
||||
aggResult.Passed++
|
||||
aggregate.Passed++
|
||||
} else {
|
||||
aggResult.Failed++
|
||||
aggregate.Failed++
|
||||
}
|
||||
}
|
||||
|
||||
return aggResult, nil
|
||||
return aggregate, nil
|
||||
}
|
||||
|
||||
// RunParallel executes all specs concurrently, regardless of dependencies.
|
||||
// result, _ := runner.RunParallel(ctx, []process.RunSpec{{Name: "a"}, {Name: "b"}, {Name: "c"}})
|
||||
func (r *Runner) RunParallel(ctx context.Context, specs []RunSpec) (*RunAllResult, error) {
|
||||
start := time.Now()
|
||||
results := make([]RunResult, len(specs))
|
||||
|
|
@ -275,20 +281,20 @@ func (r *Runner) RunParallel(ctx context.Context, specs []RunSpec) (*RunAllResul
|
|||
}
|
||||
wg.Wait()
|
||||
|
||||
aggResult := &RunAllResult{
|
||||
aggregate := &RunAllResult{
|
||||
Results: results,
|
||||
Duration: time.Since(start),
|
||||
}
|
||||
|
||||
for _, res := range results {
|
||||
if res.Skipped {
|
||||
aggResult.Skipped++
|
||||
aggregate.Skipped++
|
||||
} else if res.Passed() {
|
||||
aggResult.Passed++
|
||||
aggregate.Passed++
|
||||
} else {
|
||||
aggResult.Failed++
|
||||
aggregate.Failed++
|
||||
}
|
||||
}
|
||||
|
||||
return aggResult, nil
|
||||
return aggregate, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,14 +13,12 @@ func newTestRunner(t *testing.T) *Runner {
|
|||
t.Helper()
|
||||
|
||||
c := framework.New()
|
||||
factory := NewService(Options{})
|
||||
raw, err := factory(c)
|
||||
require.NoError(t, err)
|
||||
|
||||
return NewRunner(raw.(*Service))
|
||||
r := Register(c)
|
||||
require.True(t, r.OK)
|
||||
return NewRunner(r.Value.(*Service))
|
||||
}
|
||||
|
||||
func TestRunner_RunSequential(t *testing.T) {
|
||||
func TestRunner_RunSequential_Good(t *testing.T) {
|
||||
t.Run("all pass", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
|
|
@ -70,7 +68,7 @@ func TestRunner_RunSequential(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunParallel(t *testing.T) {
|
||||
func TestRunner_RunParallel_Good(t *testing.T) {
|
||||
t.Run("all run concurrently", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
|
|
@ -102,7 +100,7 @@ func TestRunner_RunParallel(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunAll(t *testing.T) {
|
||||
func TestRunner_RunAll_Good(t *testing.T) {
|
||||
t.Run("respects dependencies", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
|
|
@ -150,7 +148,7 @@ func TestRunner_RunAll(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunAll_CircularDeps(t *testing.T) {
|
||||
func TestRunner_CircularDeps_Bad(t *testing.T) {
|
||||
t.Run("circular dependency counts as failed", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
|
|
@ -166,7 +164,7 @@ func TestRunner_RunAll_CircularDeps(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestRunResult_Passed(t *testing.T) {
|
||||
func TestRunResult_Passed_Good(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
r := RunResult{ExitCode: 0}
|
||||
assert.True(t, r.Passed())
|
||||
|
|
@ -187,3 +185,84 @@ func TestRunResult_Passed(t *testing.T) {
|
|||
assert.False(t, r.Passed())
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunSequential_Bad(t *testing.T) {
|
||||
t.Run("invalid command fails", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
result, err := runner.RunSequential(context.Background(), []RunSpec{
|
||||
{Name: "bad", Command: "nonexistent_command_xyz"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.False(t, result.Success())
|
||||
assert.Equal(t, 1, result.Failed)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunSequential_Ugly(t *testing.T) {
|
||||
t.Run("empty spec list succeeds with no results", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
result, err := runner.RunSequential(context.Background(), []RunSpec{})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, result.Success())
|
||||
assert.Equal(t, 0, result.Passed)
|
||||
assert.Len(t, result.Results, 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunParallel_Bad(t *testing.T) {
|
||||
t.Run("invalid command fails without stopping others", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
result, err := runner.RunParallel(context.Background(), []RunSpec{
|
||||
{Name: "ok", Command: "echo", Args: []string{"1"}},
|
||||
{Name: "bad", Command: "nonexistent_command_xyz"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.False(t, result.Success())
|
||||
assert.Equal(t, 1, result.Passed)
|
||||
assert.Equal(t, 1, result.Failed)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunParallel_Ugly(t *testing.T) {
|
||||
t.Run("empty spec list succeeds", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
result, err := runner.RunParallel(context.Background(), []RunSpec{})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, result.Success())
|
||||
assert.Len(t, result.Results, 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunAll_Bad(t *testing.T) {
|
||||
t.Run("missing dependency name counts as deadlock", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
result, err := runner.RunAll(context.Background(), []RunSpec{
|
||||
{Name: "first", Command: "echo", Args: []string{"1"}, After: []string{"missing"}},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.False(t, result.Success())
|
||||
assert.Equal(t, 1, result.Failed)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunner_RunAll_Ugly(t *testing.T) {
|
||||
t.Run("empty spec list succeeds", func(t *testing.T) {
|
||||
runner := newTestRunner(t)
|
||||
|
||||
result, err := runner.RunAll(context.Background(), []RunSpec{})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, result.Success())
|
||||
assert.Len(t, result.Results, 0)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
569
service.go
569
service.go
|
|
@ -3,37 +3,37 @@ package process
|
|||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"dappco.re/go/core"
|
||||
coreerr "dappco.re/go/core/log"
|
||||
)
|
||||
|
||||
type execCmd = exec.Cmd
|
||||
|
||||
type streamReader interface {
|
||||
Read(p []byte) (n int, err error)
|
||||
}
|
||||
|
||||
// Default buffer size for process output (1MB).
|
||||
const DefaultBufferSize = 1024 * 1024
|
||||
|
||||
// Errors
|
||||
var (
|
||||
ErrProcessNotFound = coreerr.E("", "process not found", nil)
|
||||
ErrProcessNotRunning = coreerr.E("", "process is not running", nil)
|
||||
ErrStdinNotAvailable = coreerr.E("", "stdin not available", nil)
|
||||
ErrProcessNotFound = core.E("", "process not found", nil)
|
||||
ErrProcessNotRunning = core.E("", "process is not running", nil)
|
||||
ErrStdinNotAvailable = core.E("", "stdin not available", nil)
|
||||
)
|
||||
|
||||
// Service manages process execution with Core IPC integration.
|
||||
type Service struct {
|
||||
*core.ServiceRuntime[Options]
|
||||
|
||||
processes map[string]*Process
|
||||
mu sync.RWMutex
|
||||
bufSize int
|
||||
idCounter atomic.Uint64
|
||||
managed *core.Registry[*ManagedProcess]
|
||||
bufSize int
|
||||
}
|
||||
|
||||
// Options configures the process service.
|
||||
|
|
@ -43,51 +43,46 @@ type Options struct {
|
|||
BufferSize int
|
||||
}
|
||||
|
||||
// NewService creates a process service factory for Core registration.
|
||||
// Register constructs a Service bound to the provided Core instance.
|
||||
//
|
||||
// core, _ := core.New(
|
||||
// core.WithName("process", process.NewService(process.Options{})),
|
||||
// )
|
||||
func NewService(opts Options) func(*core.Core) (any, error) {
|
||||
return func(c *core.Core) (any, error) {
|
||||
if opts.BufferSize == 0 {
|
||||
opts.BufferSize = DefaultBufferSize
|
||||
}
|
||||
svc := &Service{
|
||||
ServiceRuntime: core.NewServiceRuntime(c, opts),
|
||||
processes: make(map[string]*Process),
|
||||
bufSize: opts.BufferSize,
|
||||
}
|
||||
return svc, nil
|
||||
// c := core.New()
|
||||
// svc := process.Register(c).Value.(*process.Service)
|
||||
func Register(c *core.Core) core.Result {
|
||||
opts := Options{BufferSize: DefaultBufferSize}
|
||||
svc := &Service{
|
||||
ServiceRuntime: core.NewServiceRuntime(c, opts),
|
||||
managed: core.NewRegistry[*ManagedProcess](),
|
||||
bufSize: opts.BufferSize,
|
||||
}
|
||||
return core.Result{Value: svc, OK: true}
|
||||
}
|
||||
|
||||
// OnStartup implements core.Startable.
|
||||
func (s *Service) OnStartup(ctx context.Context) error {
|
||||
return nil
|
||||
func (s *Service) OnStartup(ctx context.Context) core.Result {
|
||||
c := s.Core()
|
||||
c.Action("process.run", s.handleRun)
|
||||
c.Action("process.start", s.handleStart)
|
||||
c.Action("process.kill", s.handleKill)
|
||||
c.Action("process.list", s.handleList)
|
||||
c.Action("process.get", s.handleGet)
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
|
||||
// OnShutdown implements core.Stoppable.
|
||||
// Gracefully shuts down all running processes (SIGTERM → SIGKILL).
|
||||
func (s *Service) OnShutdown(ctx context.Context) error {
|
||||
s.mu.RLock()
|
||||
procs := make([]*Process, 0, len(s.processes))
|
||||
for _, p := range s.processes {
|
||||
if p.IsRunning() {
|
||||
procs = append(procs, p)
|
||||
}
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
|
||||
for _, p := range procs {
|
||||
_ = p.Shutdown()
|
||||
}
|
||||
|
||||
return nil
|
||||
// OnShutdown implements core.Stoppable — kills all managed processes.
|
||||
//
|
||||
// c.ServiceShutdown(ctx) // calls OnShutdown on all Stoppable services
|
||||
func (s *Service) OnShutdown(ctx context.Context) core.Result {
|
||||
s.managed.Each(func(_ string, proc *ManagedProcess) {
|
||||
_ = proc.Kill()
|
||||
})
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
|
||||
// Start spawns a new process with the given command and args.
|
||||
func (s *Service) Start(ctx context.Context, command string, args ...string) (*Process, error) {
|
||||
//
|
||||
// r := svc.Start(ctx, "echo", "hello")
|
||||
// if r.OK { proc := r.Value.(*Process) }
|
||||
func (s *Service) Start(ctx context.Context, command string, args ...string) core.Result {
|
||||
return s.StartWithOptions(ctx, RunOptions{
|
||||
Command: command,
|
||||
Args: args,
|
||||
|
|
@ -95,8 +90,18 @@ func (s *Service) Start(ctx context.Context, command string, args ...string) (*P
|
|||
}
|
||||
|
||||
// StartWithOptions spawns a process with full configuration.
|
||||
func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Process, error) {
|
||||
id := fmt.Sprintf("proc-%d", s.idCounter.Add(1))
|
||||
//
|
||||
// r := svc.StartWithOptions(ctx, process.RunOptions{Command: "go", Args: []string{"test", "./..."}})
|
||||
// if r.OK { proc := r.Value.(*Process) }
|
||||
func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) core.Result {
|
||||
if opts.Command == "" {
|
||||
return core.Result{Value: core.E("process.start", "command is required", nil), OK: false}
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
id := core.ID()
|
||||
|
||||
// Detached processes use Background context so they survive parent death
|
||||
parentCtx := ctx
|
||||
|
|
@ -104,7 +109,7 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce
|
|||
parentCtx = context.Background()
|
||||
}
|
||||
procCtx, cancel := context.WithCancel(parentCtx)
|
||||
cmd := exec.CommandContext(procCtx, opts.Command, opts.Args...)
|
||||
cmd := execCommandContext(procCtx, opts.Command, opts.Args...)
|
||||
|
||||
if opts.Dir != "" {
|
||||
cmd.Dir = opts.Dir
|
||||
|
|
@ -122,19 +127,19 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce
|
|||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, coreerr.E("Service.StartWithOptions", "failed to create stdout pipe", err)
|
||||
return core.Result{Value: core.E("process.start", core.Concat("stdout pipe failed: ", opts.Command), err), OK: false}
|
||||
}
|
||||
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, coreerr.E("Service.StartWithOptions", "failed to create stderr pipe", err)
|
||||
return core.Result{Value: core.E("process.start", core.Concat("stderr pipe failed: ", opts.Command), err), OK: false}
|
||||
}
|
||||
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, coreerr.E("Service.StartWithOptions", "failed to create stdin pipe", err)
|
||||
return core.Result{Value: core.E("process.start", core.Concat("stdin pipe failed: ", opts.Command), err), OK: false}
|
||||
}
|
||||
|
||||
// Create output buffer (enabled by default)
|
||||
|
|
@ -143,12 +148,12 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce
|
|||
output = NewRingBuffer(s.bufSize)
|
||||
}
|
||||
|
||||
proc := &Process{
|
||||
proc := &ManagedProcess{
|
||||
ID: id,
|
||||
Command: opts.Command,
|
||||
Args: opts.Args,
|
||||
Args: append([]string(nil), opts.Args...),
|
||||
Dir: opts.Dir,
|
||||
Env: opts.Env,
|
||||
Env: append([]string(nil), opts.Env...),
|
||||
StartedAt: time.Now(),
|
||||
Status: StatusRunning,
|
||||
cmd: cmd,
|
||||
|
|
@ -164,20 +169,22 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce
|
|||
// Start the process
|
||||
if err := cmd.Start(); err != nil {
|
||||
cancel()
|
||||
return nil, coreerr.E("Service.StartWithOptions", "failed to start process", err)
|
||||
return core.Result{Value: core.E("process.start", core.Concat("command failed: ", opts.Command), err), OK: false}
|
||||
}
|
||||
proc.PID = cmd.Process.Pid
|
||||
|
||||
// Store process
|
||||
s.mu.Lock()
|
||||
s.processes[id] = proc
|
||||
s.mu.Unlock()
|
||||
if r := s.managed.Set(id, proc); !r.OK {
|
||||
cancel()
|
||||
_ = cmd.Process.Kill()
|
||||
return r
|
||||
}
|
||||
|
||||
// Start timeout watchdog if configured
|
||||
if opts.Timeout > 0 {
|
||||
go func() {
|
||||
select {
|
||||
case <-proc.done:
|
||||
// Process exited before timeout
|
||||
case <-time.After(opts.Timeout):
|
||||
proc.Shutdown()
|
||||
}
|
||||
|
|
@ -185,7 +192,7 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce
|
|||
}
|
||||
|
||||
// Broadcast start
|
||||
_ = s.Core().ACTION(ActionProcessStarted{
|
||||
s.Core().ACTION(ActionProcessStarted{
|
||||
ID: id,
|
||||
Command: opts.Command,
|
||||
Args: opts.Args,
|
||||
|
|
@ -207,52 +214,40 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce
|
|||
|
||||
// Wait for process completion
|
||||
go func() {
|
||||
// Wait for output streaming to complete
|
||||
wg.Wait()
|
||||
|
||||
// Wait for process exit
|
||||
err := cmd.Wait()
|
||||
waitErr := cmd.Wait()
|
||||
|
||||
duration := time.Since(proc.StartedAt)
|
||||
status, exitCode, actionErr, killedSignal := classifyProcessExit(proc, waitErr)
|
||||
|
||||
proc.mu.Lock()
|
||||
proc.PID = cmd.Process.Pid
|
||||
proc.Duration = duration
|
||||
if err != nil {
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
proc.ExitCode = exitErr.ExitCode()
|
||||
proc.Status = StatusExited
|
||||
} else {
|
||||
proc.Status = StatusFailed
|
||||
}
|
||||
} else {
|
||||
proc.ExitCode = 0
|
||||
proc.Status = StatusExited
|
||||
}
|
||||
status := proc.Status
|
||||
exitCode := proc.ExitCode
|
||||
proc.ExitCode = exitCode
|
||||
proc.Status = status
|
||||
proc.mu.Unlock()
|
||||
|
||||
close(proc.done)
|
||||
|
||||
// Broadcast exit
|
||||
var exitErr error
|
||||
if status == StatusFailed {
|
||||
exitErr = err
|
||||
if status == StatusKilled {
|
||||
_ = s.Core().ACTION(ActionProcessKilled{
|
||||
ID: id,
|
||||
Signal: killedSignal,
|
||||
})
|
||||
}
|
||||
_ = s.Core().ACTION(ActionProcessExited{
|
||||
s.Core().ACTION(ActionProcessExited{
|
||||
ID: id,
|
||||
ExitCode: exitCode,
|
||||
Duration: duration,
|
||||
Error: exitErr,
|
||||
Error: actionErr,
|
||||
})
|
||||
}()
|
||||
|
||||
return proc, nil
|
||||
return core.Result{Value: proc, OK: true}
|
||||
}
|
||||
|
||||
// streamOutput reads from a pipe and broadcasts lines via ACTION.
|
||||
func (s *Service) streamOutput(proc *Process, r io.Reader, stream Stream) {
|
||||
func (s *Service) streamOutput(proc *ManagedProcess, r streamReader, stream Stream) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
// Increase buffer for long lines
|
||||
scanner.Buffer(make([]byte, 64*1024), 1024*1024)
|
||||
|
|
@ -275,44 +270,43 @@ func (s *Service) streamOutput(proc *Process, r io.Reader, stream Stream) {
|
|||
}
|
||||
|
||||
// Get returns a process by ID.
|
||||
func (s *Service) Get(id string) (*Process, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
proc, ok := s.processes[id]
|
||||
if !ok {
|
||||
//
|
||||
// proc, err := svc.Get("abc123")
|
||||
func (s *Service) Get(id string) (*ManagedProcess, error) {
|
||||
r := s.managed.Get(id)
|
||||
if !r.OK {
|
||||
return nil, ErrProcessNotFound
|
||||
}
|
||||
return proc, nil
|
||||
return r.Value.(*ManagedProcess), nil
|
||||
}
|
||||
|
||||
// List returns all processes.
|
||||
func (s *Service) List() []*Process {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
result := make([]*Process, 0, len(s.processes))
|
||||
for _, p := range s.processes {
|
||||
result = append(result, p)
|
||||
}
|
||||
//
|
||||
// procs := svc.List()
|
||||
func (s *Service) List() []*ManagedProcess {
|
||||
result := make([]*ManagedProcess, 0, s.managed.Len())
|
||||
s.managed.Each(func(_ string, proc *ManagedProcess) {
|
||||
result = append(result, proc)
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
// Running returns all currently running processes.
|
||||
func (s *Service) Running() []*Process {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var result []*Process
|
||||
for _, p := range s.processes {
|
||||
if p.IsRunning() {
|
||||
result = append(result, p)
|
||||
//
|
||||
// active := svc.Running()
|
||||
func (s *Service) Running() []*ManagedProcess {
|
||||
result := make([]*ManagedProcess, 0, s.managed.Len())
|
||||
s.managed.Each(func(_ string, proc *ManagedProcess) {
|
||||
if proc.IsRunning() {
|
||||
result = append(result, proc)
|
||||
}
|
||||
}
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
// Kill terminates a process by ID.
|
||||
//
|
||||
// err := svc.Kill("abc123")
|
||||
func (s *Service) Kill(id string) error {
|
||||
proc, err := s.Get(id)
|
||||
if err != nil {
|
||||
|
|
@ -322,46 +316,45 @@ func (s *Service) Kill(id string) error {
|
|||
if err := proc.Kill(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = s.Core().ACTION(ActionProcessKilled{
|
||||
ID: id,
|
||||
Signal: "SIGKILL",
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove removes a completed process from the list.
|
||||
//
|
||||
// err := svc.Remove("abc123")
|
||||
func (s *Service) Remove(id string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
proc, ok := s.processes[id]
|
||||
if !ok {
|
||||
proc, err := s.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if proc.IsRunning() {
|
||||
return core.E("process.remove", core.Concat("cannot remove running process: ", id), nil)
|
||||
}
|
||||
r := s.managed.Delete(id)
|
||||
if !r.OK {
|
||||
return ErrProcessNotFound
|
||||
}
|
||||
|
||||
if proc.IsRunning() {
|
||||
return coreerr.E("Service.Remove", "cannot remove running process", nil)
|
||||
}
|
||||
|
||||
delete(s.processes, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Clear removes all completed processes.
|
||||
//
|
||||
// svc.Clear()
|
||||
func (s *Service) Clear() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
for id, p := range s.processes {
|
||||
if !p.IsRunning() {
|
||||
delete(s.processes, id)
|
||||
ids := make([]string, 0)
|
||||
s.managed.Each(func(id string, proc *ManagedProcess) {
|
||||
if !proc.IsRunning() {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
})
|
||||
for _, id := range ids {
|
||||
s.managed.Delete(id)
|
||||
}
|
||||
}
|
||||
|
||||
// Output returns the captured output of a process.
|
||||
//
|
||||
// output, err := svc.Output("abc123")
|
||||
func (s *Service) Output(id string) (string, error) {
|
||||
proc, err := s.Get(id)
|
||||
if err != nil {
|
||||
|
|
@ -371,34 +364,286 @@ func (s *Service) Output(id string) (string, error) {
|
|||
}
|
||||
|
||||
// Run executes a command and waits for completion.
|
||||
// Returns the combined output and any error.
|
||||
func (s *Service) Run(ctx context.Context, command string, args ...string) (string, error) {
|
||||
proc, err := s.Start(ctx, command, args...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
output := proc.Output()
|
||||
if proc.ExitCode != 0 {
|
||||
return output, coreerr.E("Service.Run", fmt.Sprintf("process exited with code %d", proc.ExitCode), nil)
|
||||
}
|
||||
return output, nil
|
||||
// Value is always the output string. OK is true if exit code is 0.
|
||||
//
|
||||
// r := svc.Run(ctx, "go", "test", "./...")
|
||||
// output := r.Value.(string)
|
||||
func (s *Service) Run(ctx context.Context, command string, args ...string) core.Result {
|
||||
return s.RunWithOptions(ctx, RunOptions{
|
||||
Command: command,
|
||||
Args: args,
|
||||
})
|
||||
}
|
||||
|
||||
// RunWithOptions executes a command with options and waits for completion.
|
||||
func (s *Service) RunWithOptions(ctx context.Context, opts RunOptions) (string, error) {
|
||||
proc, err := s.StartWithOptions(ctx, opts)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
output := proc.Output()
|
||||
if proc.ExitCode != 0 {
|
||||
return output, coreerr.E("Service.RunWithOptions", fmt.Sprintf("process exited with code %d", proc.ExitCode), nil)
|
||||
}
|
||||
return output, nil
|
||||
// Value is always the output string. OK is true if exit code is 0.
|
||||
//
|
||||
// r := svc.RunWithOptions(ctx, process.RunOptions{Command: "go", Args: []string{"test"}})
|
||||
// output := r.Value.(string)
|
||||
func (s *Service) RunWithOptions(ctx context.Context, opts RunOptions) core.Result {
|
||||
return s.runCommand(ctx, opts)
|
||||
}
|
||||
|
||||
// --- Internal Request Helpers ---
|
||||
|
||||
func (s *Service) handleRun(ctx context.Context, opts core.Options) core.Result {
|
||||
command := opts.String("command")
|
||||
if command == "" {
|
||||
return core.Result{Value: core.E("process.run", "command is required", nil), OK: false}
|
||||
}
|
||||
|
||||
runOpts := RunOptions{
|
||||
Command: command,
|
||||
Dir: opts.String("dir"),
|
||||
}
|
||||
if r := opts.Get("args"); r.OK {
|
||||
runOpts.Args = optionStrings(r.Value)
|
||||
}
|
||||
if r := opts.Get("env"); r.OK {
|
||||
runOpts.Env = optionStrings(r.Value)
|
||||
}
|
||||
|
||||
return s.runCommand(ctx, runOpts)
|
||||
}
|
||||
|
||||
func (s *Service) handleStart(ctx context.Context, opts core.Options) core.Result {
|
||||
command := opts.String("command")
|
||||
if command == "" {
|
||||
return core.Result{Value: core.E("process.start", "command is required", nil), OK: false}
|
||||
}
|
||||
|
||||
runOpts := RunOptions{
|
||||
Command: command,
|
||||
Dir: opts.String("dir"),
|
||||
Detach: true,
|
||||
}
|
||||
if r := opts.Get("args"); r.OK {
|
||||
runOpts.Args = optionStrings(r.Value)
|
||||
}
|
||||
if r := opts.Get("env"); r.OK {
|
||||
runOpts.Env = optionStrings(r.Value)
|
||||
}
|
||||
|
||||
startResult := s.StartWithOptions(ctx, runOpts)
|
||||
if !startResult.OK {
|
||||
return startResult
|
||||
}
|
||||
return core.Result{Value: startResult.Value.(*ManagedProcess).ID, OK: true}
|
||||
}
|
||||
|
||||
func (s *Service) handleKill(ctx context.Context, opts core.Options) core.Result {
|
||||
id := opts.String("id")
|
||||
if id != "" {
|
||||
if err := s.Kill(id); err != nil {
|
||||
if core.Is(err, ErrProcessNotFound) {
|
||||
return core.Result{Value: core.E("process.kill", core.Concat("not found: ", id), nil), OK: false}
|
||||
}
|
||||
return core.Result{Value: err, OK: false}
|
||||
}
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
|
||||
pid := opts.Int("pid")
|
||||
if pid > 0 {
|
||||
proc, err := processHandle(pid)
|
||||
if err != nil {
|
||||
return core.Result{Value: core.E("process.kill", core.Concat("find pid failed: ", core.Sprintf("%d", pid)), err), OK: false}
|
||||
}
|
||||
if err := proc.Signal(syscall.SIGTERM); err != nil {
|
||||
return core.Result{Value: core.E("process.kill", core.Concat("signal failed: ", core.Sprintf("%d", pid)), err), OK: false}
|
||||
}
|
||||
return core.Result{OK: true}
|
||||
}
|
||||
|
||||
return core.Result{Value: core.E("process.kill", "need id or pid", nil), OK: false}
|
||||
}
|
||||
|
||||
func (s *Service) handleList(ctx context.Context, opts core.Options) core.Result {
|
||||
return core.Result{Value: s.managed.Names(), OK: true}
|
||||
}
|
||||
|
||||
func (s *Service) runCommand(ctx context.Context, opts RunOptions) core.Result {
|
||||
if opts.Command == "" {
|
||||
return core.Result{Value: core.E("process.run", "command is required", nil), OK: false}
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
cmd := execCommandContext(ctx, opts.Command, opts.Args...)
|
||||
if opts.Dir != "" {
|
||||
cmd.Dir = opts.Dir
|
||||
}
|
||||
if len(opts.Env) > 0 {
|
||||
cmd.Env = append(cmd.Environ(), opts.Env...)
|
||||
}
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return core.Result{Value: core.E("process.run", core.Concat("command failed: ", opts.Command), err), OK: false}
|
||||
}
|
||||
return core.Result{Value: string(output), OK: true}
|
||||
}
|
||||
|
||||
// Signal sends a signal to the process.
|
||||
//
|
||||
// err := proc.Signal(syscall.SIGTERM)
|
||||
func (p *ManagedProcess) Signal(sig os.Signal) error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
if p.Status != StatusRunning {
|
||||
return ErrProcessNotRunning
|
||||
}
|
||||
|
||||
if p.cmd == nil || p.cmd.Process == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if signal, ok := sig.(syscall.Signal); ok {
|
||||
p.lastSignal = normalizeSignalName(signal)
|
||||
}
|
||||
return p.cmd.Process.Signal(sig)
|
||||
}
|
||||
|
||||
func execCommandContext(ctx context.Context, name string, args ...string) *exec.Cmd {
|
||||
return exec.CommandContext(ctx, name, args...)
|
||||
}
|
||||
|
||||
func execLookPath(name string) (string, error) {
|
||||
return exec.LookPath(name)
|
||||
}
|
||||
|
||||
func currentPID() int {
|
||||
return os.Getpid()
|
||||
}
|
||||
|
||||
func processHandle(pid int) (*os.Process, error) {
|
||||
return os.FindProcess(pid)
|
||||
}
|
||||
|
||||
func userHomeDir() (string, error) {
|
||||
return os.UserHomeDir()
|
||||
}
|
||||
|
||||
func tempDir() string {
|
||||
return os.TempDir()
|
||||
}
|
||||
|
||||
func isNotExist(err error) bool {
|
||||
return os.IsNotExist(err)
|
||||
}
|
||||
|
||||
// KillPID sends SIGTERM to the process identified by pid.
|
||||
// Use this instead of os.FindProcess+syscall in consumer packages.
|
||||
//
|
||||
// err := process.KillPID(entry.PID)
|
||||
func KillPID(pid int) error {
|
||||
proc, err := processHandle(pid)
|
||||
if err != nil {
|
||||
return core.E("process.kill_pid", core.Sprintf("find pid %d failed", pid), err)
|
||||
}
|
||||
if err := proc.Signal(syscall.SIGTERM); err != nil {
|
||||
return core.E("process.kill_pid", core.Sprintf("signal pid %d failed", pid), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsPIDAlive returns true if the process with the given pid is running.
|
||||
// Use this instead of os.FindProcess+syscall.Signal(0) in consumer packages.
|
||||
//
|
||||
// alive := process.IsPIDAlive(entry.PID)
|
||||
func IsPIDAlive(pid int) bool {
|
||||
if pid <= 0 {
|
||||
return false
|
||||
}
|
||||
proc, err := processHandle(pid)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return proc.Signal(syscall.Signal(0)) == nil
|
||||
}
|
||||
|
||||
func (s *Service) handleGet(ctx context.Context, opts core.Options) core.Result {
|
||||
id := opts.String("id")
|
||||
if id == "" {
|
||||
return core.Result{Value: core.E("process.get", "id is required", nil), OK: false}
|
||||
}
|
||||
proc, err := s.Get(id)
|
||||
if err != nil {
|
||||
return core.Result{Value: core.E("process.get", core.Concat("not found: ", id), err), OK: false}
|
||||
}
|
||||
return core.Result{Value: proc.Info(), OK: true}
|
||||
}
|
||||
|
||||
func optionStrings(value any) []string {
|
||||
switch typed := value.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case []string:
|
||||
return append([]string(nil), typed...)
|
||||
case []any:
|
||||
result := make([]string, 0, len(typed))
|
||||
for _, item := range typed {
|
||||
text, ok := item.(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
result = append(result, text)
|
||||
}
|
||||
return result
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func classifyProcessExit(proc *ManagedProcess, err error) (Status, int, error, string) {
|
||||
if err == nil {
|
||||
return StatusExited, 0, nil, ""
|
||||
}
|
||||
|
||||
if sig, ok := processExitSignal(err); ok {
|
||||
return StatusKilled, -1, err, normalizeSignalName(sig)
|
||||
}
|
||||
|
||||
if ctxErr := proc.ctx.Err(); ctxErr != nil {
|
||||
signal := proc.requestedSignal()
|
||||
if signal == "" {
|
||||
signal = "SIGKILL"
|
||||
}
|
||||
return StatusKilled, -1, ctxErr, signal
|
||||
}
|
||||
|
||||
var exitErr *exec.ExitError
|
||||
if core.As(err, &exitErr) {
|
||||
return StatusExited, exitErr.ExitCode(), err, ""
|
||||
}
|
||||
|
||||
return StatusFailed, -1, err, ""
|
||||
}
|
||||
|
||||
func processExitSignal(err error) (syscall.Signal, bool) {
|
||||
var exitErr *exec.ExitError
|
||||
if !core.As(err, &exitErr) || exitErr.ProcessState == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
waitStatus, ok := exitErr.ProcessState.Sys().(syscall.WaitStatus)
|
||||
if !ok || !waitStatus.Signaled() {
|
||||
return 0, false
|
||||
}
|
||||
return waitStatus.Signal(), true
|
||||
}
|
||||
|
||||
func normalizeSignalName(sig syscall.Signal) string {
|
||||
switch sig {
|
||||
case syscall.SIGINT:
|
||||
return "SIGINT"
|
||||
case syscall.SIGKILL:
|
||||
return "SIGKILL"
|
||||
case syscall.SIGTERM:
|
||||
return "SIGTERM"
|
||||
default:
|
||||
return sig.String()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
410
service_test.go
410
service_test.go
|
|
@ -2,7 +2,6 @@ package process
|
|||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -16,27 +15,243 @@ func newTestService(t *testing.T) (*Service, *framework.Core) {
|
|||
t.Helper()
|
||||
|
||||
c := framework.New()
|
||||
factory := NewService(Options{BufferSize: 1024})
|
||||
raw, err := factory(c)
|
||||
require.NoError(t, err)
|
||||
r := Register(c)
|
||||
require.True(t, r.OK)
|
||||
return r.Value.(*Service), c
|
||||
}
|
||||
|
||||
svc := raw.(*Service)
|
||||
func newStartedTestService(t *testing.T) (*Service, *framework.Core) {
|
||||
t.Helper()
|
||||
|
||||
svc, c := newTestService(t)
|
||||
r := svc.OnStartup(context.Background())
|
||||
require.True(t, r.OK)
|
||||
return svc, c
|
||||
}
|
||||
|
||||
func TestService_Start(t *testing.T) {
|
||||
func TestService_Register_Good(t *testing.T) {
|
||||
c := framework.New(framework.WithService(Register))
|
||||
|
||||
svc, ok := framework.ServiceFor[*Service](c, "process")
|
||||
require.True(t, ok)
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
|
||||
func TestService_OnStartup_Good(t *testing.T) {
|
||||
svc, c := newTestService(t)
|
||||
|
||||
r := svc.OnStartup(context.Background())
|
||||
require.True(t, r.OK)
|
||||
|
||||
assert.True(t, c.Action("process.run").Exists())
|
||||
assert.True(t, c.Action("process.start").Exists())
|
||||
assert.True(t, c.Action("process.kill").Exists())
|
||||
assert.True(t, c.Action("process.list").Exists())
|
||||
assert.True(t, c.Action("process.get").Exists())
|
||||
}
|
||||
|
||||
func TestService_HandleRun_Good(t *testing.T) {
|
||||
_, c := newStartedTestService(t)
|
||||
|
||||
r := c.Action("process.run").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "echo"},
|
||||
framework.Option{Key: "args", Value: []string{"hello"}},
|
||||
))
|
||||
require.True(t, r.OK)
|
||||
assert.Contains(t, r.Value.(string), "hello")
|
||||
}
|
||||
|
||||
func TestService_HandleRun_Bad(t *testing.T) {
|
||||
_, c := newStartedTestService(t)
|
||||
|
||||
r := c.Action("process.run").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "nonexistent_command_xyz"},
|
||||
))
|
||||
assert.False(t, r.OK)
|
||||
}
|
||||
|
||||
func TestService_HandleRun_Ugly(t *testing.T) {
|
||||
_, c := newStartedTestService(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
r := c.Action("process.run").Run(ctx, framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "sleep"},
|
||||
framework.Option{Key: "args", Value: []string{"1"}},
|
||||
))
|
||||
assert.False(t, r.OK)
|
||||
}
|
||||
|
||||
func TestService_HandleStart_Good(t *testing.T) {
|
||||
svc, c := newStartedTestService(t)
|
||||
|
||||
r := c.Action("process.start").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "sleep"},
|
||||
framework.Option{Key: "args", Value: []string{"60"}},
|
||||
))
|
||||
require.True(t, r.OK)
|
||||
|
||||
id := r.Value.(string)
|
||||
proc, err := svc.Get(id)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, proc.IsRunning())
|
||||
|
||||
kill := c.Action("process.kill").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "id", Value: id},
|
||||
))
|
||||
require.True(t, kill.OK)
|
||||
<-proc.Done()
|
||||
}
|
||||
|
||||
func TestService_HandleStart_Bad(t *testing.T) {
|
||||
_, c := newStartedTestService(t)
|
||||
|
||||
r := c.Action("process.start").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "nonexistent_command_xyz"},
|
||||
))
|
||||
assert.False(t, r.OK)
|
||||
}
|
||||
|
||||
func TestService_HandleKill_Good(t *testing.T) {
|
||||
svc, c := newStartedTestService(t)
|
||||
|
||||
start := c.Action("process.start").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "sleep"},
|
||||
framework.Option{Key: "args", Value: []string{"60"}},
|
||||
))
|
||||
require.True(t, start.OK)
|
||||
|
||||
id := start.Value.(string)
|
||||
proc, err := svc.Get(id)
|
||||
require.NoError(t, err)
|
||||
|
||||
kill := c.Action("process.kill").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "id", Value: id},
|
||||
))
|
||||
require.True(t, kill.OK)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("process should have been killed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_HandleKill_Bad(t *testing.T) {
|
||||
_, c := newStartedTestService(t)
|
||||
|
||||
r := c.Action("process.kill").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "id", Value: "missing"},
|
||||
))
|
||||
assert.False(t, r.OK)
|
||||
}
|
||||
|
||||
func TestService_HandleList_Good(t *testing.T) {
|
||||
svc, c := newStartedTestService(t)
|
||||
|
||||
startOne := c.Action("process.start").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "sleep"},
|
||||
framework.Option{Key: "args", Value: []string{"60"}},
|
||||
))
|
||||
require.True(t, startOne.OK)
|
||||
startTwo := c.Action("process.start").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "sleep"},
|
||||
framework.Option{Key: "args", Value: []string{"60"}},
|
||||
))
|
||||
require.True(t, startTwo.OK)
|
||||
|
||||
r := c.Action("process.list").Run(context.Background(), framework.NewOptions())
|
||||
require.True(t, r.OK)
|
||||
|
||||
ids := r.Value.([]string)
|
||||
assert.Len(t, ids, 2)
|
||||
|
||||
for _, id := range ids {
|
||||
proc, err := svc.Get(id)
|
||||
require.NoError(t, err)
|
||||
_ = proc.Kill()
|
||||
<-proc.Done()
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_HandleGet_Good(t *testing.T) {
|
||||
svc, c := newStartedTestService(t)
|
||||
|
||||
start := c.Action("process.start").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "command", Value: "sleep"},
|
||||
framework.Option{Key: "args", Value: []string{"60"}},
|
||||
))
|
||||
require.True(t, start.OK)
|
||||
|
||||
id := start.Value.(string)
|
||||
r := c.Action("process.get").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "id", Value: id},
|
||||
))
|
||||
require.True(t, r.OK)
|
||||
|
||||
info := r.Value.(ProcessInfo)
|
||||
assert.Equal(t, id, info.ID)
|
||||
assert.Equal(t, "sleep", info.Command)
|
||||
assert.True(t, info.Running)
|
||||
assert.Equal(t, StatusRunning, info.Status)
|
||||
assert.Positive(t, info.PID)
|
||||
|
||||
proc, err := svc.Get(id)
|
||||
require.NoError(t, err)
|
||||
_ = proc.Kill()
|
||||
<-proc.Done()
|
||||
}
|
||||
|
||||
func TestService_HandleGet_Bad(t *testing.T) {
|
||||
_, c := newStartedTestService(t)
|
||||
|
||||
missingID := c.Action("process.get").Run(context.Background(), framework.NewOptions())
|
||||
assert.False(t, missingID.OK)
|
||||
|
||||
missingProc := c.Action("process.get").Run(context.Background(), framework.NewOptions(
|
||||
framework.Option{Key: "id", Value: "missing"},
|
||||
))
|
||||
assert.False(t, missingProc.OK)
|
||||
}
|
||||
|
||||
func TestService_Ugly_PermissionModel(t *testing.T) {
|
||||
c := framework.New()
|
||||
|
||||
r := c.Process().Run(context.Background(), "echo", "blocked")
|
||||
assert.False(t, r.OK)
|
||||
|
||||
c = framework.New(framework.WithService(Register))
|
||||
startup := c.ServiceStartup(context.Background(), nil)
|
||||
require.True(t, startup.OK)
|
||||
defer func() {
|
||||
shutdown := c.ServiceShutdown(context.Background())
|
||||
assert.True(t, shutdown.OK)
|
||||
}()
|
||||
|
||||
r = c.Process().Run(context.Background(), "echo", "allowed")
|
||||
require.True(t, r.OK)
|
||||
assert.Contains(t, r.Value.(string), "allowed")
|
||||
}
|
||||
|
||||
func startProc(t *testing.T, svc *Service, ctx context.Context, command string, args ...string) *Process {
|
||||
t.Helper()
|
||||
r := svc.Start(ctx, command, args...)
|
||||
require.True(t, r.OK)
|
||||
return r.Value.(*Process)
|
||||
}
|
||||
|
||||
func TestService_Start_Good(t *testing.T) {
|
||||
t.Run("echo command", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "hello")
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, proc)
|
||||
proc := startProc(t, svc, context.Background(), "echo", "hello")
|
||||
|
||||
assert.NotEmpty(t, proc.ID)
|
||||
assert.Positive(t, proc.PID)
|
||||
assert.Equal(t, "echo", proc.Command)
|
||||
assert.Equal(t, []string{"hello"}, proc.Args)
|
||||
|
||||
// Wait for completion
|
||||
<-proc.Done()
|
||||
|
||||
assert.Equal(t, StatusExited, proc.Status)
|
||||
|
|
@ -47,8 +262,7 @@ func TestService_Start(t *testing.T) {
|
|||
t.Run("failing command", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "sh", "-c", "exit 42")
|
||||
require.NoError(t, err)
|
||||
proc := startProc(t, svc, context.Background(), "sh", "-c", "exit 42")
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
|
|
@ -59,23 +273,23 @@ func TestService_Start(t *testing.T) {
|
|||
t.Run("non-existent command", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
_, err := svc.Start(context.Background(), "nonexistent_command_xyz")
|
||||
assert.Error(t, err)
|
||||
r := svc.Start(context.Background(), "nonexistent_command_xyz")
|
||||
assert.False(t, r.OK)
|
||||
})
|
||||
|
||||
t.Run("with working directory", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "pwd",
|
||||
Dir: "/tmp",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
// On macOS /tmp is a symlink to /private/tmp
|
||||
output := strings.TrimSpace(proc.Output())
|
||||
output := framework.Trim(proc.Output())
|
||||
assert.True(t, output == "/tmp" || output == "/private/tmp", "got: %s", output)
|
||||
})
|
||||
|
||||
|
|
@ -83,15 +297,12 @@ func TestService_Start(t *testing.T) {
|
|||
svc, _ := newTestService(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
proc, err := svc.Start(ctx, "sleep", "10")
|
||||
require.NoError(t, err)
|
||||
proc := startProc(t, svc, ctx, "sleep", "10")
|
||||
|
||||
// Cancel immediately
|
||||
cancel()
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Good - process was killed
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("process should have been killed")
|
||||
}
|
||||
|
|
@ -100,12 +311,13 @@ func TestService_Start(t *testing.T) {
|
|||
t.Run("disable capture", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "echo",
|
||||
Args: []string{"no-capture"},
|
||||
DisableCapture: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
<-proc.Done()
|
||||
|
||||
assert.Equal(t, StatusExited, proc.Status)
|
||||
|
|
@ -115,12 +327,13 @@ func TestService_Start(t *testing.T) {
|
|||
t.Run("with environment variables", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
r := svc.StartWithOptions(context.Background(), RunOptions{
|
||||
Command: "sh",
|
||||
Args: []string{"-c", "echo $MY_TEST_VAR"},
|
||||
Env: []string{"MY_TEST_VAR=hello_env"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
<-proc.Done()
|
||||
|
||||
assert.Contains(t, proc.Output(), "hello_env")
|
||||
|
|
@ -131,17 +344,16 @@ func TestService_Start(t *testing.T) {
|
|||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
proc, err := svc.StartWithOptions(ctx, RunOptions{
|
||||
r := svc.StartWithOptions(ctx, RunOptions{
|
||||
Command: "echo",
|
||||
Args: []string{"detached"},
|
||||
Detach: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, r.OK)
|
||||
proc := r.Value.(*Process)
|
||||
|
||||
// Cancel the parent context
|
||||
cancel()
|
||||
|
||||
// Detached process should still complete normally
|
||||
select {
|
||||
case <-proc.Done():
|
||||
assert.Equal(t, StatusExited, proc.Status)
|
||||
|
|
@ -152,33 +364,26 @@ func TestService_Start(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestService_Run(t *testing.T) {
|
||||
func TestService_Run_Good(t *testing.T) {
|
||||
t.Run("returns output", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
output, err := svc.Run(context.Background(), "echo", "hello world")
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, output, "hello world")
|
||||
r := svc.Run(context.Background(), "echo", "hello world")
|
||||
assert.True(t, r.OK)
|
||||
assert.Contains(t, r.Value.(string), "hello world")
|
||||
})
|
||||
|
||||
t.Run("returns error on failure", func(t *testing.T) {
|
||||
t.Run("returns !OK on failure", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
_, err := svc.Run(context.Background(), "sh", "-c", "exit 1")
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "exited with code 1")
|
||||
r := svc.Run(context.Background(), "sh", "-c", "exit 1")
|
||||
assert.False(t, r.OK)
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_Actions(t *testing.T) {
|
||||
func TestService_Actions_Good(t *testing.T) {
|
||||
t.Run("broadcasts events", func(t *testing.T) {
|
||||
c := framework.New()
|
||||
|
||||
// Register process service on Core
|
||||
factory := NewService(Options{})
|
||||
raw, err := factory(c)
|
||||
require.NoError(t, err)
|
||||
svc := raw.(*Service)
|
||||
svc, c := newTestService(t)
|
||||
|
||||
var started []ActionProcessStarted
|
||||
var outputs []ActionProcessOutput
|
||||
|
|
@ -198,12 +403,10 @@ func TestService_Actions(t *testing.T) {
|
|||
}
|
||||
return framework.Result{OK: true}
|
||||
})
|
||||
proc, err := svc.Start(context.Background(), "echo", "test")
|
||||
require.NoError(t, err)
|
||||
proc := startProc(t, svc, context.Background(), "echo", "test")
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
// Give time for events to propagate
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
mu.Lock()
|
||||
|
|
@ -216,7 +419,7 @@ func TestService_Actions(t *testing.T) {
|
|||
assert.NotEmpty(t, outputs)
|
||||
foundTest := false
|
||||
for _, o := range outputs {
|
||||
if strings.Contains(o.Line, "test") {
|
||||
if framework.Contains(o.Line, "test") {
|
||||
foundTest = true
|
||||
break
|
||||
}
|
||||
|
|
@ -226,14 +429,44 @@ func TestService_Actions(t *testing.T) {
|
|||
assert.Len(t, exited, 1)
|
||||
assert.Equal(t, 0, exited[0].ExitCode)
|
||||
})
|
||||
|
||||
t.Run("broadcasts killed event", func(t *testing.T) {
|
||||
svc, c := newTestService(t)
|
||||
|
||||
var killed []ActionProcessKilled
|
||||
var mu sync.Mutex
|
||||
|
||||
c.RegisterAction(func(cc *framework.Core, msg framework.Message) framework.Result {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if m, ok := msg.(ActionProcessKilled); ok {
|
||||
killed = append(killed, m)
|
||||
}
|
||||
return framework.Result{OK: true}
|
||||
})
|
||||
|
||||
proc := startProc(t, svc, context.Background(), "sleep", "60")
|
||||
err := svc.Kill(proc.ID)
|
||||
require.NoError(t, err)
|
||||
<-proc.Done()
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
require.Len(t, killed, 1)
|
||||
assert.Equal(t, proc.ID, killed[0].ID)
|
||||
assert.Equal(t, "SIGKILL", killed[0].Signal)
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_List(t *testing.T) {
|
||||
func TestService_List_Good(t *testing.T) {
|
||||
t.Run("tracks processes", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc1, _ := svc.Start(context.Background(), "echo", "1")
|
||||
proc2, _ := svc.Start(context.Background(), "echo", "2")
|
||||
proc1 := startProc(t, svc, context.Background(), "echo", "1")
|
||||
proc2 := startProc(t, svc, context.Background(), "echo", "2")
|
||||
|
||||
<-proc1.Done()
|
||||
<-proc2.Done()
|
||||
|
|
@ -245,7 +478,7 @@ func TestService_List(t *testing.T) {
|
|||
t.Run("get by id", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, _ := svc.Start(context.Background(), "echo", "test")
|
||||
proc := startProc(t, svc, context.Background(), "echo", "test")
|
||||
<-proc.Done()
|
||||
|
||||
got, err := svc.Get(proc.ID)
|
||||
|
|
@ -261,11 +494,11 @@ func TestService_List(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestService_Remove(t *testing.T) {
|
||||
func TestService_Remove_Good(t *testing.T) {
|
||||
t.Run("removes completed process", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, _ := svc.Start(context.Background(), "echo", "test")
|
||||
proc := startProc(t, svc, context.Background(), "echo", "test")
|
||||
<-proc.Done()
|
||||
|
||||
err := svc.Remove(proc.ID)
|
||||
|
|
@ -281,7 +514,7 @@ func TestService_Remove(t *testing.T) {
|
|||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc, _ := svc.Start(ctx, "sleep", "10")
|
||||
proc := startProc(t, svc, ctx, "sleep", "10")
|
||||
|
||||
err := svc.Remove(proc.ID)
|
||||
assert.Error(t, err)
|
||||
|
|
@ -291,12 +524,12 @@ func TestService_Remove(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestService_Clear(t *testing.T) {
|
||||
func TestService_Clear_Good(t *testing.T) {
|
||||
t.Run("clears completed processes", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc1, _ := svc.Start(context.Background(), "echo", "1")
|
||||
proc2, _ := svc.Start(context.Background(), "echo", "2")
|
||||
proc1 := startProc(t, svc, context.Background(), "echo", "1")
|
||||
proc2 := startProc(t, svc, context.Background(), "echo", "2")
|
||||
|
||||
<-proc1.Done()
|
||||
<-proc2.Done()
|
||||
|
|
@ -309,22 +542,20 @@ func TestService_Clear(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestService_Kill(t *testing.T) {
|
||||
func TestService_Kill_Good(t *testing.T) {
|
||||
t.Run("kills running process", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc, err := svc.Start(ctx, "sleep", "60")
|
||||
require.NoError(t, err)
|
||||
proc := startProc(t, svc, ctx, "sleep", "60")
|
||||
|
||||
err = svc.Kill(proc.ID)
|
||||
err := svc.Kill(proc.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-proc.Done():
|
||||
// Process killed successfully
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("process should have been killed")
|
||||
}
|
||||
|
|
@ -338,12 +569,11 @@ func TestService_Kill(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestService_Output(t *testing.T) {
|
||||
func TestService_Output_Good(t *testing.T) {
|
||||
t.Run("returns captured output", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "echo", "captured")
|
||||
require.NoError(t, err)
|
||||
proc := startProc(t, svc, context.Background(), "echo", "captured")
|
||||
<-proc.Done()
|
||||
|
||||
output, err := svc.Output(proc.ID)
|
||||
|
|
@ -359,23 +589,21 @@ func TestService_Output(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestService_OnShutdown(t *testing.T) {
|
||||
func TestService_OnShutdown_Good(t *testing.T) {
|
||||
t.Run("kills all running processes", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc1, err := svc.Start(ctx, "sleep", "60")
|
||||
require.NoError(t, err)
|
||||
proc2, err := svc.Start(ctx, "sleep", "60")
|
||||
require.NoError(t, err)
|
||||
proc1 := startProc(t, svc, ctx, "sleep", "60")
|
||||
proc2 := startProc(t, svc, ctx, "sleep", "60")
|
||||
|
||||
assert.True(t, proc1.IsRunning())
|
||||
assert.True(t, proc2.IsRunning())
|
||||
|
||||
err = svc.OnShutdown(context.Background())
|
||||
assert.NoError(t, err)
|
||||
r := svc.OnShutdown(context.Background())
|
||||
assert.True(t, r.OK)
|
||||
|
||||
select {
|
||||
case <-proc1.Done():
|
||||
|
|
@ -390,50 +618,38 @@ func TestService_OnShutdown(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestService_OnStartup(t *testing.T) {
|
||||
t.Run("returns nil", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
err := svc.OnStartup(context.Background())
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_RunWithOptions(t *testing.T) {
|
||||
func TestService_RunWithOptions_Good(t *testing.T) {
|
||||
t.Run("returns output on success", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
output, err := svc.RunWithOptions(context.Background(), RunOptions{
|
||||
r := svc.RunWithOptions(context.Background(), RunOptions{
|
||||
Command: "echo",
|
||||
Args: []string{"opts-test"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, output, "opts-test")
|
||||
assert.True(t, r.OK)
|
||||
assert.Contains(t, r.Value.(string), "opts-test")
|
||||
})
|
||||
|
||||
t.Run("returns error on failure", func(t *testing.T) {
|
||||
t.Run("returns !OK on failure", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
_, err := svc.RunWithOptions(context.Background(), RunOptions{
|
||||
r := svc.RunWithOptions(context.Background(), RunOptions{
|
||||
Command: "sh",
|
||||
Args: []string{"-c", "exit 2"},
|
||||
})
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "exited with code 2")
|
||||
assert.False(t, r.OK)
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_Running(t *testing.T) {
|
||||
func TestService_Running_Good(t *testing.T) {
|
||||
t.Run("returns only running processes", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
proc1, err := svc.Start(ctx, "sleep", "60")
|
||||
require.NoError(t, err)
|
||||
|
||||
proc2, err := svc.Start(context.Background(), "echo", "done")
|
||||
require.NoError(t, err)
|
||||
proc1 := startProc(t, svc, ctx, "sleep", "60")
|
||||
proc2 := startProc(t, svc, context.Background(), "echo", "done")
|
||||
<-proc2.Done()
|
||||
|
||||
running := svc.Running()
|
||||
|
|
|
|||
29
specs/api/RFC.md
Normal file
29
specs/api/RFC.md
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# api
|
||||
**Import:** `dappco.re/go/core/process/pkg/api`
|
||||
**Files:** 2
|
||||
|
||||
## Types
|
||||
|
||||
### `ProcessProvider`
|
||||
`struct`
|
||||
|
||||
Service provider that wraps the go-process daemon registry and bundled UI entrypoint.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
## Functions
|
||||
|
||||
### Package Functions
|
||||
|
||||
- `func NewProvider(registry *process.Registry, hub *ws.Hub) *ProcessProvider`: Returns a `ProcessProvider` for the supplied registry and WebSocket hub. When `registry` is `nil`, it uses `process.DefaultRegistry()`.
|
||||
- `func PIDAlive(pid int) bool`: Returns `false` for non-positive PIDs and otherwise reports whether `os.FindProcess(pid)` followed by signal `0` succeeds.
|
||||
|
||||
### `ProcessProvider` Methods
|
||||
|
||||
- `func (p *ProcessProvider) Name() string`: Returns `"process"`.
|
||||
- `func (p *ProcessProvider) BasePath() string`: Returns `"/api/process"`.
|
||||
- `func (p *ProcessProvider) Element() provider.ElementSpec`: Returns an element spec with tag `core-process-panel` and source `/assets/core-process.js`.
|
||||
- `func (p *ProcessProvider) Channels() []string`: Returns `process.daemon.started`, `process.daemon.stopped`, `process.daemon.health`, `process.started`, `process.output`, `process.exited`, and `process.killed`.
|
||||
- `func (p *ProcessProvider) RegisterRoutes(rg *gin.RouterGroup)`: Registers the daemon list, daemon lookup, daemon stop, and daemon health routes.
|
||||
- `func (p *ProcessProvider) Describe() []api.RouteDescription`: Returns static route descriptions for the registered daemon routes.
|
||||
68
specs/exec/RFC.md
Normal file
68
specs/exec/RFC.md
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
# exec
|
||||
**Import:** `dappco.re/go/core/process/exec`
|
||||
**Files:** 3
|
||||
|
||||
## Types
|
||||
|
||||
### `Options`
|
||||
`struct`
|
||||
|
||||
Command execution options used by `Cmd`.
|
||||
|
||||
Fields:
|
||||
- `Dir string`: Working directory.
|
||||
- `Env []string`: Environment entries appended to `os.Environ()` when non-empty.
|
||||
- `Stdin io.Reader`: Reader assigned to command stdin.
|
||||
- `Stdout io.Writer`: Writer assigned to command stdout.
|
||||
- `Stderr io.Writer`: Writer assigned to command stderr.
|
||||
|
||||
### `Cmd`
|
||||
`struct`
|
||||
|
||||
Wrapped command with chainable configuration methods.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
### `Logger`
|
||||
`interface`
|
||||
|
||||
Command-execution logger.
|
||||
|
||||
Methods:
|
||||
- `Debug(msg string, keyvals ...any)`: Logs a debug-level message.
|
||||
- `Error(msg string, keyvals ...any)`: Logs an error-level message.
|
||||
|
||||
### `NopLogger`
|
||||
`struct`
|
||||
|
||||
No-op `Logger` implementation.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
## Functions
|
||||
|
||||
### Package Functions
|
||||
|
||||
- `func Command(ctx context.Context, name string, args ...string) *Cmd`: Returns a `Cmd` for the supplied context, executable name, and arguments.
|
||||
- `func RunQuiet(ctx context.Context, name string, args ...string) error`: Runs a command with stderr captured into a buffer and returns `core.E("RunQuiet", core.Trim(stderr.String()), err)` on failure.
|
||||
- `func SetDefaultLogger(l Logger)`: Sets the package-level default logger. Passing `nil` replaces it with `NopLogger`.
|
||||
- `func DefaultLogger() Logger`: Returns the package-level default logger.
|
||||
|
||||
### `Cmd` Methods
|
||||
|
||||
- `func (c *Cmd) WithDir(dir string) *Cmd`: Sets `Options.Dir` and returns the same command.
|
||||
- `func (c *Cmd) WithEnv(env []string) *Cmd`: Sets `Options.Env` and returns the same command.
|
||||
- `func (c *Cmd) WithStdin(r io.Reader) *Cmd`: Sets `Options.Stdin` and returns the same command.
|
||||
- `func (c *Cmd) WithStdout(w io.Writer) *Cmd`: Sets `Options.Stdout` and returns the same command.
|
||||
- `func (c *Cmd) WithStderr(w io.Writer) *Cmd`: Sets `Options.Stderr` and returns the same command.
|
||||
- `func (c *Cmd) WithLogger(l Logger) *Cmd`: Sets a command-specific logger and returns the same command.
|
||||
- `func (c *Cmd) Run() error`: Prepares the underlying `exec.Cmd`, logs `"executing command"`, runs it, and wraps failures with `wrapError("Cmd.Run", ...)`.
|
||||
- `func (c *Cmd) Output() ([]byte, error)`: Prepares the underlying `exec.Cmd`, logs `"executing command"`, returns stdout bytes, and wraps failures with `wrapError("Cmd.Output", ...)`.
|
||||
- `func (c *Cmd) CombinedOutput() ([]byte, error)`: Prepares the underlying `exec.Cmd`, logs `"executing command"`, returns combined stdout and stderr, and wraps failures with `wrapError("Cmd.CombinedOutput", ...)`.
|
||||
|
||||
### `NopLogger` Methods
|
||||
|
||||
- `func (NopLogger) Debug(string, ...any)`: Discards the message.
|
||||
- `func (NopLogger) Error(string, ...any)`: Discards the message.
|
||||
207
specs/process-ui.md
Normal file
207
specs/process-ui.md
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
# @core/process-ui
|
||||
**Import:** `@core/process-ui`
|
||||
**Files:** 8
|
||||
|
||||
## Types
|
||||
|
||||
### `DaemonEntry`
|
||||
`interface`
|
||||
|
||||
Daemon-registry row returned by `ProcessApi.listDaemons` and `ProcessApi.getDaemon`.
|
||||
|
||||
Properties:
|
||||
- `code: string`: Application or component code.
|
||||
- `daemon: string`: Daemon name.
|
||||
- `pid: number`: Process ID.
|
||||
- `health?: string`: Optional health-endpoint address.
|
||||
- `project?: string`: Optional project label.
|
||||
- `binary?: string`: Optional binary label.
|
||||
- `started: string`: Start timestamp string from the API.
|
||||
|
||||
### `HealthResult`
|
||||
`interface`
|
||||
|
||||
Result returned by the daemon health endpoint.
|
||||
|
||||
Properties:
|
||||
- `healthy: boolean`: Health outcome.
|
||||
- `address: string`: Health endpoint address that was checked.
|
||||
- `reason?: string`: Optional explanation such as the absence of a health endpoint.
|
||||
|
||||
### `ProcessInfo`
|
||||
`interface`
|
||||
|
||||
Process snapshot shape used by the UI package.
|
||||
|
||||
Properties:
|
||||
- `id: string`: Managed-process identifier.
|
||||
- `command: string`: Executable name.
|
||||
- `args: string[]`: Command arguments.
|
||||
- `dir: string`: Working directory.
|
||||
- `startedAt: string`: Start timestamp string.
|
||||
- `status: 'pending' | 'running' | 'exited' | 'failed' | 'killed'`: Process status string.
|
||||
- `exitCode: number`: Exit code.
|
||||
- `duration: number`: Numeric duration value from the API payload.
|
||||
- `pid: number`: Child PID.
|
||||
|
||||
### `RunResult`
|
||||
`interface`
|
||||
|
||||
Pipeline result row used by `ProcessRunner`.
|
||||
|
||||
Properties:
|
||||
- `name: string`: Spec name.
|
||||
- `exitCode: number`: Exit code.
|
||||
- `duration: number`: Numeric duration value.
|
||||
- `output: string`: Captured output.
|
||||
- `error?: string`: Optional error message.
|
||||
- `skipped: boolean`: Whether the spec was skipped.
|
||||
- `passed: boolean`: Whether the spec passed.
|
||||
|
||||
### `RunAllResult`
|
||||
`interface`
|
||||
|
||||
Aggregate pipeline result consumed by `ProcessRunner`.
|
||||
|
||||
Properties:
|
||||
- `results: RunResult[]`: Per-spec results.
|
||||
- `duration: number`: Aggregate duration.
|
||||
- `passed: number`: Count of passed specs.
|
||||
- `failed: number`: Count of failed specs.
|
||||
- `skipped: number`: Count of skipped specs.
|
||||
- `success: boolean`: Aggregate success flag.
|
||||
|
||||
### `ProcessApi`
|
||||
`class`
|
||||
|
||||
Typed fetch client for `/api/process/*`.
|
||||
|
||||
Public API:
|
||||
- `new ProcessApi(baseUrl?: string)`: Stores an optional URL prefix. The default is `""`.
|
||||
- `listDaemons(): Promise<DaemonEntry[]>`: Fetches `GET /api/process/daemons`.
|
||||
- `getDaemon(code: string, daemon: string): Promise<DaemonEntry>`: Fetches one daemon entry.
|
||||
- `stopDaemon(code: string, daemon: string): Promise<{ stopped: boolean }>`: Sends `POST /api/process/daemons/:code/:daemon/stop`.
|
||||
- `healthCheck(code: string, daemon: string): Promise<HealthResult>`: Fetches `GET /api/process/daemons/:code/:daemon/health`.
|
||||
|
||||
### `ProcessEvent`
|
||||
`interface`
|
||||
|
||||
Event envelope consumed by `connectProcessEvents`.
|
||||
|
||||
Properties:
|
||||
- `type: string`: Event type.
|
||||
- `channel?: string`: Optional channel name.
|
||||
- `data?: any`: Event payload.
|
||||
- `timestamp?: string`: Optional timestamp string.
|
||||
|
||||
### `ProcessPanel`
|
||||
`class`
|
||||
|
||||
Top-level custom element registered as `<core-process-panel>`.
|
||||
|
||||
Public properties:
|
||||
- `apiUrl: string`: Forwarded to child elements through the `api-url` attribute.
|
||||
- `wsUrl: string`: WebSocket endpoint URL from the `ws-url` attribute.
|
||||
|
||||
Behavior:
|
||||
- Renders tabbed daemon, process, and pipeline views.
|
||||
- Opens a process-event WebSocket when `wsUrl` is set.
|
||||
- Shows the last received process channel or event type in the footer.
|
||||
|
||||
### `ProcessDaemons`
|
||||
`class`
|
||||
|
||||
Daemon-list custom element registered as `<core-process-daemons>`.
|
||||
|
||||
Public properties:
|
||||
- `apiUrl: string`: Base URL prefix for `ProcessApi`.
|
||||
|
||||
Behavior:
|
||||
- Loads daemon entries on connect.
|
||||
- Can trigger per-daemon health checks and stop requests.
|
||||
- Emits `daemon-stopped` after a successful stop request.
|
||||
|
||||
### `ProcessList`
|
||||
`class`
|
||||
|
||||
Managed-process list custom element registered as `<core-process-list>`.
|
||||
|
||||
Public properties:
|
||||
- `apiUrl: string`: Declared API prefix property.
|
||||
- `selectedId: string`: Selected process ID, reflected from `selected-id`.
|
||||
|
||||
Behavior:
|
||||
- Emits `process-selected` when a row is chosen.
|
||||
- Currently renders from local state only because the process REST endpoints referenced by the component are not implemented in this package.
|
||||
|
||||
### `ProcessOutput`
|
||||
`class`
|
||||
|
||||
Live output custom element registered as `<core-process-output>`.
|
||||
|
||||
Public properties:
|
||||
- `apiUrl: string`: Declared API prefix property. The current implementation does not use it.
|
||||
- `wsUrl: string`: WebSocket endpoint URL.
|
||||
- `processId: string`: Selected process ID from the `process-id` attribute.
|
||||
|
||||
Behavior:
|
||||
- Connects to the WebSocket when both `wsUrl` and `processId` are present.
|
||||
- Filters for `process.output` events whose payload `data.id` matches `processId`.
|
||||
- Appends output lines and auto-scrolls by default.
|
||||
|
||||
### `ProcessRunner`
|
||||
`class`
|
||||
|
||||
Pipeline-results custom element registered as `<core-process-runner>`.
|
||||
|
||||
Public properties:
|
||||
- `apiUrl: string`: Declared API prefix property.
|
||||
- `result: RunAllResult | null`: Aggregate pipeline result used for rendering.
|
||||
|
||||
Behavior:
|
||||
- Renders summary counts plus expandable per-spec output.
|
||||
- Depends on the `result` property today because pipeline REST endpoints are not implemented in the package.
|
||||
|
||||
## Functions
|
||||
|
||||
### Package Functions
|
||||
|
||||
- `function connectProcessEvents(wsUrl: string, handler: (event: ProcessEvent) => void): WebSocket`: Opens a WebSocket, parses incoming JSON, forwards only messages whose `type` or `channel` starts with `process.`, ignores malformed payloads, and returns the `WebSocket` instance.
|
||||
|
||||
### `ProcessPanel` Methods
|
||||
|
||||
- `connectedCallback(): void`: Calls the LitElement base implementation and opens the WebSocket when `wsUrl` is set.
|
||||
- `disconnectedCallback(): void`: Calls the LitElement base implementation and closes the current WebSocket.
|
||||
- `render(): unknown`: Renders the header, tab strip, active child element, and connection footer.
|
||||
|
||||
### `ProcessDaemons` Methods
|
||||
|
||||
- `connectedCallback(): void`: Instantiates `ProcessApi` and loads daemon data.
|
||||
- `loadDaemons(): Promise<void>`: Fetches daemon entries, stores them in component state, and records any request error message.
|
||||
- `render(): unknown`: Renders the daemon list, loading state, empty state, and action buttons.
|
||||
|
||||
### `ProcessList` Methods
|
||||
|
||||
- `connectedCallback(): void`: Calls the LitElement base implementation and invokes `loadProcesses`.
|
||||
- `loadProcesses(): Promise<void>`: Current placeholder implementation that clears state because the referenced process REST endpoints are not implemented yet.
|
||||
- `render(): unknown`: Renders the process list or an informational empty state explaining the missing REST support.
|
||||
|
||||
### `ProcessOutput` Methods
|
||||
|
||||
- `connectedCallback(): void`: Calls the LitElement base implementation and opens the WebSocket when `wsUrl` and `processId` are both set.
|
||||
- `disconnectedCallback(): void`: Calls the LitElement base implementation and closes the current WebSocket.
|
||||
- `updated(changed: Map<string, unknown>): void`: Reconnects when `processId` or `wsUrl` changes, resets buffered lines on reconnection, and auto-scrolls when enabled.
|
||||
- `render(): unknown`: Renders the output panel, waiting state, and accumulated stdout or stderr lines.
|
||||
|
||||
### `ProcessRunner` Methods
|
||||
|
||||
- `connectedCallback(): void`: Calls the LitElement base implementation and invokes `loadResults`.
|
||||
- `loadResults(): Promise<void>`: Current placeholder method. The implementation is empty because pipeline endpoints are not present.
|
||||
- `render(): unknown`: Renders the empty-state notice when `result` is absent, or the aggregate summary plus per-spec details when `result` is present.
|
||||
|
||||
### `ProcessApi` Methods
|
||||
|
||||
- `listDaemons(): Promise<DaemonEntry[]>`: Returns the `data` field from a successful daemon-list response.
|
||||
- `getDaemon(code: string, daemon: string): Promise<DaemonEntry>`: Returns one daemon entry from the provider API.
|
||||
- `stopDaemon(code: string, daemon: string): Promise<{ stopped: boolean }>`: Issues the stop request and returns the provider's `{ stopped }` payload.
|
||||
- `healthCheck(code: string, daemon: string): Promise<HealthResult>`: Returns the daemon-health payload.
|
||||
372
specs/process.md
Normal file
372
specs/process.md
Normal file
|
|
@ -0,0 +1,372 @@
|
|||
# process
|
||||
**Import:** `dappco.re/go/core/process`
|
||||
**Files:** 11
|
||||
|
||||
## Types
|
||||
|
||||
### `ActionProcessStarted`
|
||||
`struct`
|
||||
|
||||
Broadcast payload for a managed process that has successfully started.
|
||||
|
||||
Fields:
|
||||
- `ID string`: Generated managed-process identifier.
|
||||
- `Command string`: Executable name passed to the service.
|
||||
- `Args []string`: Argument vector used to start the process.
|
||||
- `Dir string`: Working directory supplied at start time.
|
||||
- `PID int`: OS process ID of the child process.
|
||||
|
||||
### `ActionProcessOutput`
|
||||
`struct`
|
||||
|
||||
Broadcast payload for one scanned line of process output.
|
||||
|
||||
Fields:
|
||||
- `ID string`: Managed-process identifier.
|
||||
- `Line string`: One line from stdout or stderr, without the trailing newline.
|
||||
- `Stream Stream`: Output source, using `StreamStdout` or `StreamStderr`.
|
||||
|
||||
### `ActionProcessExited`
|
||||
`struct`
|
||||
|
||||
Broadcast payload emitted after the service wait goroutine finishes.
|
||||
|
||||
Fields:
|
||||
- `ID string`: Managed-process identifier.
|
||||
- `ExitCode int`: Process exit code.
|
||||
- `Duration time.Duration`: Time elapsed since `StartedAt`.
|
||||
- `Error error`: Declared error slot for exit metadata. The current `Service` emitter does not populate it.
|
||||
|
||||
### `ActionProcessKilled`
|
||||
`struct`
|
||||
|
||||
Broadcast payload emitted by `Service.Kill`.
|
||||
|
||||
Fields:
|
||||
- `ID string`: Managed-process identifier.
|
||||
- `Signal string`: Signal name reported by the service. The current implementation emits `"SIGKILL"`.
|
||||
|
||||
### `RingBuffer`
|
||||
`struct`
|
||||
|
||||
Fixed-size circular byte buffer used for captured process output. The implementation is mutex-protected and overwrites the oldest bytes when full.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
### `DaemonOptions`
|
||||
`struct`
|
||||
|
||||
Configuration for `NewDaemon`.
|
||||
|
||||
Fields:
|
||||
- `PIDFile string`: PID file path. Empty disables PID-file management.
|
||||
- `ShutdownTimeout time.Duration`: Grace period used by `Stop`. Zero is normalized to 30 seconds by `NewDaemon`.
|
||||
- `HealthAddr string`: Listen address for the health server. Empty disables health endpoints.
|
||||
- `HealthChecks []HealthCheck`: Additional `/health` checks to register on the health server.
|
||||
- `Registry *Registry`: Optional daemon registry used for automatic register/unregister.
|
||||
- `RegistryEntry DaemonEntry`: Base registry payload. `Start` fills in `PID`, `Health`, and `Started` behavior through `Registry.Register`.
|
||||
|
||||
### `Daemon`
|
||||
`struct`
|
||||
|
||||
Lifecycle wrapper around a PID file, optional health server, and optional registry entry.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
### `HealthCheck`
|
||||
`type HealthCheck func() error`
|
||||
|
||||
Named function type used by `HealthServer` and `DaemonOptions`. Returning `nil` marks the check healthy; returning an error makes `/health` respond with `503`.
|
||||
|
||||
### `HealthServer`
|
||||
`struct`
|
||||
|
||||
HTTP server exposing `/health` and `/ready` endpoints.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
### `PIDFile`
|
||||
`struct`
|
||||
|
||||
Single-instance guard backed by a PID file on disk.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
### `ManagedProcess`
|
||||
`struct`
|
||||
|
||||
Service-owned process record for a started child process.
|
||||
|
||||
Fields:
|
||||
- `ID string`: Managed-process identifier generated by `core.ID()`.
|
||||
- `Command string`: Executable name.
|
||||
- `Args []string`: Command arguments.
|
||||
- `Dir string`: Working directory used when starting the process.
|
||||
- `Env []string`: Extra environment entries appended to the command environment.
|
||||
- `StartedAt time.Time`: Timestamp recorded immediately before `cmd.Start`.
|
||||
- `Status Status`: Current lifecycle state tracked by the service.
|
||||
- `ExitCode int`: Exit status after completion.
|
||||
- `Duration time.Duration`: Runtime duration set when the wait goroutine finishes.
|
||||
|
||||
### `Process`
|
||||
`type alias of ManagedProcess`
|
||||
|
||||
Compatibility alias that exposes the same fields and methods as `ManagedProcess`.
|
||||
|
||||
### `Program`
|
||||
`struct`
|
||||
|
||||
Thin helper for finding and running a named executable.
|
||||
|
||||
Fields:
|
||||
- `Name string`: Binary name to look up or execute.
|
||||
- `Path string`: Resolved absolute path populated by `Find`. When empty, `Run` and `RunDir` fall back to `Name`.
|
||||
|
||||
### `DaemonEntry`
|
||||
`struct`
|
||||
|
||||
Serialized daemon-registry record written as JSON.
|
||||
|
||||
Fields:
|
||||
- `Code string`: Application or component code.
|
||||
- `Daemon string`: Daemon name within that code.
|
||||
- `PID int`: Running process ID.
|
||||
- `Health string`: Health endpoint address, if any.
|
||||
- `Project string`: Optional project label.
|
||||
- `Binary string`: Optional binary label.
|
||||
- `Started time.Time`: Start timestamp persisted in RFC3339Nano format.
|
||||
|
||||
### `Registry`
|
||||
`struct`
|
||||
|
||||
Filesystem-backed daemon registry that stores one JSON file per daemon entry.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
### `Runner`
|
||||
`struct`
|
||||
|
||||
Pipeline orchestrator that starts `RunSpec` processes through a `Service`.
|
||||
|
||||
Exported fields:
|
||||
- None.
|
||||
|
||||
### `RunSpec`
|
||||
`struct`
|
||||
|
||||
One process specification for `Runner`.
|
||||
|
||||
Fields:
|
||||
- `Name string`: Friendly name used for dependencies and result reporting.
|
||||
- `Command string`: Executable name.
|
||||
- `Args []string`: Command arguments.
|
||||
- `Dir string`: Working directory.
|
||||
- `Env []string`: Additional environment variables.
|
||||
- `After []string`: Dependency names that must complete before this spec can run in `RunAll`.
|
||||
- `AllowFailure bool`: When true, downstream work is not skipped because of this spec's failure.
|
||||
|
||||
### `RunResult`
|
||||
`struct`
|
||||
|
||||
Per-spec runner result.
|
||||
|
||||
Fields:
|
||||
- `Name string`: Spec name.
|
||||
- `Spec RunSpec`: Original spec payload.
|
||||
- `ExitCode int`: Exit code from the managed process.
|
||||
- `Duration time.Duration`: Process duration or start-attempt duration.
|
||||
- `Output string`: Captured output returned from the managed process.
|
||||
- `Error error`: Start or orchestration error. For a started process that exits non-zero, this remains `nil`.
|
||||
- `Skipped bool`: Whether the spec was skipped instead of run.
|
||||
|
||||
### `RunAllResult`
|
||||
`struct`
|
||||
|
||||
Aggregate result returned by `RunAll`, `RunSequential`, and `RunParallel`.
|
||||
|
||||
Fields:
|
||||
- `Results []RunResult`: Collected per-spec results.
|
||||
- `Duration time.Duration`: End-to-end runtime for the orchestration method.
|
||||
- `Passed int`: Count of results where `Passed()` is true.
|
||||
- `Failed int`: Count of non-skipped results that did not pass.
|
||||
- `Skipped int`: Count of skipped results.
|
||||
|
||||
### `Service`
|
||||
`struct`
|
||||
|
||||
Core service that owns managed processes and registers action handlers.
|
||||
|
||||
Fields:
|
||||
- `*core.ServiceRuntime[Options]`: Embedded Core runtime used for lifecycle hooks and access to `Core()`.
|
||||
|
||||
### `Options`
|
||||
`struct`
|
||||
|
||||
Service configuration.
|
||||
|
||||
Fields:
|
||||
- `BufferSize int`: Ring-buffer capacity for captured output. `Register` currently initializes this from `DefaultBufferSize`.
|
||||
|
||||
### `Status`
|
||||
`type Status string`
|
||||
|
||||
Named lifecycle-state type for a managed process.
|
||||
|
||||
Exported values:
|
||||
- `StatusPending`: queued but not started.
|
||||
- `StatusRunning`: currently executing.
|
||||
- `StatusExited`: completed and waited.
|
||||
- `StatusFailed`: start or wait failure state.
|
||||
- `StatusKilled`: terminated by signal.
|
||||
|
||||
### `Stream`
|
||||
`type Stream string`
|
||||
|
||||
Named output-stream discriminator for process output events.
|
||||
|
||||
Exported values:
|
||||
- `StreamStdout`: stdout line.
|
||||
- `StreamStderr`: stderr line.
|
||||
|
||||
### `RunOptions`
|
||||
`struct`
|
||||
|
||||
Execution settings accepted by `Service.StartWithOptions` and `Service.RunWithOptions`.
|
||||
|
||||
Fields:
|
||||
- `Command string`: Executable name. Required by both start and run paths.
|
||||
- `Args []string`: Command arguments.
|
||||
- `Dir string`: Working directory.
|
||||
- `Env []string`: Additional environment entries appended to the command environment.
|
||||
- `DisableCapture bool`: Disables the managed-process ring buffer when true.
|
||||
- `Detach bool`: Starts the child in a separate process group and replaces the parent context with `context.Background()`.
|
||||
- `Timeout time.Duration`: Optional watchdog timeout that calls `Shutdown` after the duration elapses.
|
||||
- `GracePeriod time.Duration`: Delay between `SIGTERM` and fallback kill in `Shutdown`.
|
||||
- `KillGroup bool`: Requests process-group termination. The current service only enables this when `Detach` is also true.
|
||||
|
||||
### `ProcessInfo`
|
||||
`struct`
|
||||
|
||||
Serializable snapshot returned by `ManagedProcess.Info` and `Service` action lookups.
|
||||
|
||||
Fields:
|
||||
- `ID string`: Managed-process identifier.
|
||||
- `Command string`: Executable name.
|
||||
- `Args []string`: Command arguments.
|
||||
- `Dir string`: Working directory.
|
||||
- `StartedAt time.Time`: Start timestamp.
|
||||
- `Running bool`: Convenience boolean derived from `Status`.
|
||||
- `Status Status`: Current lifecycle state.
|
||||
- `ExitCode int`: Exit status.
|
||||
- `Duration time.Duration`: Runtime duration.
|
||||
- `PID int`: Child PID, or `0` if no process handle is available.
|
||||
|
||||
### `Info`
|
||||
`type alias of ProcessInfo`
|
||||
|
||||
Compatibility alias that exposes the same fields as `ProcessInfo`.
|
||||
|
||||
## Functions
|
||||
|
||||
### Package Functions
|
||||
|
||||
- `func Register(c *core.Core) core.Result`: Builds a `Service` with a fresh `core.Registry[*ManagedProcess]`, sets the buffer size to `DefaultBufferSize`, and returns the service in `Result.Value`.
|
||||
- `func NewRingBuffer(size int) *RingBuffer`: Allocates a fixed-capacity ring buffer of exactly `size` bytes.
|
||||
- `func NewDaemon(opts DaemonOptions) *Daemon`: Normalizes `ShutdownTimeout`, creates optional `PIDFile` and `HealthServer` helpers, and attaches any configured health checks.
|
||||
- `func NewHealthServer(addr string) *HealthServer`: Returns a health server with the supplied listen address and readiness initialized to `true`.
|
||||
- `func WaitForHealth(addr string, timeoutMs int) bool`: Polls `http://<addr>/health` every 200 ms until it gets HTTP 200 or the timeout expires.
|
||||
- `func NewPIDFile(path string) *PIDFile`: Returns a PID-file manager for `path`.
|
||||
- `func ReadPID(path string) (int, bool)`: Reads and parses a PID file, then uses signal `0` to report whether that PID is still alive.
|
||||
- `func NewRegistry(dir string) *Registry`: Returns a daemon registry rooted at `dir`.
|
||||
- `func DefaultRegistry() *Registry`: Returns a registry at `~/.core/daemons`, falling back to the OS temp directory if the home directory cannot be resolved.
|
||||
- `func NewRunner(svc *Service) *Runner`: Returns a runner bound to a specific `Service`.
|
||||
|
||||
### `RingBuffer` Methods
|
||||
|
||||
- `func (rb *RingBuffer) Write(p []byte) (n int, err error)`: Appends bytes one by one, advancing the circular window and overwriting the oldest bytes when capacity is exceeded.
|
||||
- `func (rb *RingBuffer) String() string`: Returns the current buffer contents in logical order as a string.
|
||||
- `func (rb *RingBuffer) Bytes() []byte`: Returns a copied byte slice of the current logical contents, or `nil` when the buffer is empty.
|
||||
- `func (rb *RingBuffer) Len() int`: Returns the number of bytes currently retained.
|
||||
- `func (rb *RingBuffer) Cap() int`: Returns the configured capacity.
|
||||
- `func (rb *RingBuffer) Reset()`: Clears the buffer indexes and full flag.
|
||||
|
||||
### `Daemon` Methods
|
||||
|
||||
- `func (d *Daemon) Start() error`: Acquires the PID file, starts the health server, marks the daemon running, and auto-registers it when `Registry` is configured. If a later step fails, it rolls back earlier setup.
|
||||
- `func (d *Daemon) Run(ctx context.Context) error`: Requires a started daemon, waits for `ctx.Done()`, and then calls `Stop`.
|
||||
- `func (d *Daemon) Stop() error`: Sets readiness false, shuts down the health server, releases the PID file, unregisters the daemon, and joins health or PID teardown errors with `core.ErrorJoin`.
|
||||
- `func (d *Daemon) SetReady(ready bool)`: Forwards readiness changes to the health server when one exists.
|
||||
- `func (d *Daemon) HealthAddr() string`: Returns the bound health-server address or `""` when health endpoints are disabled.
|
||||
|
||||
### `HealthServer` Methods
|
||||
|
||||
- `func (h *HealthServer) AddCheck(check HealthCheck)`: Appends a health-check callback under lock.
|
||||
- `func (h *HealthServer) SetReady(ready bool)`: Updates the readiness flag used by `/ready`.
|
||||
- `func (h *HealthServer) Start() error`: Installs `/health` and `/ready` handlers, listens on `addr`, stores the listener and `http.Server`, and serves in a goroutine.
|
||||
- `func (h *HealthServer) Stop(ctx context.Context) error`: Calls `Shutdown` on the underlying `http.Server` when started; otherwise returns `nil`.
|
||||
- `func (h *HealthServer) Addr() string`: Returns the actual bound listener address after `Start`, or the configured address before startup.
|
||||
|
||||
### `PIDFile` Methods
|
||||
|
||||
- `func (p *PIDFile) Acquire() error`: Rejects a live existing PID file, deletes stale state, creates the parent directory when needed, and writes the current process ID.
|
||||
- `func (p *PIDFile) Release() error`: Deletes the PID file.
|
||||
- `func (p *PIDFile) Path() string`: Returns the configured PID-file path.
|
||||
|
||||
### `ManagedProcess` Methods
|
||||
|
||||
- `func (p *ManagedProcess) Info() ProcessInfo`: Returns a snapshot containing public fields plus the current child PID.
|
||||
- `func (p *ManagedProcess) Output() string`: Returns captured output as a string, or `""` when capture is disabled.
|
||||
- `func (p *ManagedProcess) OutputBytes() []byte`: Returns captured output as bytes, or `nil` when capture is disabled.
|
||||
- `func (p *ManagedProcess) IsRunning() bool`: Reports running state by checking whether the `done` channel has closed.
|
||||
- `func (p *ManagedProcess) Wait() error`: Blocks for completion and then returns a wrapped error for failed-start, killed, or non-zero-exit outcomes.
|
||||
- `func (p *ManagedProcess) Done() <-chan struct{}`: Returns the completion channel.
|
||||
- `func (p *ManagedProcess) Kill() error`: Sends `SIGKILL` to the child, or to the entire process group when group killing is enabled.
|
||||
- `func (p *ManagedProcess) Shutdown() error`: Sends `SIGTERM`, waits for the configured grace period, and falls back to `Kill`. With no grace period configured, it immediately calls `Kill`.
|
||||
- `func (p *ManagedProcess) SendInput(input string) error`: Writes to the child's stdin pipe while the process is running.
|
||||
- `func (p *ManagedProcess) CloseStdin() error`: Closes the stdin pipe and clears the stored handle.
|
||||
- `func (p *ManagedProcess) Signal(sig os.Signal) error`: Sends an arbitrary signal while the process is in `StatusRunning`.
|
||||
|
||||
### `Program` Methods
|
||||
|
||||
- `func (p *Program) Find() error`: Resolves `Name` through `exec.LookPath`, stores the absolute path in `Path`, and wraps `ErrProgramNotFound` when lookup fails.
|
||||
- `func (p *Program) Run(ctx context.Context, args ...string) (string, error)`: Executes the program in the current working directory by delegating to `RunDir("", args...)`.
|
||||
- `func (p *Program) RunDir(ctx context.Context, dir string, args ...string) (string, error)`: Runs the program with combined stdout/stderr capture, trims the combined output, and returns that output even when the command fails.
|
||||
|
||||
### `Registry` Methods
|
||||
|
||||
- `func (r *Registry) Register(entry DaemonEntry) error`: Ensures the registry directory exists, defaults `Started` when zero, marshals the entry with the package's JSON writer, and writes one `<code>-<daemon>.json` file.
|
||||
- `func (r *Registry) Unregister(code, daemon string) error`: Deletes the registry file for the supplied daemon key.
|
||||
- `func (r *Registry) Get(code, daemon string) (*DaemonEntry, bool)`: Reads one entry, prunes invalid or stale files, and returns `(nil, false)` when the daemon is missing or dead.
|
||||
- `func (r *Registry) List() ([]DaemonEntry, error)`: Lists all JSON files in the registry directory, prunes invalid or stale entries, and returns only live daemons. A missing registry directory returns `nil, nil`.
|
||||
|
||||
### `RunResult` and `RunAllResult` Methods
|
||||
|
||||
- `func (r RunResult) Passed() bool`: Returns true only when the result is not skipped, has no `Error`, and has `ExitCode == 0`.
|
||||
- `func (r RunAllResult) Success() bool`: Returns true when `Failed == 0`, regardless of skipped count.
|
||||
|
||||
### `Runner` Methods
|
||||
|
||||
- `func (r *Runner) RunAll(ctx context.Context, specs []RunSpec) (*RunAllResult, error)`: Executes dependency-aware waves of specs, skips dependents after failing required dependencies, and marks circular or missing dependency sets as failed results with `ExitCode` 1.
|
||||
- `func (r *Runner) RunSequential(ctx context.Context, specs []RunSpec) (*RunAllResult, error)`: Runs specs in order and marks remaining specs skipped after the first disallowed failure.
|
||||
- `func (r *Runner) RunParallel(ctx context.Context, specs []RunSpec) (*RunAllResult, error)`: Runs all specs concurrently and aggregates counts after all goroutines finish.
|
||||
|
||||
### `Service` Methods
|
||||
|
||||
- `func (s *Service) OnStartup(ctx context.Context) core.Result`: Registers the Core actions `process.run`, `process.start`, `process.kill`, `process.list`, and `process.get`.
|
||||
- `func (s *Service) OnShutdown(ctx context.Context) core.Result`: Iterates all managed processes and calls `Kill` on each one.
|
||||
- `func (s *Service) Start(ctx context.Context, command string, args ...string) core.Result`: Convenience wrapper that builds `RunOptions` and delegates to `StartWithOptions`.
|
||||
- `func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) core.Result`: Starts a managed process, configures pipes, optional capture, detach and timeout behavior, stores it in the registry, emits `ActionProcessStarted`, streams stdout/stderr lines, and emits `ActionProcessExited` after completion.
|
||||
- `func (s *Service) Get(id string) (*ManagedProcess, error)`: Returns one managed process or `ErrProcessNotFound`.
|
||||
- `func (s *Service) List() []*ManagedProcess`: Returns all managed processes currently stored in the service registry.
|
||||
- `func (s *Service) Running() []*ManagedProcess`: Returns only processes whose `done` channel has not closed yet.
|
||||
- `func (s *Service) Kill(id string) error`: Kills the managed process by ID and emits `ActionProcessKilled`.
|
||||
- `func (s *Service) Remove(id string) error`: Deletes a completed process from the registry and rejects removal while it is still running.
|
||||
- `func (s *Service) Clear()`: Deletes every completed process from the registry.
|
||||
- `func (s *Service) Output(id string) (string, error)`: Returns the managed process's captured output.
|
||||
- `func (s *Service) Run(ctx context.Context, command string, args ...string) core.Result`: Convenience wrapper around `RunWithOptions`.
|
||||
- `func (s *Service) RunWithOptions(ctx context.Context, opts RunOptions) core.Result`: Executes an unmanaged one-shot command with `CombinedOutput`. On success it returns the output string in `Value`; on failure it returns a wrapped error in `Value` and sets `OK` false.
|
||||
26
types.go
26
types.go
|
|
@ -5,30 +5,24 @@
|
|||
//
|
||||
// # Getting Started
|
||||
//
|
||||
// // Register with Core
|
||||
// core, _ := framework.New(
|
||||
// framework.WithName("process", process.NewService(process.Options{})),
|
||||
// )
|
||||
// c := core.New(core.WithService(process.Register))
|
||||
// _ = c.ServiceStartup(ctx, nil)
|
||||
//
|
||||
// // Get service and run a process
|
||||
// svc, err := framework.ServiceFor[*process.Service](core, "process")
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// proc, err := svc.Start(ctx, "go", "test", "./...")
|
||||
// r := c.Process().Run(ctx, "go", "test", "./...")
|
||||
// output := r.Value.(string)
|
||||
//
|
||||
// # Listening for Events
|
||||
//
|
||||
// Process events are broadcast via Core.ACTION:
|
||||
//
|
||||
// core.RegisterAction(func(c *framework.Core, msg framework.Message) error {
|
||||
// c.RegisterAction(func(c *core.Core, msg core.Message) core.Result {
|
||||
// switch m := msg.(type) {
|
||||
// case process.ActionProcessOutput:
|
||||
// fmt.Print(m.Line)
|
||||
// case process.ActionProcessExited:
|
||||
// fmt.Printf("Exit code: %d\n", m.ExitCode)
|
||||
// }
|
||||
// return nil
|
||||
// return core.Result{OK: true}
|
||||
// })
|
||||
package process
|
||||
|
||||
|
|
@ -91,15 +85,19 @@ type RunOptions struct {
|
|||
KillGroup bool
|
||||
}
|
||||
|
||||
// Info provides a snapshot of process state without internal fields.
|
||||
type Info struct {
|
||||
// ProcessInfo provides a snapshot of process state without internal fields.
|
||||
type ProcessInfo struct {
|
||||
ID string `json:"id"`
|
||||
Command string `json:"command"`
|
||||
Args []string `json:"args"`
|
||||
Dir string `json:"dir"`
|
||||
StartedAt time.Time `json:"startedAt"`
|
||||
Running bool `json:"running"`
|
||||
Status Status `json:"status"`
|
||||
ExitCode int `json:"exitCode"`
|
||||
Duration time.Duration `json:"duration"`
|
||||
PID int `json:"pid"`
|
||||
}
|
||||
|
||||
// Info is kept as a compatibility alias for ProcessInfo.
|
||||
type Info = ProcessInfo
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue