Add configuration item: UUIDFile

pull/98/merge v0.3.0pre
Doflatango 2018-03-08 18:33:20 +08:00
parent 57870c360c
commit e45e2bb133
2 changed files with 20 additions and 4 deletions

View File

@ -1,9 +1,11 @@
package conf package conf
import ( import (
"errors"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"strings"
"time" "time"
client "github.com/coreos/etcd/clientv3" client "github.com/coreos/etcd/clientv3"
@ -53,6 +55,8 @@ type Conf struct {
Group string // 节点分组 Group string // 节点分组
Noticer string // 通知 Noticer string // 通知
UUIDFile string
Ttl int64 // 节点超时时间,单位秒 Ttl int64 // 节点超时时间,单位秒
ReqTimeout int // 请求超时时间,单位秒 ReqTimeout int // 请求超时时间,单位秒
// 执行任务信息过期时间,单位秒 // 执行任务信息过期时间,单位秒
@ -139,10 +143,16 @@ func cleanKeyPrefix(p string) string {
return p return p
} }
const UUID_FILE = "/etc/cronsun/CRONSUN_UUID" var errUUIDFilePathRequired = errors.New("the UUIDFile file path is required, see base.json.sample")
func (c *Conf) UUID() (string, error) { func (c *Conf) UUID() (string, error) {
b, err := ioutil.ReadFile(UUID_FILE) c.UUIDFile = strings.TrimSpace(c.UUIDFile)
if len(c.UUIDFile) == 0 {
return "", errUUIDFilePathRequired
}
c.UUIDFile = path.Clean(c.UUIDFile)
b, err := ioutil.ReadFile(c.UUIDFile)
if err == nil { if err == nil {
if len(b) == 0 { if len(b) == 0 {
return c.genUUID() return c.genUUID()
@ -163,7 +173,12 @@ func (c *Conf) genUUID() (string, error) {
return "", err return "", err
} }
err = ioutil.WriteFile(UUID_FILE, []byte(u.String()), 0600) uuidDir := path.Dir(c.UUIDFile)
if err := os.MkdirAll(uuidDir, 0755); err != nil {
return "", err
}
err = ioutil.WriteFile(c.UUIDFile, []byte(u.String()), 0600)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -20,5 +20,6 @@
"Etcd": "@extend:etcd.json", "Etcd": "@extend:etcd.json",
"Mgo": "@extend:db.json", "Mgo": "@extend:db.json",
"Mail": "@extend:mail.json", "Mail": "@extend:mail.json",
"Security": "@extend:security.json" "Security": "@extend:security.json",
"UUIDFile": "/etc/cronsun/CRONSUN_UUID"
} }