From bf839693b97cb19a587b25649bbcb59ee16bdb2f Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Fri, 19 Apr 2019 14:36:41 +0300 Subject: [PATCH] Minor improvements and corrections for examples --- examples/cmd/gd-log-rotation/main.go | 19 ++++++++++------- .../cmd/gd-signal-handling/signal-handling.go | 21 ++++++++++--------- examples/cmd/gd-simple/simple.go | 7 +++---- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/examples/cmd/gd-log-rotation/main.go b/examples/cmd/gd-log-rotation/main.go index f05b99c..684f9f8 100644 --- a/examples/cmd/gd-log-rotation/main.go +++ b/examples/cmd/gd-log-rotation/main.go @@ -10,18 +10,19 @@ import ( ) var ( - signal = flag.String("s", "", `send signal to the daemon - stop — shutdown`) + signal = flag.String("s", "", `Send signal to the daemon: + stop — shutdown`) ) -const logFileName = "log" +const logFileName = "sample.log" +const pidFileName = "sample.pid" func main() { flag.Parse() daemon.AddCommand(daemon.StringFlag(signal, "stop"), syscall.SIGTERM, termHandler) cntxt := &daemon.Context{ - PidFileName: "pid", + PidFileName: pidFileName, PidFilePerm: 0644, LogFileName: logFileName, LogFilePerm: 0640, @@ -33,8 +34,9 @@ func main() { if len(daemon.ActiveFlags()) > 0 { d, err := cntxt.Search() if err != nil { - log.Fatalln("Unable send signal to the daemon:", err) + log.Fatalf("Unable send signal to the daemon: %s", err.Error()) } + daemon.SendCommands(d) return } @@ -57,7 +59,7 @@ func main() { err = daemon.ServeSignals() if err != nil { - log.Println("Error:", err) + log.Printf("Error: %s", err.Error()) } log.Println("daemon terminated") } @@ -65,8 +67,9 @@ func main() { func setupLog() { lf, err := NewLogFile(logFileName, os.Stderr) if err != nil { - log.Fatal("Unable to create log file: ", err) + log.Fatalf("Unable to create log file: %s", err.Error()) } + log.SetOutput(lf) // rotate log every 30 seconds. rotateLogSignal := time.Tick(30 * time.Second) @@ -74,7 +77,7 @@ func setupLog() { for { <-rotateLogSignal if err := lf.Rotate(); err != nil { - log.Fatal("Unable to rotate log: ", err) + log.Fatalf("Unable to rotate log: %s", err.Error()) } } }() diff --git a/examples/cmd/gd-signal-handling/signal-handling.go b/examples/cmd/gd-signal-handling/signal-handling.go index 203f2b5..3697206 100644 --- a/examples/cmd/gd-signal-handling/signal-handling.go +++ b/examples/cmd/gd-signal-handling/signal-handling.go @@ -10,10 +10,10 @@ import ( ) var ( - signal = flag.String("s", "", `send signal to the daemon - quit — graceful shutdown - stop — fast shutdown - reload — reloading the configuration file`) + signal = flag.String("s", "", `Send signal to the daemon: + quit — graceful shutdown + stop — fast shutdown + reload — reloading the configuration file`) ) func main() { @@ -23,9 +23,9 @@ func main() { daemon.AddCommand(daemon.StringFlag(signal, "reload"), syscall.SIGHUP, reloadHandler) cntxt := &daemon.Context{ - PidFileName: "pid", + PidFileName: "sample.pid", PidFilePerm: 0644, - LogFileName: "log", + LogFileName: "sample.log", LogFilePerm: 0640, WorkDir: "./", Umask: 027, @@ -35,7 +35,7 @@ func main() { if len(daemon.ActiveFlags()) > 0 { d, err := cntxt.Search() if err != nil { - log.Fatalln("Unable send signal to the daemon:", err) + log.Fatalf("Unable send signal to the daemon: %s", err.Error()) } daemon.SendCommands(d) return @@ -57,8 +57,9 @@ func main() { err = daemon.ServeSignals() if err != nil { - log.Println("Error:", err) + log.Printf("Error: %s", err.Error()) } + log.Println("daemon terminated") } @@ -68,11 +69,11 @@ var ( ) func worker() { - LOOP: +LOOP: for { time.Sleep(time.Second) // this is work to be done by worker. select { - case <- stop: + case <-stop: break LOOP default: } diff --git a/examples/cmd/gd-simple/simple.go b/examples/cmd/gd-simple/simple.go index 49f43f6..cad0d33 100644 --- a/examples/cmd/gd-simple/simple.go +++ b/examples/cmd/gd-simple/simple.go @@ -10,13 +10,12 @@ import ( ) // To terminate the daemon use: -// kill `cat pid` - +// kill `cat sample.pid` func main() { cntxt := &daemon.Context{ - PidFileName: "pid", + PidFileName: "sample.pid", PidFilePerm: 0644, - LogFileName: "log", + LogFileName: "sample.log", LogFilePerm: 0640, WorkDir: "./", Umask: 027,