• Stars
    star
    116
  • Rank 298,071 (Top 6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 7 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Golang library that provides a standard way to execute code when a process exits normally or due to a signal.

Goodbye

Goodbye is a Golang library that provides a standard way to execute code when a process exits normally or due to a signal.

Overview

The Goodbye library uses a sync.Once to ensure that the registered exit handlers are executed only once -- whether as a result of the process exiting normally or as a result of a received signal.

Install

Say hello to goodbye with go get github.com/thecodeteam/goodbye.

Example

There is an example program that illustrates how to use the Goodbye library:

package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/thecodeteam/goodbye"
)

func main() {
	// Create a context to use with the Goodbye library's functions.
	ctx := context.Background()

	// Always defer `goodbye.Exit` as early as possible since it is
	// safe to execute no matter what.
	defer goodbye.Exit(ctx, -1)

	// Invoke `goodbye.Notify` to begin trapping signals this process
	// might receive. The Notify function can specify which signals are
	// trapped, but if none are specified then a default list is used.
	// The default set is platform dependent. See the files
	// "goodbye_GOOS.go" for more information.
	goodbye.Notify(ctx)

	// Register two functions that will be executed when this process
	// exits.
	goodbye.Register(func(ctx context.Context, sig os.Signal) {
		fmt.Printf("1: %[1]d: %[1]s\n", sig)
	})

	goodbye.Register(func(ctx context.Context, sig os.Signal) {
		fmt.Printf("2: %[1]d: %[1]s\n", sig)
	})

	// Register a function with a priority that is higher than the two
	// handlers above. Since the default priority is 0, a priority of -1
	// will ensure this function, registered last, is executed first.
	goodbye.RegisterWithPriority(func(ctx context.Context, sig os.Signal) {

		// Use the `goodbye.IsNormalExit` function in conjunction with
		// the signal to determine if this is a signal-based exit. If it
		// is then emit a leading newline character to place the desired
		// text on a line after the `CTRL-C` if that was used to send
		// SIGINT to the process.
		//
		// Note that the extra text is being printed inside the second,
		// registered handler. This is because handlers are executed in
		// reverse order -- the earlier a handler is registered, the later
		// it is executed.
		if !goodbye.IsNormalExit(sig) {
			fmt.Println()
		}

		fmt.Printf("0: %[1]d: %[1]s\n", sig)
	}, -1)

	if len(os.Args) < 2 {
		return
	}

	// If the program's first argument is "wait" then block until the
	// program is killed with a signal -- either from the "kill" command
	// or a CTRL-C.
	if strings.EqualFold("wait", os.Args[1]) {
		c := make(chan int)
		<-c
	}
}

Example 1

This example shows how the exit handlers are invoked when the program exits normally.

$ go run example/example.go
0: 0: nosig
1: 0: nosig
2: 0: nosig

Please note that the first registered exit handler was executed last.

Example 2

This example shows how the exit handlers are executed when a signal is received:

$ go run example/example.go wait
^C
0: 2: interrupt
1: 2: interrupt
2: 2: interrupt

More Repositories

1

libstorage

libStorage provides a portable and remotable storage plugin framework.
Go
146
star
2

training

HTML
120
star
3

labs

{code} Projects in Action
90
star
4

mesos-module-dvdi

Mesos Docker Volume Driver Isolator module
C++
77
star
5

vagrant

All {code] by Dell EMC related Vagrant projects
Shell
54
star
6

polly

Volume scheduling for container schedulers
Go
48
star
7

codedellemc.github.io

the home for {code}
JavaScript
44
star
8

ecs-cf-service-broker

Cloud Foundry Service Broker for EMC ECS Object Storage
Java
29
star
9

scaleio-framework

The ScaleIO Framework implements an Apache Mesos Framework to manage the lifecycle of ScaleIO, a scale-out software-based block storage platform.
Go
26
star
10

puppet-scaleio

A Puppet module for installing, and configuring ScaleIO 2.0x data services components.
Ruby
20
star
11

mars-challenge

{code} by Dell EMC Hackathon presented at ContainerCon, MesosCon, and LISA
Go
19
star
12

mosaicme

Micro service application that retrieves images from Twitter feeds, stores and process them to build a mosaic based on a selected picture.
JavaScript
15
star
13

goisilon

Isilon package that provides API bindings for Go
Go
13
star
14

flocker-drivers

This is a collection of the EMC storage platform drivers for ClusterHQ's Flocker
Python
12
star
15

ansible-role-rexray

Ansible Galaxy role for installing REX-Ray
12
star
16

docker-machine-rackhd

RackHD Driver for Docker Machine
Go
12
star
17

dssd

Linux Developer Libraries
12
star
18

goscaleio

Archived repo for GoScaleIO
Go
11
star
19

heliosburn

Helios Burn - A REST Fault Injection Platform
Python
11
star
20

csi-vsphere

A Container Storage Interface (CSI) Storage Plug-in (SP) for VMware vSphere (deprecated)
Go
10
star
21

SIOToolKit

EMC ScaleIO Powershell Toolkit
PowerShell
10
star
22

viprcommand

ViPR Command provides command line interface for ViPR REST APIs.
Python
9
star
23

gorackhd-redfish

Go bindings for the RackHD Redfish API
Go
9
star
24

puppet-vnx

Simplify the configuration of VNX with a puppet module
Ruby
8
star
25

nagios-unity

Nagios plugin for monitoring Unity system
Python
7
star
26

csi-scaleio

See the VxFlexOS repo for the new driver!
Go
7
star
27

emccode-newsletter

Archive of Weekly EMC {code} Newsletter
6
star
28

machine

All Docker Machine related demonstration material
Shell
6
star
29

community

JavaScript
5
star
30

govmax

VMAX package that provides API bindings for Go
Go
5
star
31

infrakit.rackhd

Infrakit plugins for RackHD.
Go
5
star
32

dashboard

JavaScript
5
star
33

python-scaleioclient

API binding for ScaleIO used by Nova
Python
5
star
34

puppet-scaleio-docs

4
star
35

ecs-broker-tile

Dell EMC ECS Service Broker packaged as a Cloud Foundry tile
JavaScript
4
star
36

goxtremio

A package that provides XtremIO storage platform API bindings
Go
4
star
37

viprshell

ViPR PowerShell Module
PowerShell
3
star
38

roadmap

The {code} Team Roadmap
3
star
39

mesos-module-libstorage

libStorage mesos module
C++
3
star
40

nova-scaleio-ephemeral

Implementation of ephemeral storage drivers for various versions of OpenStack
3
star
41

vg-kubernetes

Shell
2
star
42

puppet-unity

Puppet module for Unity system
Ruby
2
star
43

puppet-scaleio-openstack

Puppet module to configure OpenStack to work with ScaleIO
Python
2
star
44

gorackhd

Go bindings for the RackHD API
Go
2
star
45

goscli

Go
1
star
46

juju-scaleio

JuJu Charms for ScaleIO
Shell
1
star
47

presentations

a home for the presentations we use at any conference or meetup
1
star
48

conferences

1
star
49

rackhdcli

A Go-based CLI for RackHD
Go
1
star
50

VStriker

ViPR Data Services Throughput Analyzer
Java
1
star
51

cloudformation

1
star
52

puppet-rexray

Install REX-Ray via Puppet
Puppet
1
star
53

ecs-broker-boshrelease

Shell
1
star