forked from openp2p-cn/service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
516 additions
and
604 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}{} | ||
} |
Oops, something went wrong.