waitabit

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2019 License: MIT Imports: 5 Imported by: 0

README

Wait a bit

Tiny library for manage you application shutdown in graceful way by catching the OS signals.

Documentation

Installation

go get -u github.com/heartwilltell/waitabit

Usage

package main

import (
    "log"
    "os"

    "github.com/heartwilltell/waitabit"
)


func main() { 
	
    // call your application here ...
    
    wait := waitabit.NewWait(os.Interrupt)
    wait.WaitWithFunc(func() {
        log.Println("Bye")
    })
    
    // or ...
    
    waitabit.NewWait(os.Interrupt).WaitWithFunc(func() {
    	log.Println("Bye")
    })
    
}

Documentation

Overview

Package waitabit provides functionality for handling system interrupts.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Wait

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

Wait struct represent wait functionality.

func NewWait

func NewWait(signals ...os.Signal) *Wait

NewWait create a new instance of Wait type.

func (*Wait) Wait

func (w *Wait) Wait()

Wait simply wait for interruption.

Example
wait := NewWait(os.Interrupt)
wait.Wait()
fmt.Println("Bye")
Output:

Bye

func (*Wait) WaitWithFunc

func (w *Wait) WaitWithFunc(function func())

WaitWithFunc receive function as argument and call Wait() to wait interrupt signal. The function will be called after Wait().

Example
wait := NewWait(os.Interrupt)
wait.WaitWithFunc(func() {
	// your logic here
	fmt.Println("Bye")
})
Output:

Bye

func (*Wait) WaitWithFuncErr

func (w *Wait) WaitWithFuncErr(function func() error) error

WaitWithFuncErr receive function as argument and call Wait() to wait interrupt signal. The function will be called after Wait(). If function return an error it would be wrapped and returned.

Example
wait := NewWait(os.Interrupt)
if err := wait.WaitWithFuncErr(func() error {
	// your logic here
	return fmt.Errorf("something went wrong")
}); err != nil {
	fmt.Println("something bad happened:", err)
}
fmt.Println("Bye")
Output:

Bye

func (*Wait) WaitWithTimeout

func (w *Wait) WaitWithTimeout(timeout time.Duration)

WaitWithTimeout wait a given amount fo time and then exit.

Example
wait := NewWait(os.Interrupt)
wait.WaitWithTimeout(1 * time.Second)
fmt.Println("Bye")
Output:

Bye

func (*Wait) WaitWithTimeoutAndFunc

func (w *Wait) WaitWithTimeoutAndFunc(timeout time.Duration, function func())

WaitWithTimeoutAndFunc wait a given amount of time then call function and then exit.

Example
wait := NewWait(os.Interrupt)
wait.WaitWithTimeoutAndFunc(1*time.Second, func() {
	// your logic here
	fmt.Println("Bye")
})
Output:

Bye

func (*Wait) WaitWithTimeoutAndFuncErr

func (w *Wait) WaitWithTimeoutAndFuncErr(timeout time.Duration, function func() error) error

WaitWithTimeoutAndFunc wait a given amount of call function and then exit. If function return an error it would be wrapped and returned.

Example
wait := NewWait(os.Interrupt)
if err := wait.WaitWithTimeoutAndFuncErr(1*time.Second, func() error {
	// your logic here
	return fmt.Errorf("something went wrong")
}); err != nil {
	fmt.Println("something bad happened:", err)
}
fmt.Println("Bye")
Output:

Bye

Jump to

Keyboard shortcuts

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