Files
crawlab/backend/services/log.go
2019-09-03 15:24:26 +08:00

58 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package services
import (
"crawlab/constants"
"crawlab/database"
"crawlab/model"
"crawlab/utils"
"encoding/json"
"github.com/apex/log"
"io/ioutil"
"runtime/debug"
)
// 任务日志频道映射
var TaskLogChanMap = utils.NewChanMap()
// 获取本地日志
func GetLocalLog(logPath string) (fileBytes []byte, err error) {
fileBytes, err = ioutil.ReadFile(logPath)
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
return fileBytes, err
}
return fileBytes, nil
}
// 获取远端日志
func GetRemoteLog(task model.Task) (logStr string, err error) {
// 序列化消息
msg := NodeMessage{
Type: constants.MsgTypeGetLog,
LogPath: task.LogPath,
TaskId: task.Id,
}
msgBytes, err := json.Marshal(&msg)
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
return "", err
}
// 发布获取日志消息
channel := "nodes:" + task.NodeId.Hex()
if err := database.Publish(channel, string(msgBytes)); err != nil {
log.Errorf(err.Error())
return "", err
}
// 生成频道等待获取log
ch := TaskLogChanMap.ChanBlocked(task.Id)
// 此处阻塞,等待结果
logStr = <-ch
return logStr, nil
}