using System.Collections.Generic; using System.Linq; using FileTransDataObject; using Sog; using Google.Protobuf.WellKnownTypes; namespace SMCenter { // 文件拉取命令 public class FilePullCmdProc : BaseCmdProc { public override void ClearData() { m_nowCmd = null; } // cmdParams[0] is serverId public override int DoCmd(out string msg) { if (m_nowCmd.CMD == "cancelpull") { CancelPull(); msg = ""; return 0; } if (m_nowCmd.CmdParams.Count < 1) { msg = "invalid cmd param"; return -1; } if (CmdUtils.CheckValidServerId(m_nowCmd.CmdParams[0], out string fixSvrId) == false) { msg = "invalid server id"; return -1; } if (fixSvrId != null) { m_nowCmd.CmdParams[0] = fixSvrId; } SMProcAppMgr.Instance.GetAllMatchingProc(m_nowCmd.CmdParams[0], m_nowCmd.m_procs); // HostName去重 CmdProcInfoDistinct(); if (CenterFilePullMng.Instance.DoPullCmd(m_nowCmd, out msg) != 0) { return -1; } // 通知agent pull cmd long nowSec = AppTime.ServerAppTime.GetTimeSecond(); foreach (var cmdInfo in m_nowCmd.m_procs.Values) { if (cmdInfo.SendCmdToHostTime == 0) { SendCmdToHost(cmdInfo); cmdInfo.SendCmdToHostTime = nowSec; } } msg += m_nowCmd.ConsoleInput + " begin ..."; TraceLog.Trace("FilePullCmdProc.DoCmd {0}", msg); return 0; } public override int UpdateCmd(long tMs) { if (CenterFilePullMng.Instance.isFinish) { AckConsoleCmdResult(); CenterFilePullMng.Instance.Clear(); return 0; } return 1; } private void AckConsoleCmdResult() { SMConsoleCommandRes res = new SMConsoleCommandRes(); res.Command = m_nowCmd.ConsoleInput; res.Message = ""; var recvMgr = CenterFilePullMng.Instance.recvMgr; string procResult = ""; string pullFiles = null; foreach (var proc in m_nowCmd.m_procs.Values) { string hostName = proc.SMApp.HostName; var recvNode = recvMgr.GetRecvNode(hostName); if (recvNode == null) { procResult += string.Format("\n{0}: NoRes", hostName); } else { procResult += string.Format("\n{0}: ", hostName); // pull file的文件列表对于所有recvNode都是相同的 if (pullFiles == null) { for (int i = 0; i < recvNode.fileList.Count; i++) { pullFiles += recvNode.fileList[i].fileName; // 每行3个文件 pullFiles += i % 3 == 2 ? "\n\t" : "\t"; } } // 1表示成功 string str = string.Join(' ', recvNode.fileList.Where(r => r.recvState != 1).Select(r => r.fileName)); if (str.Length == 0) { procResult += "Succ"; } else { procResult += "Fail (" + str + ")"; } } if (pullFiles != null) { res.Message += "Files: " + pullFiles; } res.Message += procResult; } SMCenterNet.Instance.SendMsg(m_nowCmd.SessionId, SMMsgID.ConsoleCommandRes, res); } private void SendCmdToHost(CmdProcInfo cmdInfo) { string hostname = cmdInfo.SMApp.HostName; ClientInfo client = SMCenterNet.Instance.GetClientInfoByName(hostname); if (client == null) { TraceLog.Error("FilePullCmdProc.SendCmdToHost host {0} agent not register!", hostname); return; } SMAgentDoCommandReq req = new SMAgentDoCommandReq(); req.Command = m_nowCmd.CMD; req.SeqNum = m_nowCmd.SeqNum; req.ServerId = cmdInfo.SMApp.AppId; req.StopTimeout = cmdInfo.SMApp.StopTimeout; req.CenterName = SMCenterUtils.HostName; req.TestMode = SMCenterUtils.Config.testMode; client.SendMsg(req, SMMsgID.AgentDoCommandReq); } public override void OnAgentDoCommandRes(ClientInfo client, SMAgentDoCommandRes res) { if (m_nowCmd.SeqNum != res.SeqNum) { TraceLog.Error("FilePullCmdProc.OnAgentDoCommandRes CMD {0} svrId {1} seq not same, local {2} res {3}" , res.Command, res.ServerId, m_nowCmd.SeqNum, res.SeqNum); return; } if (m_nowCmd.m_procs.ContainsKey(res.ServerId) == false) { TraceLog.Error("FilePullCmdProc.OnAgentDoCommandRes CMD {0} serverId {1} not in m_procs, skip" , res.Command, res.ServerId); return; } TraceLog.Trace("FilePullCmdProc.OnAgentDoCommandRes CMD {0} svrId {1} ret {2} msg {3}" , res.Command, res.ServerId, res.ResultCode, res.Result); // 文件不存在或异常, 先回复console if (res.ResultCode != ResResultCode.Success) { SMConsoleCommandRes csRes = new SMConsoleCommandRes(); csRes.Command = m_nowCmd.ConsoleInput; csRes.Message = res.Result; SMCenterNet.Instance.SendMsg(m_nowCmd.SessionId, SMMsgID.ConsoleCommandRes, csRes); } } public void CancelPull() { TraceLog.Trace("FilePullCmdProc.CancelPull"); CenterFilePullMng.Instance.CancelPull(); } } }