binary

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

doc.go Package binary provides lifecycle management for executable subprocesses.

Binary wraps os/exec with structured logging, port readiness checks, graceful shutdown via SIGTERM, and asynchronous exit notification.

A binary can be run synchronously or asynchronously:

// Synchronous: blocks until the process exits.
err := binary.MustNew("migrate", "--up").Run()

// Asynchronous: returns once the process is running and ready.
server := binary.MustNew("myserver").WithPort(8080)
server.OnExit(func(err error) { log.Fatal(err) })
if err := server.Start(); err != nil { ... }
defer server.Stop()

Worker orchestrates multiple binaries, coordinating startup, error propagation, and shutdown:

w := binary.NewWorker("myapp", binaries)
go func() { <-sigChan; w.Stop() }()
if err := w.Run(); err != nil { ... }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Binary

type Binary struct {
	// contains filtered or unexported fields
}

Binary represents an executable subprocess with lifecycle management.

A Binary must be created with New or MustNew, configured via builder methods, and then executed with either Binary.Run (synchronous) or [Binary.Start] (asynchronous). Only one of Run/Start may be called per Binary instance.

func MustNew

func MustNew(binaryPath string, args ...string) *Binary

MustNew is like New but panics on error.

func New

func New(binaryPath string, args ...string) (*Binary, error)

New returns a new Binary for the given executable path and arguments. The path is resolved against PATH if it contains no separator, or against the current working directory if it is relative.

func (*Binary) AsJob

func (b *Binary) AsJob() *Binary

AsJob flags this binary as a finite task rather than a long-running service. This is used by Worker to determine expected lifecycle behavior: a job is expected to exit on its own, while a service exiting is treated as an error.

func (*Binary) IsJob

func (b *Binary) IsJob() bool

IsJob reports whether this binary has been flagged as a job via Binary.AsJob.

func (*Binary) Name

func (b *Binary) Name() string

Name returns this binary's display name.

func (*Binary) OnExit

func (b *Binary) OnExit(callback func(error)) *Binary

OnExit registers a callback invoked when the process exits after [Binary.Start]. The error is nil if the process exited with status 0 or was stopped via Binary.Stop. Has no effect with Binary.Run, where the exit error is returned directly.

func (*Binary) Run

func (b *Binary) Run() error

Run starts the process and blocks until it exits. Returns nil if the process exits with status 0, or an error otherwise. For background execution, use [Binary.Start] instead.

func (*Binary) RunAsync

func (b *Binary) RunAsync() error

RunAsync starts the process asynchronously. It returns an error if the process fails to launch or, when a port is configured via Binary.WithPort, if the port does not accept connections within the timeout. The Binary.OnExit callback is invoked when the process later exits. Use Binary.Stop for graceful shutdown.

func (*Binary) Stop

func (b *Binary) Stop()

Stop sends SIGTERM to the process and waits for it to exit. The Binary.OnExit callback receives a nil error. Stop is safe to call multiple times or on a binary that was never started.

func (*Binary) WithEnv

func (b *Binary) WithEnv(key, value string) *Binary

WithEnv adds an environment variable to this binary's process.

func (*Binary) WithLogger

func (b *Binary) WithLogger(logger *slog.Logger) *Binary

WithLogger sets the logger for this binary's stdout and stderr output.

func (*Binary) WithName

func (b *Binary) WithName(name string) *Binary

WithName overrides this binary's display name used in log output.

func (*Binary) WithPort

func (b *Binary) WithPort(port int) *Binary

WithPort sets a TCP port that this binary is expected to open. When using [Binary.Start], the call blocks until the port accepts connections or a timeout is reached. Has no effect with Binary.Run.

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

Worker orchestrates the lifecycle of a group of Binary instances, handling coordinated startup, error propagation, and shutdown.

In parallel mode (Worker.Run or [Worker.Start]), all binaries are started concurrently. In sequential mode (Worker.RunSequentially), binaries are started in order, blocking on each job before proceeding.

If any binary exits unexpectedly or with an error, the worker shuts down all remaining binaries and reports the error. Call Worker.Stop from another goroutine to initiate a graceful shutdown.

func NewWorker

func NewWorker(name string, binaries []*Binary) *Worker

NewWorker returns a new Worker managing the given binaries.

func (*Worker) OnExit

func (w *Worker) OnExit(callback func(error)) *Worker

OnExit registers a callback invoked when the worker finishes after [Worker.Start]. The error is nil if all binaries exited cleanly or Worker.Stop was called. Has no effect with Worker.Run or Worker.RunSequentially, where the error is returned directly.

func (*Worker) Run

func (w *Worker) Run() error

Run starts all binaries concurrently and blocks until one of:

  • All job binaries complete successfully (services are then stopped).
  • Any binary exits unexpectedly or with an error.
  • Worker.Stop is called.

Returns the first error encountered, or nil on clean shutdown.

func (*Worker) RunAsync

func (w *Worker) RunAsync() error

RunAsync starts all binaries concurrently and returns once every binary has launched successfully (and any port checks have passed). If any binary fails to start, already-started binaries are shut down and the error is returned. After Start returns nil, the worker runs in the background; the Worker.OnExit callback is invoked when the worker finishes. Use Worker.Stop for graceful shutdown.

func (*Worker) RunSequentially

func (w *Worker) RunSequentially() error

RunSequentially starts binaries in declaration order. Job binaries are run synchronously via Binary.Run; service binaries are started in the background via [Binary.Start]. After all binaries have been processed:

  • If no services are running, returns immediately.
  • Otherwise, blocks until a service fails or Worker.Stop is called.

Returns the first error encountered, or nil on clean shutdown.

func (*Worker) Stop

func (w *Worker) Stop()

Stop signals the worker to shut down gracefully. The actual shutdown completes asynchronously when Worker.Run or Worker.RunSequentially returns, or when the Worker.OnExit callback fires after [Worker.Start]. Safe to call from any goroutine and multiple times.

func (*Worker) WithEnv

func (w *Worker) WithEnv(keyToValue map[string]string) *Worker

WithEnv adds environment variables to every managed binary.

func (*Worker) WithLogger

func (w *Worker) WithLogger(logger *slog.Logger) *Worker

WithLogger sets the logger for the worker and all managed binaries.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL