Documentation
¶
Overview ¶
Package startStop provides a thread-safe runner for managing service lifecycle with start and stop operations. It handles context cancellation, error tracking, and uptime monitoring for long-running services.
The runner executes start/stop functions asynchronously and tracks their execution state, errors, and uptime. All operations are thread-safe and can be called concurrently.
Example usage:
runner := startStop.New(
func(ctx context.Context) error {
// Start your service
return server.ListenAndServe()
},
func(ctx context.Context) error {
// Stop your service
return server.Shutdown(ctx)
},
)
runner.Start(context.Background())
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalid = errors.New("invalid instance")
ErrInvalid is returned when the runner instance or its functions are nil/invalid.
Functions ¶
This section is empty.
Types ¶
type StartStop ¶
type StartStop interface {
// Runner embeds the base server interface providing lifecycle operations:
// Start, Stop, Restart, IsRunning, Uptime
libsrv.Runner
// Errors embeds error tracking operations:
// ErrorsLast, ErrorsList
liberr.Errors
}
StartStop defines the interface for managing service lifecycle operations. It combines server management (Start, Stop, Restart, IsRunning, Uptime) with error tracking (ErrorsLast, ErrorsList). All operations are thread-safe.
func New ¶
New creates a new StartStop runner with the provided start and stop functions. The start function is executed asynchronously when Start() is called, and should block until the service terminates. The stop function is called to gracefully shut down the service.
Parameters:
- start: Function to start the service (runs asynchronously, should block)
- stop: Function to stop the service (called on Stop/Restart)
Returns:
- StartStop: Thread-safe runner instance ready to use
Example:
runner := startStop.New(
func(ctx context.Context) error {
return httpServer.ListenAndServe() // Blocks until server stops
},
func(ctx context.Context) error {
return httpServer.Shutdown(ctx) // Graceful shutdown
},
)