cmd

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2018 License: MIT Imports: 5 Imported by: 0

README

cmd

Go library to encapsulate the signal handling pattern for termination signal so that I can properly clean up.

package main

import (
	"fmt"
	"time"

	"github.com/gdey/cmd"
)

func cleanup(s string) {
	fmt.Printf("Cleaning up: %s!\n", s)
}

func main() {
	defer cmd.New().Complete()
	// Main code here
	fmt.Println("Runing 3 times.")
	for i := 0; i < 3; i++ {
		fmt.Println("Going to nap for a second.")
		select {
		case <-time.After(5 * time.Second):
			fmt.Println("Ahhh that was a good nap!")
		case <-cmd.Cancelled():
			fmt.Println("Ho! I got Ctr-C")
			cleanup("for and return")
			return
		}
		fmt.Println("Going sleep some more")
		<-time.After(2 * time.Second)
		fmt.Println("Good sleep!")
		if cmd.IsCancelled() {
			fmt.Println("Ctr-C got called.")
			cleanup("for and break")
			break
		}
		// do some chunk off work.
		<-time.After(2 * time.Second)
	}
	fmt.Println("All done!")
}

Documentation

Overview

Package cmd contains the Context type that can be used to cleanly terminate an application upon receiving a Termination signal. This package wrapps the basic pattern I have observed to enable it to be simpler to use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cancelled

func Cancelled() <-chan struct{}

Cancelled is provided for use in select statments. It can be used to determine if a termination signal has been sent.

func Complete

func Complete(fns ...func())

Complete is a blocking call that should be the last call in your main function. The purpose of this function is to wait for the cancel go routine to cleanup corretly.

func IsCancelled

func IsCancelled() bool

IsCancelled is provided for use in if and for blocks, this can be used to check to see if a termination signal has been send, and to the excuate appropriate logic as needed.

func New

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

New initilizes and setups up the global context. An explicate list of signals can be passed in as well, if no list is passed os.Interrupt, and syscall.SIGTERM is assumed.

func NewContext

func NewContext(signals ...os.Signal) *contextType

NewContext initilizes and setups up the context. An explicate list of signals can be passed in as well, if no list is passed os.Interrupt, and syscall.SIGTERM is assumed.

func OnComplete

func OnComplete(fns ...func())

OnComplete adds a set of functions that are called just before complete; exists. The functions are called in reverse order. function passed to Complete are called after functions defined by OnComplete

func Signal

func Signal() os.Signal

Signal provides one the ability to introspect which signal was actually send.

Types

This section is empty.

Jump to

Keyboard shortcuts

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