diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5867049 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +conf/files/*.json +.tags +.tags_sorted_by_file +bin/*/*server +.DS_Store diff --git a/bin/node/server.go b/bin/node/server.go new file mode 100644 index 0000000..a794c97 --- /dev/null +++ b/bin/node/server.go @@ -0,0 +1,46 @@ +// node 服务 +// 用于在所需要执行 cron 任务的机器启动服务,替代 cron 执行所需的任务 +package main + +import ( + "flag" + "runtime" + + "sunteng/commons/event" + "sunteng/commons/log" + "sunteng/commons/util" + + "sunteng/cronsun/conf" +) + +var ( + gomax = flag.Int("gomax", + 4, "GOMAXPROCS: the max number of operating system threads that can execute") + localIp = "cronsun_node" +) + +func main() { + flag.Parse() + //set cpu usage + runtime.GOMAXPROCS(*gomax) + + if err := conf.Init(); err != nil { + log.Error(err.Error()) + return + } + + if ip, err := util.GetLocalIP(); err != nil { + log.Errorf("local ip error, node init may be fail, error: %s", err.Error()) + } else { + localIp = ip.String() + } + + log.Noticef("cronsun node[%s] service started, Ctrl+C or send kill sign to exit", localIp) + // 注册退出事件 + event.On(event.EXIT) + // 监听退出信号 + event.Wait() + // 处理退出事件 + event.Emit(event.EXIT, nil) + log.Notice("exit success") +} diff --git a/conf/conf.go b/conf/conf.go new file mode 100644 index 0000000..e306976 --- /dev/null +++ b/conf/conf.go @@ -0,0 +1,40 @@ +package conf + +import ( + "path" + "time" + + client "github.com/coreos/etcd/clientv3" + + "sunteng/commons/confutil" + "sunteng/commons/log" + "sunteng/commons/util" +) + +var ( + Config = new(Conf) +) + +func Init() error { + Config.Root = util.CurDir() + + confFile := path.Join(Config.Root, "files", "base.json") + err := confutil.LoadExtendConf(confFile, Config) + if err != nil { + return err + } + + if Config.Etcd.DialTimeout > 0 { + Config.Etcd.DialTimeout *= time.Second + } + log.InitConf(&Config.Log) + + return nil +} + +type Conf struct { + Root string // 项目根目录 + + Log log.Config + Etcd client.Config +} diff --git a/conf/files/base.json.sample b/conf/files/base.json.sample new file mode 100644 index 0000000..f7c4d3b --- /dev/null +++ b/conf/files/base.json.sample @@ -0,0 +1,4 @@ +{ + "Log": "@extend:log.json", + "Etcd": "@extend:etcd.json" +} diff --git a/conf/files/etcd.json.sample b/conf/files/etcd.json.sample new file mode 100644 index 0000000..95a5b80 --- /dev/null +++ b/conf/files/etcd.json.sample @@ -0,0 +1,11 @@ +{ + "Endpoints":[ + "http://192.168.11.27:2379", + "http://192.168.11.28:2379", + "http://192.168.11.29:2379" + ], + "Username":"", + "Password":"", + "#DialTimeout":"单位秒", + "DialTimeout": 5 +} diff --git a/conf/files/log.json.sample b/conf/files/log.json.sample new file mode 100644 index 0000000..00d4eae --- /dev/null +++ b/conf/files/log.json.sample @@ -0,0 +1,15 @@ +{ + "Appenders":{ + "Node":{ + "Type":"file", + "Target":"/data/logs/cronsun/node.log" + }, + "stdout":{ + "Type":"console" + } + }, + "Root":{ + "Level":"DEBUG", + "Appenders":["stdout", "Node"] + } +} diff --git a/node/node.go b/node/node.go new file mode 100644 index 0000000..b4b0e16 --- /dev/null +++ b/node/node.go @@ -0,0 +1,25 @@ +package node + +import ( + client "github.com/coreos/etcd/clientv3" +) + +// Node 执行 cron 命令服务的结构体 +type Node struct { + *client.Client +} + +func NewNode(cfg client.Config) *Node { + return &Node{} +} + +// 注册到 /cronsun/proc/xx +func (n *Node) Register() { + +} + +// 更新 /cronsun/proc/xx/time +// 用于检查 node 是否存活 +func (n *Node) Heartbeat() { + +}