Skip to content

Commit

Permalink
node: 启动流程
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclesu committed May 9, 2017
1 parent b494d87 commit 5302689
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
22 changes: 14 additions & 8 deletions bin/node/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
// 处理退出事件
Expand Down
3 changes: 3 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func Init() error {
type Conf struct {
Root string // 项目根目录

Proc string // proc 路径
Cmd string // cmd 路径

Log log.Config
Etcd client.Config
}
2 changes: 2 additions & 0 deletions conf/files/base.json.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"Proc": "/cronsun/proc",
"Cmd": "cronsun/cmd",
"Log": "@extend:log.json",
"Etcd": "@extend:etcd.json"
}
41 changes: 37 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
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
// 用于检查 node 是否存活
func (n *Node) Heartbeat() {

}

// 启动服务
func (n *Node) Run() {

}

// 启动服务
func (n *Node) Stop(i interface{}) {
n.Client.Close()
}

0 comments on commit 5302689

Please sign in to comment.