Coder Social home page Coder Social logo

wait_signals's Introduction

Wait Signal

Tests

This library has a BSD 3-Clause License.

Use it has you will.

Disclaimer

See: The LICENCE information.

Overview

The Go module wait_signal implements two widely used blocks of code for waiting for os.Signals from the operating system.

Both functions wait until one of the desired os.Signals to be fired by the operating system but another also waits for a certain timeout.

  • The Wait function just blocks the thread execution until it get one of the desired signals.
  • The SleepWait function blocks the thread execution until it get one of the desired signals or a timeout occurs.

Add to your project

go get github.com/joaoribeirodasilva/wait_signal

Usage

Bellow you will find the usage exemples.

SleepWait() Example

package main

import (
    "fmt"
    "os"
    "syscall"
    "time"

    "github.com/joaoribeirodasilva/wait_signals"
)

// Example
// SleepWait(sleep_time time.Duration, sigs ...os.Signal) os.Signal.
func main() {

    fmt.Println("wait_signals.SleepWait example")
    fmt.Println("wait 5 seconds for a sleep timeout or press CRTL+C to exit by a signal.")

    // the thread will block until a timeout set by the *sleep_time*
    // parameter or until it gets a syscall.SIGINT or a syscall.SIGTERM
    // signal (Ex: CTRL+C).
    sig := wait_signals.SleepWait(
        time.Duration(5000) * time.Millisecond, // sleep for 5 seconds.
        syscall.SIGINT, // wait for syscall.SIGINT.
        syscall.SIGTERM, // or for syscall.SIGTERM.
    )

    // if *sig* is null the we unblock due to the duration timeout.
    // if *sig* is not nil the it's a pointer to the signal received.
    if sig == nil {
        fmt.Printf("we exited due to a timeout\n")
    } else if *sig == syscall.SIGINT {
        fmt.Printf("we exited due to syscall.SIGINT signal\n")
    } else if *sig == syscall.SIGTERM {
        fmt.Printf("we exited due to syscall.SIGTERM signal\n")
    }

    os.Exit(0)
}

Wait() Example

package main

// Example
// Wait(sigs ...os.Signal) *os.Signal.
func main() {

    // the thread will block until it gets a syscall.SIGINT 
    // or a syscall.SIGTERM signal (Ex: CTRL+C).
    sig := wait_signals.Wait(syscall.SIGINT, syscall.SIGTERM)

    // the sig return which signal was received.
    if sig == syscall.SIGINT {
        fmt.Printf("we received a syscall.SIGINT signal\n")
    } else {
        fmt.Printf("we received a syscall.SIGTERM signal\n")
    }
    os.Exit(0)
}

wait_signals's People

Contributors

joaoribeirodasilva avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.