sigtrigger

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package sigtrigger provides a Task that executes an action each time a configured OS signal is received. Unlike ossignal, which exits on the first signal, sigtrigger stays alive and re-runs the action on every signal delivery. Signal capture begins at NewTask construction time.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultSignals

func DefaultSignals() []os.Signal

DefaultSignals returns the default signal list: [SIGHUP].

Types

type Option

type Option func(*options)

Option is an option func for NewTask.

func WithContinueOnError

func WithContinueOnError() Option

WithContinueOnError causes action errors to be logged and discarded rather than terminating the task. A logger should be set via WithLogger so errors are visible; without one they are silently discarded.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger used for signal receipts and (when WithContinueOnError is active) action errors.

func WithSignals

func WithSignals(signals ...os.Signal) Option

WithSignals overrides the default signals being listened for.

type Task

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

Task is a task.Task that runs an action each time a configured OS signal is received. It stays alive until the context is cancelled.

func NewTask

func NewTask(action task.Action, opts ...Option) *Task

NewTask creates a new Task that calls action on each signal delivery. Signal capture begins immediately upon construction. Panics if the resolved signals list is empty.

Example

ExampleNewTask demonstrates creating a sigtrigger Task that reloads configuration each time SIGHUP is received.

package main

import (
	"context"
	"log/slog"

	"github.com/wood-jp/task/sigtrigger"
)

func main() {
	t := sigtrigger.NewTask(
		func(ctx context.Context) error {
			// reload configuration from disk
			return nil
		},
		sigtrigger.WithLogger(slog.Default()),
		sigtrigger.WithContinueOnError(),
	)
	_ = t
}

func (*Task) Name

func (t *Task) Name() string

Name returns the name of this task, including the signals being listened for.

func (*Task) Run

func (t *Task) Run(ctx context.Context) error

Run blocks until ctx is cancelled, returning nil. Each time a configured signal is received, the action is executed. If the action returns an error and WithContinueOnError is not set, Run returns that error immediately. Returns task.ErrAlreadyStarted if called more than once.

Jump to

Keyboard shortcuts

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