2
0
mirror of https://github.com/carlostrub/sisyphus synced 2024-10-31 09:20:15 +00:00

move daemon stuff out of main into daemon source file

This commit is contained in:
Carlo Strub 2017-03-14 21:31:28 +00:00
parent f1ac61d5ae
commit 029e8b2457
2 changed files with 100 additions and 69 deletions

View File

@ -1,7 +1,12 @@
package main package main
import ( import (
"errors"
"fmt"
"io/ioutil"
"log"
"os" "os"
"os/exec"
"strconv" "strconv"
) )
@ -26,3 +31,92 @@ func savePID(pidfile string, p int) error {
return nil 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
}

75
main.go
View File

@ -1,14 +1,10 @@
package main package main
import ( import (
"fmt"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"os/signal" "os/signal"
"strconv"
"syscall" "syscall"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -129,18 +125,10 @@ func main() {
Aliases: []string{"s"}, Aliases: []string{"s"},
Usage: "start sisyphus daemon in the background", Usage: "start sisyphus daemon in the background",
Action: func(c *cli.Context) error { 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") err := daemonStart(*pidfile)
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 { if err != nil {
return cli.NewExitError(err, 73) log.Fatal(err)
} }
return nil return nil
@ -152,43 +140,11 @@ func main() {
Usage: "stop sisyphus daemon", Usage: "stop sisyphus daemon",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
_, err := os.Stat(*pidfile) err := daemonStop(*pidfile)
if err != nil { if err != nil {
return cli.NewExitError("sisyphus is not running", 64) log.Fatal(err)
} }
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()
if err != nil {
e := fmt.Sprintf("Unable to kill process ID [%v] with error %v \n", processID, err)
return cli.NewExitError(e, 71)
}
log.Println("sisyphus stopped")
os.Exit(0)
return nil return nil
}, },
}, },
@ -197,31 +153,12 @@ func main() {
Aliases: []string{"r"}, Aliases: []string{"r"},
Usage: "restart sisyphus daemon", Usage: "restart sisyphus daemon",
Action: func(c *cli.Context) error { 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 { 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 return nil
}, },
}, },