From 18e8cc1986ece61f61845091e5f4003683f1975d Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Wed, 21 Jan 2015 14:46:30 -0800 Subject: [PATCH] service/example: add a super simple example. Use in doc. --- example/{ => logging}/main.go | 0 example/simple/main.go | 51 ++++++++++++++++++++++++++ service.go | 67 ++++------------------------------- 3 files changed, 58 insertions(+), 60 deletions(-) rename example/{ => logging}/main.go (100%) create mode 100644 example/simple/main.go diff --git a/example/main.go b/example/logging/main.go similarity index 100% rename from example/main.go rename to example/logging/main.go diff --git a/example/simple/main.go b/example/simple/main.go new file mode 100644 index 0000000..cc4866b --- /dev/null +++ b/example/simple/main.go @@ -0,0 +1,51 @@ +// Copyright 2015 Daniel Theophanes. +// Use of this source code is governed by a zlib-style +// license that can be found in the LICENSE file.package service + +// simple does nothing except block while running the service. +package main + +import ( + "log" + + "github.com/kardianos/service" +) + +var logger service.Logger + +type program struct{} + +func (p *program) Start(s service.Service) error { + // Start should not block. Do the actual work async. + go p.run() + return nil +} +func (p *program) run() { + // Do work here +} +func (p *program) Stop(s service.Service) error { + // Stop should not block. Return with a few seconds. + return nil +} + +func main() { + svcConfig := &service.Config{ + Name: "GoServiceTest", + DisplayName: "Go Service Test", + Description: "This is a test Go service.", + } + + prg := &program{} + s, err := service.New(prg, svcConfig) + if err != nil { + log.Fatal(err) + } + logger, err = s.Logger(nil) + if err != nil { + log.Fatal(err) + } + err = s.Run() + if err != nil { + logger.Error(err) + } +} diff --git a/service.go b/service.go index 0d9bf76..bcc1d0d 100644 --- a/service.go +++ b/service.go @@ -10,69 +10,35 @@ // despite the substantial differences. // It also can be used to detect how a program is called, from an interactive // terminal or from a service manager. +// +// Examples in the example/ folder. /* - // Simple service that only works by printing a log message every few seconds. package main import ( - "flag" "log" - "time" "github.com/kardianos/service" ) var logger service.Logger - // Program structures. - // Define Start and Stop methods. - type program struct { - exit chan struct{} - } + type program struct{} func (p *program) Start(s service.Service) error { - if service.Interactive() { - logger.Info("Running in terminal.") - } else { - logger.Info("Running under service manager.") - } - p.exit = make(chan struct{}) - // Start should not block. Do the actual work async. go p.run() return nil } - func (p *program) run() error { - logger.Infof("I'm running %v.", service.Platform()) - ticker := time.NewTicker(2 * time.Second) - for { - select { - case tm := <-ticker.C: - logger.Infof("Still running at %v...", tm) - case <-p.exit: - ticker.Stop() - return nil - } - } - return nil + func (p *program) run() { + // Do work here } func (p *program) Stop(s service.Service) error { - // Any work in Stop should be quick, usually a few seconds at most. - logger.Info("I'm Stopping!") - close(p.exit) + // Stop should not block. Return with a few seconds. return nil } - // Service setup. - // Define service config. - // Create the service. - // Setup the logger. - // Handle service controls (optional). - // Run the service. func main() { - svcFlag := flag.String("service", "", "Control the system service.") - flag.Parse() - svcConfig := &service.Config{ Name: "GoServiceTest", DisplayName: "Go Service Test", @@ -84,29 +50,10 @@ if err != nil { log.Fatal(err) } - errs := make(chan error, 5) - logger, err = s.Logger(errs) + logger, err = s.Logger(nil) if err != nil { log.Fatal(err) } - - go func() { - for { - err := <-errs - if err != nil { - log.Print(err) - } - } - }() - - if len(*svcFlag) != 0 { - err := service.Control(s, *svcFlag) - if err != nil { - log.Printf("Valid actions: %q\n", service.ControlAction) - log.Fatal(err) - } - return - } err = s.Run() if err != nil { logger.Error(err)