init commit

pull/1/head
miraclesu 2017-01-04 17:27:36 +08:00
parent 54bdab8baf
commit b494d87144
7 changed files with 146 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
conf/files/*.json
.tags
.tags_sorted_by_file
bin/*/*server
.DS_Store

46
bin/node/server.go Normal file
View File

@ -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")
}

40
conf/conf.go Normal file
View File

@ -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
}

View File

@ -0,0 +1,4 @@
{
"Log": "@extend:log.json",
"Etcd": "@extend:etcd.json"
}

View File

@ -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
}

View File

@ -0,0 +1,15 @@
{
"Appenders":{
"Node":{
"Type":"file",
"Target":"/data/logs/cronsun/node.log"
},
"stdout":{
"Type":"console"
}
},
"Root":{
"Level":"DEBUG",
"Appenders":["stdout", "Node"]
}
}

25
node/node.go Normal file
View File

@ -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() {
}