Skip to content

Commit

Permalink
Added the sample of a daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
sevlyar committed Feb 27, 2014
1 parent eaf845d commit a14bad1
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"flag"
"github.com/sevlyar/go-daemon"
"log"
"os"
"syscall"
"time"
)

var (
signal = flag.String("s", "", `send signal to the daemon
quit — graceful shutdown
stop — fast shutdown
reload — reloading the configuration file`)
)

func main() {
flag.Parse()
daemon.AddCommand(daemon.StringFlag(signal, "quit"), syscall.SIGQUIT, termHandler)
daemon.AddCommand(daemon.StringFlag(signal, "stop"), syscall.SIGTERM, termHandler)
daemon.AddCommand(daemon.StringFlag(signal, "reload"), syscall.SIGHUP, reloadHandler)

cntxt := &daemon.Context{
PidFileName: "pid",
PidFilePerm: 0644,
LogFileName: "log",
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
Args: []string{"[go-daemon sample]"},
}

if len(daemon.ActiveFlags()) > 0 {
d, err := cntxt.Search()
if err != nil {
log.Fatalln("Unable send signal to the daemon:", err)
}
daemon.SendCommands(d)
return
}

d, err := cntxt.Reborn()
if err != nil {
log.Fatalln(err)
}
if d != nil {
return
}
defer cntxt.Release()

log.Println("- - - - - - - - - - - - - - -")
log.Println("daemon started")

go worker()

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

var (
stop = make(chan struct{})
done = make(chan struct{})
)

func worker() {
for {
time.Sleep(time.Second)
if _, ok := <-stop; ok {
break
}
}
done <- struct{}{}
}

func termHandler(sig os.Signal) error {
log.Println("terminating...")
stop <- struct{}{}
if sig == syscall.SIGQUIT {
<-done
}
return daemon.ErrStop
}

func reloadHandler(sig os.Signal) error {
log.Println("configuration reloaded")
return nil
}

0 comments on commit a14bad1

Please sign in to comment.