feat: creat pid folder automatically.

This commit is contained in:
Bo-Yi Wu 2017-01-10 17:13:12 +08:00
parent eddcb86470
commit d8c4659c2b
1 changed files with 11 additions and 4 deletions

15
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"strconv" "strconv"
"github.com/appleboy/gorush/config" "github.com/appleboy/gorush/config"
@ -65,19 +66,25 @@ func createPIDFile() error {
if !gorush.PushConf.Core.PID.Enabled { if !gorush.PushConf.Core.PID.Enabled {
return nil return nil
} }
_, err := os.Stat(gorush.PushConf.Core.PID.Path)
pidPath := gorush.PushConf.Core.PID.Path
_, err := os.Stat(pidPath)
if os.IsNotExist(err) || gorush.PushConf.Core.PID.Override { if os.IsNotExist(err) || gorush.PushConf.Core.PID.Override {
currentPid := os.Getpid() currentPid := os.Getpid()
file, err := os.Create(gorush.PushConf.Core.PID.Path) if err := os.MkdirAll(filepath.Dir(pidPath), os.ModePerm); err != nil {
return fmt.Errorf("Can't create PID folder on %v", err)
}
file, err := os.Create(pidPath)
if err != nil { if err != nil {
return fmt.Errorf("Can't create PID file: %v", err) return fmt.Errorf("Can't create PID file: %v", err)
} }
defer file.Close() defer file.Close()
if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil { if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil {
return fmt.Errorf("Can'write PID information on %s: %v", gorush.PushConf.Core.PID.Path, err) return fmt.Errorf("Can'write PID information on %s: %v", pidPath, err)
} }
} else { } else {
return fmt.Errorf("%s already exists", gorush.PushConf.Core.PID.Path) return fmt.Errorf("%s already exists", pidPath)
} }
return nil return nil
} }