2017-04-15 20:23:26 +00:00
|
|
|
package sisyphus
|
2017-03-14 20:27:05 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-14 21:31:28 +00:00
|
|
|
"io/ioutil"
|
2017-03-14 20:27:05 +00:00
|
|
|
"os"
|
2017-03-14 21:31:28 +00:00
|
|
|
"os/exec"
|
2017-03-14 20:27:05 +00:00
|
|
|
"strconv"
|
2017-05-18 19:21:46 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2017-03-14 20:27:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// See
|
|
|
|
// https://www.socketloop.com/tutorials/golang-daemonizing-a-simple-web-server-process-example
|
|
|
|
// for the process we are using to daemonize
|
|
|
|
|
2017-04-19 07:44:38 +00:00
|
|
|
// Pidfile holds the Process ID file of sisyphus
|
|
|
|
type Pidfile string
|
|
|
|
|
2017-03-14 20:27:05 +00:00
|
|
|
// savePID stores a pidfile
|
2017-04-19 07:44:38 +00:00
|
|
|
func (p Pidfile) savePID(process int) error {
|
|
|
|
file, err := os.Create(string(p))
|
2017-03-14 20:27:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2017-04-19 07:44:38 +00:00
|
|
|
_, err = file.WriteString(strconv.Itoa(process))
|
2017-03-14 20:27:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 11:20:21 +00:00
|
|
|
err = file.Sync()
|
2017-03-14 20:27:05 +00:00
|
|
|
|
2017-04-26 11:20:21 +00:00
|
|
|
return err
|
2017-03-14 20:27:05 +00:00
|
|
|
}
|
2017-03-14 21:31:28 +00:00
|
|
|
|
2017-04-17 21:13:18 +00:00
|
|
|
// DaemonStart starts sisyphus as a backgound process
|
2017-05-23 20:43:17 +00:00
|
|
|
func (p Pidfile) DaemonStart() {
|
2017-03-14 21:31:28 +00:00
|
|
|
// check if daemon already running.
|
2017-04-19 07:44:38 +00:00
|
|
|
if _, err := os.Stat(string(p)); err == nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"pidfile": p,
|
|
|
|
}).Fatal("Already running or pidfile exists")
|
|
|
|
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Info("Starting sisyphus daemon")
|
2017-03-14 21:31:28 +00:00
|
|
|
cmd := exec.Command(os.Args[0], "run")
|
2017-05-23 20:43:17 +00:00
|
|
|
cmd.Start()
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"pid": cmd.Process.Pid,
|
|
|
|
}).Info("Sisyphus started")
|
|
|
|
err := (p).savePID(cmd.Process.Pid)
|
2017-04-26 11:20:21 +00:00
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Warning("Save process ID file")
|
2017-04-26 11:20:21 +00:00
|
|
|
}
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"pidfile": p,
|
|
|
|
}).Info("Process ID file stored")
|
2017-03-14 21:31:28 +00:00
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
return
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 21:13:18 +00:00
|
|
|
// DaemonStop stops a running sisyphus background process
|
2017-05-23 20:43:17 +00:00
|
|
|
func (p Pidfile) DaemonStop() {
|
2017-03-14 21:31:28 +00:00
|
|
|
|
2017-04-19 07:44:38 +00:00
|
|
|
_, err := os.Stat(string(p))
|
2017-03-14 21:31:28 +00:00
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Fatal("Sisyphus is not running")
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 07:44:38 +00:00
|
|
|
processIDRaw, err := ioutil.ReadFile(string(p))
|
2017-03-14 21:31:28 +00:00
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Fatal("Sisyphus is not running")
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
processID, err := strconv.Atoi(string(processIDRaw))
|
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"pid": p,
|
|
|
|
}).Fatal("Unable to read process ID")
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
process, err := os.FindProcess(processID)
|
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"pid": p,
|
|
|
|
"err": err,
|
|
|
|
}).Fatal("Unable to find process ID")
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove PID file
|
2017-04-26 11:20:21 +00:00
|
|
|
err = os.Remove(string(p))
|
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Warning("Unable to remove process ID file")
|
2017-04-26 11:20:21 +00:00
|
|
|
}
|
2017-03-14 21:31:28 +00:00
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"pid": processID,
|
|
|
|
}).Info("Stopping sisyphus process")
|
2017-03-14 21:31:28 +00:00
|
|
|
// kill process and exit immediately
|
|
|
|
err = process.Kill()
|
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"pid": processID,
|
|
|
|
"err": err,
|
|
|
|
}).Fatal("Unable to kill sisyphus process")
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Info("Sisyphus stopped")
|
2017-03-14 21:31:28 +00:00
|
|
|
os.Exit(0)
|
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
return
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 21:13:18 +00:00
|
|
|
// DaemonRestart restarts a running sisyphus background process
|
2017-05-23 20:43:17 +00:00
|
|
|
func (p Pidfile) DaemonRestart() {
|
2017-04-19 07:44:38 +00:00
|
|
|
_, err := os.Stat(string(p))
|
2017-03-14 21:31:28 +00:00
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Fatal("Sisyphus not running")
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 07:44:38 +00:00
|
|
|
pid, err := ioutil.ReadFile(string(p))
|
2017-03-14 21:31:28 +00:00
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Fatal("Sisyphus not running")
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
2017-05-23 20:55:47 +00:00
|
|
|
"pid": string(pid),
|
2017-05-23 20:43:17 +00:00
|
|
|
}).Info("Stopping sisyphus process")
|
2017-03-14 21:31:28 +00:00
|
|
|
cmd := exec.Command(os.Args[0], "stop")
|
2017-05-23 20:43:17 +00:00
|
|
|
cmd.Start()
|
2017-03-14 21:31:28 +00:00
|
|
|
|
|
|
|
cmd = exec.Command(os.Args[0], "start")
|
2017-05-23 20:43:17 +00:00
|
|
|
cmd.Start()
|
2017-03-14 21:31:28 +00:00
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Info("Sisyphus restarted")
|
2017-03-14 21:31:28 +00:00
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
return
|
2017-03-14 21:31:28 +00:00
|
|
|
}
|