signal

package
v0.2.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package signal provides graceful signal handling for the StackEye CLI.

It intercepts SIGINT (Ctrl+C) and SIGTERM, cancels in-progress operations via context, runs registered cleanup functions, and exits with POSIX-standard codes (130 for SIGINT, 143 for SIGTERM).

Usage:

ctx, handler := signal.Setup()
handler.OnCleanup(func() { telemetry.Flush(2 * time.Second) })
exitCode := cmd.ExecuteWithContext(ctx)
os.Exit(handler.ExitCode(exitCode))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler manages signal interception and cleanup coordination.

func Setup

func Setup() (context.Context, *Handler)

Setup registers SIGINT and SIGTERM handlers and returns a cancellable context along with a Handler for cleanup coordination. The returned context is canceled when a signal is received, allowing in-progress operations to terminate gracefully.

func (*Handler) Cancel

func (h *Handler) Cancel()

Cancel cancels the handler's context. Called during normal shutdown to release the signal-watching goroutine.

func (*Handler) ExitCode

func (h *Handler) ExitCode(cmdExitCode int) int

ExitCode returns the appropriate exit code. If a signal was caught, it returns the POSIX signal exit code (130 for SIGINT, 143 for SIGTERM). Otherwise it returns the provided command exit code unchanged.

func (*Handler) OnCleanup

func (h *Handler) OnCleanup(fn func())

OnCleanup registers a function to be called during graceful shutdown. Cleanup functions run in LIFO order (last registered runs first).

func (*Handler) RunCleanups

func (h *Handler) RunCleanups()

RunCleanups executes all registered cleanup functions in LIFO order. Safe to call multiple times; subsequent calls are no-ops.

func (*Handler) Signal

func (h *Handler) Signal() os.Signal

Signal returns the caught signal, or nil if none was received.

func (*Handler) Signaled

func (h *Handler) Signaled() bool

Signaled returns true if a signal was caught.

Jump to

Keyboard shortcuts

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