Skip to content

Commit

Permalink
Minor improvements and corrections for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay authored and sevlyar committed May 4, 2019
1 parent 1c1036e commit bf83969
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
19 changes: 11 additions & 8 deletions examples/cmd/gd-log-rotation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -57,24 +59,25 @@ func main() {

err = daemon.ServeSignals()
if err != nil {
log.Println("Error:", err)
log.Printf("Error: %s", err.Error())
}
log.Println("daemon terminated")
}

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)
go func() {
for {
<-rotateLogSignal
if err := lf.Rotate(); err != nil {
log.Fatal("Unable to rotate log: ", err)
log.Fatalf("Unable to rotate log: %s", err.Error())
}
}
}()
Expand Down
21 changes: 11 additions & 10 deletions examples/cmd/gd-signal-handling/signal-handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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")
}

Expand All @@ -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:
}
Expand Down
7 changes: 3 additions & 4 deletions examples/cmd/gd-simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bf83969

Please sign in to comment.