2
0
mirror of https://github.com/carlostrub/sisyphus synced 2024-11-13 13:10:40 +00:00
sisyphus/daemon.go

131 lines
2.7 KiB
Go
Raw Normal View History

2017-04-15 20:23:26 +00:00
package sisyphus
2017-03-14 20:27:05 +00:00
import (
"errors"
"fmt"
"io/ioutil"
2017-03-14 20:27:05 +00:00
"os"
"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
}
// DaemonStart starts sisyphus as a backgound process
2017-04-19 07:44:38 +00:00
func (p Pidfile) DaemonStart() error {
// check if daemon already running.
2017-04-19 07:44:38 +00:00
if _, err := os.Stat(string(p)); err == nil {
return errors.New("sisyphus running or " + string(p) + " file exists.")
}
cmd := exec.Command(os.Args[0], "run")
2017-04-26 11:20:21 +00:00
err := cmd.Start()
if err != nil {
return err
}
log.Printf("starting sisyphus process ID [%v]\n", cmd.Process.Pid)
log.Println("sisyphus started")
2017-04-26 11:20:21 +00:00
err = (p).savePID(cmd.Process.Pid)
2017-04-25 17:18:21 +00:00
return err
}
// DaemonStop stops a running sisyphus background process
2017-04-19 07:44:38 +00:00
func (p Pidfile) DaemonStop() error {
2017-04-19 07:44:38 +00:00
_, err := os.Stat(string(p))
if err != nil {
return errors.New("sisyphus is not running")
}
2017-04-19 07:44:38 +00:00
processIDRaw, err := ioutil.ReadFile(string(p))
if err != nil {
return errors.New("sisyphus is not running")
}
processID, err := strconv.Atoi(string(processIDRaw))
if err != nil {
2017-04-19 07:44:38 +00:00
return errors.New("unable to read and parse process id found in " + string(p))
}
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
2017-04-26 11:20:21 +00:00
err = os.Remove(string(p))
if err != nil {
return err
}
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
}
// DaemonRestart restarts a running sisyphus background process
2017-04-19 07:44:38 +00:00
func (p Pidfile) DaemonRestart() error {
_, err := os.Stat(string(p))
if err != nil {
return errors.New("sisyphus is not running")
}
2017-04-19 07:44:38 +00:00
pid, err := ioutil.ReadFile(string(p))
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
}