2026-03-06 12:50:09 +00:00
|
|
|
package process
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"sync"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
2026-03-21 20:00:57 +00:00
|
|
|
"dappco.re/go/core"
|
2026-03-21 23:49:08 +00:00
|
|
|
coreerr "dappco.re/go/core/log"
|
2026-03-06 12:50:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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().
|
2026-03-09 08:26:00 +00:00
|
|
|
func SetDefault(s *Service) error {
|
2026-03-06 12:50:09 +00:00
|
|
|
if s == nil {
|
fix(dx): audit CLAUDE.md, error handling, and test coverage
- Update CLAUDE.md: document Detach, DisableCapture, ShutdownTimeout,
auto-registration, graceful shutdown, and error handling conventions;
add missing go-log and go-io dependencies
- Replace ServiceError type in process_global.go with coreerr.E()
sentinel errors for consistency with the rest of the package
- Wrap raw error returns in Registry.Register, Registry.Unregister,
and PIDFile.Release with coreerr.E() for proper context
- Add tests for Service.Kill, Service.Output, Service.OnShutdown,
Service.OnStartup, Service.RunWithOptions, Service.Running,
Process.Signal, Daemon.Run (context cancellation),
Daemon.Stop (idempotent), DisableCapture, Detach, env vars,
exec.WithDir, exec.WithEnv, exec.WithStdin/Stdout/Stderr,
exec.RunQuiet
- Coverage: root 82.7% → 88.3%, exec/ 61.9% → 87.3%
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-17 08:42:56 +00:00
|
|
|
return ErrSetDefaultNil
|
2026-03-06 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
defaultService.Store(s)
|
2026-03-09 08:26:00 +00:00
|
|
|
return nil
|
2026-03-06 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Init initializes the default global service with a Core instance.
|
|
|
|
|
// This is typically called during application startup.
|
2026-03-06 14:10:56 +00:00
|
|
|
func Init(c *core.Core) error {
|
2026-03-06 12:50:09 +00:00
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
fix(dx): audit CLAUDE.md, error handling, and test coverage
- Update CLAUDE.md: document Detach, DisableCapture, ShutdownTimeout,
auto-registration, graceful shutdown, and error handling conventions;
add missing go-log and go-io dependencies
- Replace ServiceError type in process_global.go with coreerr.E()
sentinel errors for consistency with the rest of the package
- Wrap raw error returns in Registry.Register, Registry.Unregister,
and PIDFile.Release with coreerr.E() for proper context
- Add tests for Service.Kill, Service.Output, Service.OnShutdown,
Service.OnStartup, Service.RunWithOptions, Service.Running,
Process.Signal, Daemon.Run (context cancellation),
Daemon.Stop (idempotent), DisableCapture, Detach, env vars,
exec.WithDir, exec.WithEnv, exec.WithStdin/Stdout/Stderr,
exec.RunQuiet
- Coverage: root 82.7% → 88.3%, exec/ 61.9% → 87.3%
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-17 08:42:56 +00:00
|
|
|
// 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)
|
|
|
|
|
)
|