重构msg的包

This commit is contained in:
陈景阳
2019-09-10 09:32:48 +08:00
parent e5d33b8982
commit 9514a8a6af
14 changed files with 398 additions and 220 deletions

View File

@@ -0,0 +1,48 @@
package msg_handler
import (
"crawlab/constants"
"crawlab/model"
)
type Handler interface {
Handle() error
}
func GetMsgHandler(msg NodeMessage) Handler {
if msg.Type == constants.MsgTypeGetLog {
return &Log{
msg: msg,
}
} else if msg.Type == constants.MsgTypeCancelTask {
return &Task{
msg: msg,
}
} else if msg.Type == constants.MsgTypeGetSystemInfo {
return &SystemInfo{
msg: msg,
}
}
return nil
}
type NodeMessage struct {
// 通信类别
Type string `json:"type"`
// 任务相关
TaskId string `json:"task_id"` // 任务ID
// 节点相关
NodeId string `json:"node_id"` // 节点ID
// 日志相关
LogPath string `json:"log_path"` // 日志路径
Log string `json:"log"` // 日志
// 系统信息
SysInfo model.SystemInfo `json:"sys_info"`
// 错误相关
Error string `json:"error"`
}

View File

@@ -0,0 +1,60 @@
package msg_handler
import (
"crawlab/constants"
"crawlab/database"
"crawlab/model"
"crawlab/utils"
"encoding/json"
"github.com/apex/log"
"runtime/debug"
)
type Log struct {
msg NodeMessage
}
func (g *Log) Handle() error {
if g.msg.Type == constants.MsgTypeGetLog {
return g.get()
} else if g.msg.Type == constants.MsgTypeRemoveLog {
return g.remove()
}
return nil
}
func (g *Log) get() error {
// 发出的消息
msgSd := NodeMessage{
Type: constants.MsgTypeGetLog,
TaskId: g.msg.TaskId,
}
// 获取本地日志
logStr, err := model.GetLocalLog(g.msg.LogPath)
log.Info(utils.BytesToString(logStr))
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
msgSd.Error = err.Error()
msgSd.Log = err.Error()
} else {
msgSd.Log = utils.BytesToString(logStr)
}
// 序列化
msgSdBytes, err := json.Marshal(&msgSd)
if err != nil {
return err
}
// 发布消息给主节点
log.Info("publish get log msg to master")
if _, err := database.RedisClient.Publish("nodes:master", utils.BytesToString(msgSdBytes)); err != nil {
return err
}
return nil
}
func (g *Log) remove() error {
return model.RemoveFile(g.msg.LogPath)
}

View File

@@ -0,0 +1,39 @@
package msg_handler
import (
"crawlab/constants"
"crawlab/database"
"crawlab/model"
"crawlab/utils"
"encoding/json"
"github.com/apex/log"
"runtime/debug"
)
type SystemInfo struct {
msg NodeMessage
}
func (s *SystemInfo) Handle() error {
// 获取环境信息
sysInfo, err := model.GetLocalSystemInfo()
if err != nil {
return err
}
msgSd := NodeMessage{
Type: constants.MsgTypeGetSystemInfo,
NodeId: s.msg.NodeId,
SysInfo: sysInfo,
}
msgSdBytes, err := json.Marshal(&msgSd)
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
return err
}
if _, err := database.RedisClient.Publish("nodes:master", utils.BytesToString(msgSdBytes)); err != nil {
log.Errorf(err.Error())
return err
}
return nil
}

View File

@@ -0,0 +1,17 @@
package msg_handler
import (
"crawlab/constants"
"crawlab/utils"
)
type Task struct {
msg NodeMessage
}
func (t *Task) Handle() error {
// 取消任务
ch := utils.TaskExecChanMap.ChanBlocked(t.msg.TaskId)
ch <- constants.TaskCancel
return nil
}