svc

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2021 License: MIT Imports: 4 Imported by: 61

README

go-svc

Go Reference MIT License Go Report Card Build Status

Go Windows Service wrapper that plays nice with Linux. Windows tests here.

Project Status

  • Used in Production.
  • Maintained. Issues and Pull Requests will be responded to.

Go Modules

  • Please note the import path and go.mod change from github.com/judwhite/go-svc/svc to github.com/judwhite/go-svc for v1.2+
  • v1.1.3 and earlier can be imported using the previous import path
  • v1.2+ code is backwards compatible with previous versions
module awesomeProject

go 1.15

require github.com/judwhite/go-svc v1.2.0
import "github.com/judwhite/go-svc"

Example

package main

import (
	"log"
	"sync"

	"github.com/judwhite/go-svc"
)

// program implements svc.Service
type program struct {
	wg   sync.WaitGroup
	quit chan struct{}
}

func main() {
	prg := &program{}

	// Call svc.Run to start your program/service.
	if err := svc.Run(prg); err != nil {
		log.Fatal(err)
	}
}

func (p *program) Init(env svc.Environment) error {
	log.Printf("is win service? %v\n", env.IsWindowsService())
	return nil
}

func (p *program) Start() error {
	// The Start method must not block, or Windows may assume your service failed
	// to start. Launch a Goroutine here to do something interesting/blocking.

	p.quit = make(chan struct{})

	p.wg.Add(1)
	go func() {
		log.Println("Starting...")
		<-p.quit
		log.Println("Quit signal received...")
		p.wg.Done()
	}()

	return nil
}

func (p *program) Stop() error {
	// The Stop method is invoked by stopping the Windows service, or by pressing Ctrl+C on the console.
	// This method may block, but it's a good idea to finish quickly or your process may be killed by
	// Windows during a shutdown/reboot. As a general rule you shouldn't rely on graceful shutdown.

	log.Println("Stopping...")
	close(p.quit)
	p.wg.Wait()
	log.Println("Stopped.")
	return nil
}

More Examples

See the example directory for more examples, including installing and uninstalling binaries built in Go as Windows services.

Similar Projects

License

go-svc is under the MIT license. See the LICENSE file for details.

Documentation

Overview

Package svc helps you write Windows Service executables without getting in the way of other target platforms.

To get started, implement the Init, Start, and Stop methods to do any work needed during these steps.

Init and Start cannot block. Launch long-running code in a new Goroutine.

Stop may block for a short amount of time to attempt clean shutdown.

Call svc.Run() with a reference to your svc.Service implementation to start your program.

When running in console mode Ctrl+C is treated like a Stop Service signal.

For a full guide visit https://github.com/judwhite/go-svc

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(service Service, sig ...os.Signal) error

Run runs your Service.

Run will block until one of the signals specified in sig is received or a provided context is done. If sig is empty syscall.SIGINT and syscall.SIGTERM are used by default.

Types

type Context

type Context interface {
	Context() context.Context
}

Context interface contains an optional Context function which a Service can implement. When implemented the context.Done() channel will be used in addition to signal handling to exit a process.

type Environment

type Environment interface {
	// IsWindowsService reports whether the program is running as a Windows Service.
	IsWindowsService() bool
}

Environment contains information about the environment your application is running in.

type Service

type Service interface {
	// Init is called before the program/service is started and after it's
	// determined if the program is running as a Windows Service. This method must
	// be non-blocking.
	Init(Environment) error

	// Start is called after Init. This method must be non-blocking.
	Start() error

	// Stop is called in response to syscall.SIGINT, syscall.SIGTERM, or when a
	// Windows Service is stopped.
	Stop() error
}

Service interface contains Start and Stop methods which are called when the service is started and stopped. The Init method is called before the service is started, and after it's determined if the program is running as a Windows Service.

The Start and Init methods must be non-blocking.

Implement this interface and pass it to the Run function to start your program.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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