mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-31 18:10:50 +01:00
This commit is contained in:
@@ -74,6 +74,8 @@ func GetService(msg entity.RpcMessage) Service {
|
||||
return &GetLangService{msg: msg}
|
||||
case constants.RpcGetInstalledDepList:
|
||||
return &GetInstalledDepsService{msg: msg}
|
||||
case constants.RpcCancelTask:
|
||||
return &CancelTaskService{msg: msg}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
63
backend/services/rpc/cancel_task.go
Normal file
63
backend/services/rpc/cancel_task.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"crawlab/constants"
|
||||
"crawlab/entity"
|
||||
"crawlab/model"
|
||||
"crawlab/utils"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
type CancelTaskService struct {
|
||||
msg entity.RpcMessage
|
||||
}
|
||||
|
||||
func (s *CancelTaskService) ServerHandle() (entity.RpcMessage, error) {
|
||||
taskId := utils.GetRpcParam("task_id", s.msg.Params)
|
||||
nodeId := utils.GetRpcParam("node_id", s.msg.Params)
|
||||
if err := CancelTaskLocal(taskId, nodeId); err != nil {
|
||||
s.msg.Error = err.Error()
|
||||
return s.msg, err
|
||||
}
|
||||
s.msg.Result = "success"
|
||||
return s.msg, nil
|
||||
}
|
||||
|
||||
func (s *CancelTaskService) ClientHandle() (o interface{}, err error) {
|
||||
// 发起 RPC 请求,获取服务端数据
|
||||
_, err = ClientFunc(s.msg)()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func CancelTaskLocal(taskId string, nodeId string) error {
|
||||
if !utils.TaskExecChanMap.HasChanKey(taskId) {
|
||||
_ = model.UpdateTaskToAbnormal(bson.ObjectIdHex(nodeId))
|
||||
return errors.New(fmt.Sprintf("task id (%s) does not exist", taskId))
|
||||
}
|
||||
ch := utils.TaskExecChanMap.ChanBlocked(taskId)
|
||||
ch <- constants.TaskCancel
|
||||
return nil
|
||||
}
|
||||
|
||||
func CancelTaskRemote(taskId string, nodeId string) (err error) {
|
||||
params := make(map[string]string)
|
||||
params["task_id"] = taskId
|
||||
params["node_id"] = nodeId
|
||||
s := GetService(entity.RpcMessage{
|
||||
NodeId: nodeId,
|
||||
Method: constants.RpcCancelTask,
|
||||
Params: params,
|
||||
Timeout: 60,
|
||||
})
|
||||
_, err = s.ClientHandle()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user