diff --git a/daemon.go b/daemon.go index 4fb3b25..1e5fef6 100644 --- a/daemon.go +++ b/daemon.go @@ -1,7 +1,12 @@ package main import ( + "errors" + "fmt" + "io/ioutil" + "log" "os" + "os/exec" "strconv" ) @@ -26,3 +31,92 @@ func savePID(pidfile string, p int) error { return nil } + +func daemonStart(pidfile string) error { + // check if daemon already running. + if _, err := os.Stat(pidfile); err == nil { + return errors.New("sisyphus running or " + pidfile + " file exists.") + } + + cmd := exec.Command(os.Args[0], "run") + cmd.Start() + log.Printf("starting sisyphus process ID [%v]\n", cmd.Process.Pid) + log.Println("sisyphus started") + err := savePID(pidfile, cmd.Process.Pid) + if err != nil { + return err + } + + return nil +} + +func daemonStop(pidfile string) error { + + _, err := os.Stat(pidfile) + if err != nil { + return errors.New("sisyphus is not running") + } + + processIDRaw, err := ioutil.ReadFile(pidfile) + if err != nil { + return errors.New("sisyphus is not running") + } + + processID, err := strconv.Atoi(string(processIDRaw)) + if err != nil { + return errors.New("unable to read and parse process id found in " + pidfile) + } + + process, err := os.FindProcess(processID) + + if err != nil { + e := fmt.Sprintf("Unable to find process ID [%v] with error %v \n", processID, err) + return errors.New(e) + } + + // remove PID file + os.Remove(pidfile) + + log.Printf("stopping sisyphus process ID [%v]\n", processID) + // kill process and exit immediately + err = process.Kill() + + if err != nil { + e := fmt.Sprintf("Unable to kill process ID [%v] with error %v \n", processID, err) + return errors.New(e) + } + + log.Println("sisyphus stopped") + os.Exit(0) + + return nil +} + +func daemonRestart(pidfile string) error { + _, err := os.Stat(pidfile) + if err != nil { + return errors.New("sisyphus is not running") + } + + pid, err := ioutil.ReadFile(pidfile) + if err != nil { + return errors.New("sisyphus is not running") + } + + cmd := exec.Command(os.Args[0], "stop") + err = cmd.Start() + if err != nil { + return err + } + log.Printf("stopping sisyphus process ID [%v]\n", string(pid)) + + cmd = exec.Command(os.Args[0], "start") + err = cmd.Start() + if err != nil { + return err + } + + log.Println("sisyphus restarted") + + return nil +} diff --git a/main.go b/main.go index 9dcb8d4..69fccf6 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,10 @@ package main import ( - "fmt" - "io/ioutil" "log" "net/http" "os" - "os/exec" "os/signal" - "strconv" "syscall" "github.com/urfave/cli" @@ -129,18 +125,10 @@ func main() { Aliases: []string{"s"}, Usage: "start sisyphus daemon in the background", Action: func(c *cli.Context) error { - // check if daemon already running. - if _, err := os.Stat(*pidfile); err == nil { - return cli.NewExitError("sisyphus running or "+*pidfile+" file exists.", 69) - } - cmd := exec.Command(os.Args[0], "run") - cmd.Start() - log.Printf("starting sisyphus process ID [%v]\n", cmd.Process.Pid) - log.Println("sisyphus started") - err := savePID(*pidfile, cmd.Process.Pid) + err := daemonStart(*pidfile) if err != nil { - return cli.NewExitError(err, 73) + log.Fatal(err) } return nil @@ -152,43 +140,11 @@ func main() { Usage: "stop sisyphus daemon", Action: func(c *cli.Context) error { - _, err := os.Stat(*pidfile) - if err != nil { - return cli.NewExitError("sisyphus is not running", 64) - } - - processIDRaw, err := ioutil.ReadFile(*pidfile) - if err != nil { - return cli.NewExitError("sisyphus is not running", 64) - } - - processID, err := strconv.Atoi(string(processIDRaw)) - if err != nil { - return cli.NewExitError("unable to read and parse process id found in "+*pidfile, 74) - } - - process, err := os.FindProcess(processID) - - if err != nil { - e := fmt.Sprintf("Unable to find process ID [%v] with error %v \n", processID, err) - return cli.NewExitError(e, 71) - } - - // remove PID file - os.Remove(*pidfile) - - log.Printf("stopping sisyphus process ID [%v]\n", processID) - // kill process and exit immediately - err = process.Kill() - + err := daemonStop(*pidfile) if err != nil { - e := fmt.Sprintf("Unable to kill process ID [%v] with error %v \n", processID, err) - return cli.NewExitError(e, 71) + log.Fatal(err) } - log.Println("sisyphus stopped") - os.Exit(0) - return nil }, }, @@ -197,31 +153,12 @@ func main() { Aliases: []string{"r"}, Usage: "restart sisyphus daemon", Action: func(c *cli.Context) error { - _, err := os.Stat(*pidfile) - if err != nil { - return cli.NewExitError("sisyphus is not running", 64) - } - pid, err := ioutil.ReadFile(*pidfile) + err := daemonRestart(*pidfile) if err != nil { - return cli.NewExitError("sisyphus is not running", 64) + log.Fatal(err) } - cmd := exec.Command(os.Args[0], "stop") - err = cmd.Start() - if err != nil { - return cli.NewExitError(err, 64) - } - log.Printf("stopping sisyphus process ID [%v]\n", string(pid)) - - cmd = exec.Command(os.Args[0], "start") - err = cmd.Start() - if err != nil { - return cli.NewExitError(err, 64) - } - - log.Println("sisyphus restarted") - return nil }, },