Skip to content

Commit

Permalink
service: move to v2 API (beta).
Browse files Browse the repository at this point in the history
  • Loading branch information
kardianos committed Jan 13, 2015
1 parent 6d50620 commit 8b12f7c
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 604 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012 Daniel Theophanes
Copyright (c) 2015 Daniel Theophanes

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
Expand Down
170 changes: 0 additions & 170 deletions config/config.go

This file was deleted.

44 changes: 44 additions & 0 deletions console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package service

import (
"log"
"os"
)

// ConsoleLogger logs to the std err.
var ConsoleLogger = consoleLogger{}

type consoleLogger struct {
info, warn, err *log.Logger
}

func init() {
ConsoleLogger.info = log.New(os.Stderr, "I: ", log.Ltime)
ConsoleLogger.warn = log.New(os.Stderr, "W: ", log.Ltime)
ConsoleLogger.err = log.New(os.Stderr, "E: ", log.Ltime)
}

func (c consoleLogger) Error(v ...interface{}) error {
c.err.Print(v...)
return nil
}
func (c consoleLogger) Warning(v ...interface{}) error {
c.warn.Print(v...)
return nil
}
func (c consoleLogger) Info(v ...interface{}) error {
c.info.Print(v...)
return nil
}
func (c consoleLogger) Errorf(format string, a ...interface{}) error {
c.err.Printf(format, a...)
return nil
}
func (c consoleLogger) Warningf(format string, a ...interface{}) error {
c.warn.Printf(format, a...)
return nil
}
func (c consoleLogger) Infof(format string, a ...interface{}) error {
c.info.Printf(format, a...)
return nil
}
Binary file added example/example
Binary file not shown.
130 changes: 58 additions & 72 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,95 +1,81 @@
package main

import (
"fmt"
"log"
"os"
"time"

"bitbucket.org/kardianos/service"
"bitbucket.org/kardianos/service2beta"
)

var log service.Logger
var logger service.Logger

func main() {
var name = "GoServiceTest"
var displayName = "Go Service Test"
var desc = "This is a test Go service. It is designed to run well."

var s, err = service.NewService(name, displayName, desc)
log = s
type program struct {
exit chan struct{}
}

if err != nil {
fmt.Printf("%s unable to start: %s", displayName, err)
return
func (p *program) Start(s service.Service) error {
if s.Interactive() {
logger.Info("Running in terminal.")
} else {
logger.Info("Running under service manager.")
}

if len(os.Args) > 1 {
var err error
verb := os.Args[1]
switch verb {
case "install":
err = s.Install()
if err != nil {
fmt.Printf("Failed to install: %s\n", err)
return
}
fmt.Printf("Service \"%s\" installed.\n", displayName)
case "remove":
err = s.Remove()
if err != nil {
fmt.Printf("Failed to remove: %s\n", err)
return
}
fmt.Printf("Service \"%s\" removed.\n", displayName)
case "run":
doWork()
case "start":
err = s.Start()
if err != nil {
fmt.Printf("Failed to start: %s\n", err)
return
}
fmt.Printf("Service \"%s\" started.\n", displayName)
case "stop":
err = s.Stop()
p.exit = make(chan struct{})
go p.run()
return nil
}
func (p *program) run() error {
logger.Infof("I'm running %v.", service.LocalSystem())
ticker := time.NewTicker(2 * time.Second)
for {
select {
case tm := <-ticker.C:
err := logger.Infof("Still running at %v...", tm)
if err != nil {
fmt.Printf("Failed to stop: %s\n", err)
return
panic(err)
}
fmt.Printf("Service \"%s\" stopped.\n", displayName)
case <-p.exit:
ticker.Stop()
return nil
}
return
}
err = s.Run(func() error {
// start
go doWork()
return nil
}, func() error {
// stop
stopWork()
return nil
})
return nil
}
func (p *program) Stop(s service.Service) error {
err := logger.Info("I'm Stopping!")
if err != nil {
s.Error(err.Error())
panic(err)
}
close(p.exit)
return nil
}

var exit = make(chan struct{})
func main() {
svcConfig := &service.Config{
Name: "GoServiceTest",
DisplayName: "Go Service Test",
Description: "This is a test Go service.",
}

func doWork() {
log.Info("I'm Running!")
ticker := time.NewTicker(time.Minute)
for {
select {
case <-ticker.C:
log.Info("Still running...")
case <-exit:
ticker.Stop()
return
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
panic(err)
}
logger, err = s.SystemLogger()
if err != nil {
panic(err)
}

if len(os.Args) > 1 {
err := service.Control(s, os.Args[1])
if err != nil {
log.Fatal(err)
}
return
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}
func stopWork() {
log.Info("I'm Stopping!")
exit <- struct{}{}
}
Loading

0 comments on commit 8b12f7c

Please sign in to comment.