shutdown

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: MIT Imports: 4 Imported by: 2

README

shutdown

GoDoc

shutdown is a simple package that helps facilitate a graceful shutdown of your application. It can create a context that will be canceled when your program receives SIGTERM or SIGINT from the os.

Installation

go get github.com/jtwatson/shutdown@latest

Simple Example

package main

import (
	"context"
	"fmt"
	"syscall"
	"time"

	"github.com/jtwatson/shutdown"
)

func main() {
	// Capture interrupt so we can handle them gracefully.
	ctx, cancel := shutdown.CaptureInterrupts(context.Background())
	defer cancel()

	fmt.Println("Waiting for interrupt")

	go func() {
		time.Sleep(time.Second)
		_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
	}()

	<-ctx.Done()

	fmt.Println("Received interrupt")
}

Documentation

Overview

shutdown is a simple package for creating a context that will be canceled if your program receives SIGTERM or SIGINT from the os

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureInterrupts

func CaptureInterrupts(parent context.Context) (ctx context.Context, cancel context.CancelFunc)

CaptureInterrupts is the same as context.WithCancel, but the returned context will cancel if your program receives an interrupt from the os

Example
package main

import (
	"context"
	"fmt"
	"syscall"
	"time"

	"github.com/jtwatson/shutdown"
)

func main() {
	// Capture interrupt so we can handle them gracefully.
	ctx, cancel := shutdown.CaptureInterrupts(context.Background())
	defer cancel()

	fmt.Println("Waiting for interrupt")

	go func() {
		time.Sleep(time.Millisecond * 100)
		_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
	}()

	<-ctx.Done()

	fmt.Println("Received interrupt")
}
Output:
Waiting for interrupt
Received interrupt

Types

This section is empty.

Jump to

Keyboard shortcuts

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