winsvc

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 8 Imported by: 0

README

winsvc

Provides creating and running Go Windows Service

Features
  • Restarts service on failure. Service will be restarted:
    1. Threw panic
    2. Exit from run function had happened before context execution canceled (command of the stop was not sent) . winsvc.DisablePanic is option to disable this behavior.
    3. Service had got command but it caught panic
  • context.Context for graceful self shutdown
  • Returns from winsvc.Run if it stops for a long time. winsvc.TimeoutStop is option which it default equals value 20s
  • Package uses os.Chdir for easy using relative path
Install

go get -u github.com/itcomusic/winsvc

Example
package main

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

	"github.com/itcomusic/winsvc"
)

type Application struct {
	srv *http.Server
}

func main() {
	winsvc.Run(func(ctx context.Context) {
		app := New()
		if err := app.Run(ctx); err != nil {
			log.Printf("[ERROR] rest terminated with error, %s", err)
			return
		}
		log.Printf("[WARN] rest terminated")
	})
	// service has been just stopped, but process of the go has not stopped yet
	// that is why recommendation is to not write any logic
}

func New() *Application {
	mux := http.NewServeMux()
	server := &http.Server{
		Addr:    "0.0.0.0:8080",
		Handler: mux,
	}
	
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte("hello winsvc"))
	})
	mux.HandleFunc("/exit", func(w http.ResponseWriter, r *http.Request) {
		// service will be restarted
		os.Exit(1)
	})
	mux.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
		// service will be restarted
		server.Shutdown(context.TODO())
	})
    
	return &Application{srv: server}
}

func (a *Application) Run(ctx context.Context) error {
	log.Print("[INFO] started rest")

	go func() {
		defer log.Print("[WARN] shutdown rest server")
		// shutdown on context cancellation
		<-ctx.Done()
		c, _ := context.WithTimeout(context.Background(), time.Second*5)
		a.srv.Shutdown(c)
	}()

	log.Printf("[INFO] started http server on port :%d", 8080)
	return a.srv.ListenAndServe()
}
Using sc.exe
$ sc.exe create "gowinsvc" binPath= "path\gowinsvc.exe" start= auto
$ sc.exe failure "gowinsvc" reset= 0 actions= restart/5000

Documentation

Rendered for windows/amd64

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisablePanic

func DisablePanic() option

DisablePanic is a option to disabling panic when exit from run function.

func Interactive

func Interactive() bool

Interactive returns false if running under the OS service manager and true otherwise.

func Run

func Run(r runFunc, opts ...option)

Run initializes new windows service and runs command action. runFunc provides a place to initiate the service. runFunc function always has blocked and exit from it, means that service will be stopped correctly if is context was canceled. runFunc should not call os.Exit directly in the function, it is not correctly service stop. Context canceled it is mean that signal of stop got and need to stop run function.

func TimeoutStop

func TimeoutStop(t time.Duration) option

TimeoutStop is a option to specify timeout of stopping service. After expired timeout, process of service will be terminated. If is not set option, value will be equal default value 20s.

Types

This section is empty.

Jump to

Keyboard shortcuts

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