starter

package module
v0.0.0-...-e7e1d84 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2017 License: MIT Imports: 8 Imported by: 0

README

Starter - Start/Stop daemon in Golang

This is for UNIX based systems only.

Install

$ go get github.com/AlexanderGrom/go-starter

Simple Example

package main

import (
	"context"
	"log"
	"net/http"
	"time"

	"github.com/AlexanderGrom/go-starter"
)

func main() {
	mux := http.NewServeMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World!\n"))
	})

	srv := &http.Server{
		Addr:    ":8080",
		Handler: mux,
	}

	// Registration function: Close listening port
	starter.Bind(func() {
		ctx, _ := context.WithTimeout(context.Background(), 60*time.Second)
		srv.Shutdown(ctx)
	})

	go func() {
		if err := srv.ListenAndServe(); err != nil {
			log.Println("Error Serve:", err)
		}
	}()

	// Registration function: Close cache
	starter.Bind(func() {
		time.Sleep(1 * time.Second) // Simulation
	})

	// Registration function: Close database connection
	starter.Bind(func() {
		time.Sleep(1 * time.Second) // Simulation
	})

	// Registration function: Close logs
	starter.Bind(func() {
		time.Sleep(1 * time.Second) // Simulation
	})

	// Wait until all functions completes
	starter.Wait()
}

Start programm:

$ ./myapp start

Correct Stop programm:

$ ./myapp stop

Correct Restart programm:

$ ./myapp restart

-- THE END --

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(fn func())

Привязываем функцию завершения

func Errorln

func Errorln(format string, v ...interface{}) error

Отправляет сообщение в Stderr

func Fatalln

func Fatalln(format string, v ...interface{})

Отправляет сообщение в Stderr и заверщает программу с кодом 1

func Print

func Print(format string, v ...interface{}) error

Отправляет сообщение в Stdout

func Println

func Println(format string, v ...interface{}) error

Отправляет сообщение в Stdout и добавляет перевед строки

func Wait

func Wait()

Ждем пока не будет обработан выход

Types

This section is empty.

Jump to

Keyboard shortcuts

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