service/example: gofmt -s

This commit is contained in:
Daniel Theophanes
2015-05-01 13:47:05 -07:00
parent b7229667de
commit 87e1886ce4
2 changed files with 156 additions and 156 deletions
+105 -105
View File
@@ -1,105 +1,105 @@
// Copyright 2015 Daniel Theophanes. // Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style // Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.package service // license that can be found in the LICENSE file.package service
// Simple service that only works by printing a log message every few seconds. // Simple service that only works by printing a log message every few seconds.
package main package main
import ( import (
"flag" "flag"
"log" "log"
"time" "time"
"github.com/kardianos/service" "github.com/kardianos/service"
) )
var logger service.Logger var logger service.Logger
// Program structures. // Program structures.
// Define Start and Stop methods. // Define Start and Stop methods.
type program struct { type program struct {
exit chan struct{} exit chan struct{}
} }
func (p *program) Start(s service.Service) error { func (p *program) Start(s service.Service) error {
if service.Interactive() { if service.Interactive() {
logger.Info("Running in terminal.") logger.Info("Running in terminal.")
} else { } else {
logger.Info("Running under service manager.") logger.Info("Running under service manager.")
} }
p.exit = make(chan struct{}) p.exit = make(chan struct{})
// Start should not block. Do the actual work async. // Start should not block. Do the actual work async.
go p.run() go p.run()
return nil return nil
} }
func (p *program) run() error { func (p *program) run() error {
logger.Infof("I'm running %v.", service.Platform()) logger.Infof("I'm running %v.", service.Platform())
ticker := time.NewTicker(2 * time.Second) ticker := time.NewTicker(2 * time.Second)
for { for {
select { select {
case tm := <-ticker.C: case tm := <-ticker.C:
logger.Infof("Still running at %v...", tm) logger.Infof("Still running at %v...", tm)
case <-p.exit: case <-p.exit:
ticker.Stop() ticker.Stop()
return nil return nil
} }
} }
return nil return nil
} }
func (p *program) Stop(s service.Service) error { func (p *program) Stop(s service.Service) error {
// Any work in Stop should be quick, usually a few seconds at most. // Any work in Stop should be quick, usually a few seconds at most.
logger.Info("I'm Stopping!") logger.Info("I'm Stopping!")
close(p.exit) close(p.exit)
return nil return nil
} }
// Service setup. // Service setup.
// Define service config. // Define service config.
// Create the service. // Create the service.
// Setup the logger. // Setup the logger.
// Handle service controls (optional). // Handle service controls (optional).
// Run the service. // Run the service.
func main() { func main() {
svcFlag := flag.String("service", "", "Control the system service.") svcFlag := flag.String("service", "", "Control the system service.")
flag.Parse() flag.Parse()
svcConfig := &service.Config{ svcConfig := &service.Config{
Name: "GoServiceExampleLogging", Name: "GoServiceExampleLogging",
DisplayName: "Go Service Example for Logging", DisplayName: "Go Service Example for Logging",
Description: "This is an example Go service that outputs log messages.", Description: "This is an example Go service that outputs log messages.",
} }
prg := &program{} prg := &program{}
s, err := service.New(prg, svcConfig) s, err := service.New(prg, svcConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
errs := make(chan error, 5) errs := make(chan error, 5)
logger, err = s.Logger(errs) logger, err = s.Logger(errs)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
go func() { go func() {
for { for {
err := <-errs err := <-errs
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
} }
}() }()
if len(*svcFlag) != 0 { if len(*svcFlag) != 0 {
err := service.Control(s, *svcFlag) err := service.Control(s, *svcFlag)
if err != nil { if err != nil {
log.Printf("Valid actions: %q\n", service.ControlAction) log.Printf("Valid actions: %q\n", service.ControlAction)
log.Fatal(err) log.Fatal(err)
} }
return return
} }
err = s.Run() err = s.Run()
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
} }
} }
+51 -51
View File
@@ -1,51 +1,51 @@
// Copyright 2015 Daniel Theophanes. // Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style // Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.package service // license that can be found in the LICENSE file.package service
// simple does nothing except block while running the service. // simple does nothing except block while running the service.
package main package main
import ( import (
"log" "log"
"github.com/kardianos/service" "github.com/kardianos/service"
) )
var logger service.Logger var logger service.Logger
type program struct{} type program struct{}
func (p *program) Start(s service.Service) error { func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async. // Start should not block. Do the actual work async.
go p.run() go p.run()
return nil return nil
} }
func (p *program) run() { func (p *program) run() {
// Do work here // Do work here
} }
func (p *program) Stop(s service.Service) error { func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds. // Stop should not block. Return with a few seconds.
return nil return nil
} }
func main() { func main() {
svcConfig := &service.Config{ svcConfig := &service.Config{
Name: "GoServiceExampleSimple", Name: "GoServiceExampleSimple",
DisplayName: "Go Service Example", DisplayName: "Go Service Example",
Description: "This is an example Go service.", Description: "This is an example Go service.",
} }
prg := &program{} prg := &program{}
s, err := service.New(prg, svcConfig) s, err := service.New(prg, svcConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
logger, err = s.Logger(nil) logger, err = s.Logger(nil)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = s.Run() err = s.Run()
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
} }
} }