wrapper

package module
v0.0.0-...-5fa47ac Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: MIT Imports: 10 Imported by: 4

README

CLI Wrapper

This package allows to set a set of Signal Handling functions and then execute them when the corresponding signal has been sent by the OS to the executable.

See the example for an idea about how to use.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HttpServer

func HttpServer(setters ...SetFn) (func(context.Context) error, func(context.Context) error)

HttpServer initializes an http.Server object with values set using SetFn() functions

func RegisterSignalHandlers

func RegisterSignalHandlers(handlers SignalHandlers) *w

RegisterSignalHandlers sets up the signal handlers we want to use

Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"log"
	"os"
	"syscall"
	"time"
)

var out = bytes.Buffer{}

func loopPrintSeconds(w io.Writer) func(ctx context.Context) error {
	return func(ctx context.Context) error {
		st := time.Now()
		for {
			select {
			case <-ctx.Done():
				if err := ctx.Err(); err != nil {
					_, _ = fmt.Fprintf(w, "Stopping\n")
					return err
				}
			default:
				time.Sleep(time.Second)
				_, _ = fmt.Fprintf(w, "%s", time.Now().Sub(st).Truncate(time.Second))
			}
		}
		return nil
	}
}

func sendSignals(pid int) {
	time.Sleep(1100 * time.Millisecond)
	_ = syscall.Kill(pid, syscall.SIGHUP)
	time.Sleep(1 * time.Second)
	_ = syscall.Kill(pid, syscall.SIGUSR1)
	time.Sleep(1 * time.Second)
	_ = syscall.Kill(pid, syscall.SIGTERM)
	time.Sleep(1 * time.Second)
	_ = syscall.Kill(pid, syscall.SIGINT)
}

func main() {
	l := log.New(&out, "", 0)

	ctx, stopFn := context.WithTimeout(context.Background(), 7*time.Second)
	defer stopFn()

	go sendSignals(os.Getpid())

	err := RegisterSignalHandlers(
		SignalHandlers{
			syscall.SIGHUP: func(_ chan<- error) {
				l.SetPrefix("\nSIGHUP ")
				l.Printf("reloading config")
			},
			syscall.SIGUSR1: func(_ chan<- error) {
				l.SetPrefix("\nSIGUSR1 ")
				l.Printf("performing maintenance task")
			},
			syscall.SIGTERM: func(exit chan<- error) {
				l.SetPrefix("\nSIGTERM ")
				l.Printf("stopping gracefully")
				_, _ = fmt.Fprintf(l.Writer(), "Here we can gracefully close things (waiting 3s)\n")
				time.Sleep(3 * time.Second)
				exit <- nil
			},
			syscall.SIGINT: func(exit chan<- error) {
				l.SetPrefix("\nSIGINT ")
				l.Printf("interrupted by user\n")
				exit <- fmt.Errorf("stopped with interruption")
			},
		},
	).Exec(ctx, loopPrintSeconds(&out))

	if err != nil {
		l.Printf("%+s", err)
	}
	fmt.Printf(out.String())

}
Output:

1s
SIGHUP reloading config
2s
SIGUSR1 performing maintenance task
3s
SIGTERM stopping gracefully
Here we can gracefully close things (waiting 3s)
4s5s6s
SIGINT interrupted by user

Types

type SetFn

type SetFn func(*c) error

func GracefulWait

func GracefulWait(g time.Duration) SetFn

func Handler

func Handler(h http.Handler) SetFn

func OnSocket

func OnSocket(s string) SetFn

func OnSystemd

func OnSystemd() SetFn

func OnTCP

func OnTCP(addr string) SetFn

func WithTLSCert

func WithTLSCert(cert, key string) SetFn

func WriteWait

func WriteWait(d time.Duration) SetFn

type SignalHandlers

type SignalHandlers map[os.Signal]handlerFn

SignalHandlers is a map that stores the association between signals and functions to be executed

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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