mirror of https://github.com/shunfei/cronsun
Write pid to file, resolve #79
parent
1848f7169f
commit
40538f0527
|
@ -63,6 +63,8 @@ type Conf struct {
|
|||
// 默认 300
|
||||
LockTtl int64
|
||||
|
||||
PIDFile string
|
||||
|
||||
Etcd client.Config
|
||||
Mgo *db.Config
|
||||
Web *webConfig
|
||||
|
|
|
@ -20,5 +20,6 @@
|
|||
"Etcd": "@extend:etcd.json",
|
||||
"Mgo": "@extend:db.json",
|
||||
"Mail": "@extend:mail.json",
|
||||
"Security": "@extend:security.json"
|
||||
"Security": "@extend:security.json",
|
||||
"PIDFile": "/tmp/cronsun/cronnode_pid"
|
||||
}
|
||||
|
|
5
node.go
5
node.go
|
@ -23,8 +23,9 @@ const (
|
|||
// 执行 cron cmd 的进程
|
||||
// 注册到 /cronsun/node/<id>
|
||||
type Node struct {
|
||||
ID string `bson:"_id" json:"id"` // ip
|
||||
PID string `bson:"pid" json:"pid"` // 进程 pid
|
||||
ID string `bson:"_id" json:"id"` // ip
|
||||
PID string `bson:"pid" json:"pid"` // 进程 pid
|
||||
PIDFile string `bson:"-" json:"-"`
|
||||
|
||||
Version string `bson:"version" json:"version"`
|
||||
UpTime time.Time `bson:"up" json:"up"` // 启动时间
|
||||
|
|
35
node/node.go
35
node/node.go
|
@ -2,8 +2,11 @@ package node
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
client "github.com/coreos/etcd/clientv3"
|
||||
|
@ -43,8 +46,9 @@ func NewNode(cfg *conf.Conf) (n *Node, err error) {
|
|||
n = &Node{
|
||||
Client: cronsun.DefalutClient,
|
||||
Node: &cronsun.Node{
|
||||
ID: ip.String(),
|
||||
PID: strconv.Itoa(os.Getpid()),
|
||||
ID: ip.String(),
|
||||
PID: strconv.Itoa(os.Getpid()),
|
||||
PIDFile: strings.TrimSpace(cfg.PIDFile),
|
||||
},
|
||||
Cron: cron.New(),
|
||||
|
||||
|
@ -85,9 +89,36 @@ func (n *Node) set() error {
|
|||
}
|
||||
|
||||
n.lID = resp.ID
|
||||
n.writePIDFile()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Node) writePIDFile() {
|
||||
if len(n.PIDFile) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
filename := "cronnode_pid"
|
||||
if !strings.HasSuffix(n.PIDFile, "/") {
|
||||
filename = path.Base(n.PIDFile)
|
||||
}
|
||||
|
||||
dir := path.Dir(n.PIDFile)
|
||||
err := os.MkdirAll(dir, 0755)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to write pid file: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
n.PIDFile = path.Join(dir, filename)
|
||||
err = ioutil.WriteFile(n.PIDFile, []byte(n.PID), 0600)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to write pid file: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 断网掉线重新注册
|
||||
func (n *Node) keepAlive() {
|
||||
duration := time.Duration(n.ttl) * time.Second
|
||||
|
|
Loading…
Reference in New Issue