Skip to content

Commit

Permalink
Add configuration item: UUIDFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Doflatango committed Mar 8, 2018
1 parent 57870c3 commit e45e2bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 18 additions & 3 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package conf

import (
"errors"
"io/ioutil"
"os"
"path"
"strings"
"time"

client "github.com/coreos/etcd/clientv3"
Expand Down Expand Up @@ -53,6 +55,8 @@ type Conf struct {
Group string // 节点分组
Noticer string // 通知

UUIDFile string

Ttl int64 // 节点超时时间,单位秒
ReqTimeout int // 请求超时时间,单位秒
// 执行任务信息过期时间,单位秒
Expand Down Expand Up @@ -139,10 +143,16 @@ func cleanKeyPrefix(p string) string {
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) {
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 len(b) == 0 {
return c.genUUID()
Expand All @@ -163,7 +173,12 @@ func (c *Conf) genUUID() (string, error) {
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 {
return "", err
}
Expand Down
3 changes: 2 additions & 1 deletion conf/files/base.json.sample
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"Etcd": "@extend:etcd.json",
"Mgo": "@extend:db.json",
"Mail": "@extend:mail.json",
"Security": "@extend:security.json"
"Security": "@extend:security.json",
"UUIDFile": "/etc/cronsun/CRONSUN_UUID"
}

0 comments on commit e45e2bb

Please sign in to comment.