sigmod

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: Apache-2.0 Imports: 3 Imported by: 1

README

sigmod

sigmod listens for OS signals and returns from Run when one is received, triggering srvc shutdown.

package main

import (
	"fmt"
	"net/http"
	"os"
	"syscall"

	"github.com/go-srvc/mods/httpmod"
	"github.com/go-srvc/mods/sigmod"
	"github.com/go-srvc/srvc"
)

func main() {
	srvc.RunAndExit(
		// Trigger graceful shutdown of all modules on SIGINT or SIGTERM.
		sigmod.New(os.Interrupt, syscall.SIGTERM),
		httpmod.New(
			httpmod.WithAddr(":8080"),
			httpmod.WithHandler(http.HandlerFunc(hello)),
		),
	)
}

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "hello, world")
}

Documentation

Overview

Package sigmod provides signal listening as a module.

Index

Examples

Constants

View Source
const ID = "sigmod"

Variables

This section is empty.

Functions

This section is empty.

Types

type Listener

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

func New

func New(signals ...os.Signal) *Listener

New creates signal listener for given signals. If no signal is provided, os.Interrupt will be used.

Example
package main

import (
	"os"
	"syscall"
	"time"

	"github.com/go-srvc/mods/sigmod"
	"github.com/go-srvc/srvc"
)

func main() {
	//nolint: errcheck
	go func() {
		// Send SIGINT after 1 second.
		time.Sleep(time.Second)
		p, _ := os.FindProcess(syscall.Getegid())
		p.Signal(os.Interrupt)
	}()

	srvc.RunAndExit(
		sigmod.New(os.Interrupt),
	)
}

func (*Listener) ID

func (l *Listener) ID() string

func (*Listener) Init

func (l *Listener) Init() error

func (*Listener) Run

func (l *Listener) Run() error

func (*Listener) Stop

func (l *Listener) Stop() error

Jump to

Keyboard shortcuts

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