startStop

package
v1.19.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 9 Imported by: 0

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

View Source
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

func New(start, stop func(ctx context.Context) error) StartStop

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
    },
)

Jump to

Keyboard shortcuts

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