node: 启动流程

pull/1/head
miraclesu 2017-01-05 10:37:48 +08:00
parent b494d87144
commit 5302689406
4 changed files with 56 additions and 12 deletions

View File

@ -8,15 +8,14 @@ import (
"sunteng/commons/event"
"sunteng/commons/log"
"sunteng/commons/util"
"sunteng/cronsun/conf"
"sunteng/cronsun/node"
)
var (
gomax = flag.Int("gomax",
4, "GOMAXPROCS: the max number of operating system threads that can execute")
localIp = "cronsun_node"
)
func main() {
@ -29,15 +28,22 @@ func main() {
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()
n, err := node.NewNode(conf.Config.Etcd)
if err != nil {
log.Error(err.Error())
return
}
log.Noticef("cronsun node[%s] service started, Ctrl+C or send kill sign to exit", localIp)
if err = n.Register(); err != nil {
log.Error(err.Error())
return
}
go n.Run()
log.Noticef("cronsun node[%s] pid[%d] service started, Ctrl+C or send kill sign to exit", n.IP, n.PID)
// 注册退出事件
event.On(event.EXIT)
event.On(event.EXIT, n.Stop)
// 监听退出信号
event.Wait()
// 处理退出事件

View File

@ -35,6 +35,9 @@ func Init() error {
type Conf struct {
Root string // 项目根目录
Proc string // proc 路径
Cmd string // cmd 路径
Log log.Config
Etcd client.Config
}

View File

@ -1,4 +1,6 @@
{
"Proc": "/cronsun/proc",
"Cmd": "cronsun/cmd",
"Log": "@extend:log.json",
"Etcd": "@extend:etcd.json"
}

View File

@ -1,21 +1,44 @@
package node
import (
"os"
client "github.com/coreos/etcd/clientv3"
"sunteng/commons/util"
)
// Node 执行 cron 命令服务的结构体
type Node struct {
*client.Client
IP string
PID int
}
func NewNode(cfg client.Config) *Node {
return &Node{}
func NewNode(cfg client.Config) (n *Node, err error) {
ip, err := util.GetLocalIP()
if err != nil {
return
}
cli, err := client.New(cfg)
if err != nil {
return
}
n = &Node{
Client: cli,
IP: ip.String(),
PID: os.Getpid(),
}
return
}
// 注册到 /cronsun/proc/xx
func (n *Node) Register() {
func (n *Node) Register() error {
return nil
}
// 更新 /cronsun/proc/xx/time
@ -23,3 +46,13 @@ func (n *Node) Register() {
func (n *Node) Heartbeat() {
}
// 启动服务
func (n *Node) Run() {
}
// 启动服务
func (n *Node) Stop(i interface{}) {
n.Client.Close()
}