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 ¶
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 ¶
WithLogger sets the logger used for signal receipts and (when WithContinueOnError is active) action errors.
func WithSignals ¶
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 ¶
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
}
Output:
func (*Task) Run ¶
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.