static ExeStatus ForceStop(TaskItem task, List <ProcessItem> processes, StringBuilder msg) { // 停止后要把它设置为不启动状态 Dal.Default.UpdateTaskType(task.id, task.runtype, RunType.Stop); if (processes.Count > 0) { // 杀死进程 var killNum = ProcessHelper.KillProcesses(processes); msg.Append("\r\n\t停止完成" + killNum.ToString() + "个"); foreach (ProcessItem item in processes) { msg.AppendFormat("\r\n\t\t{0}", item); } } else { msg.Append("\r\n\t任务未启动"); } Utils.Output(msg); return(ExeStatus.Stopped); }
static ExeStatus StopAndWait1Min(TaskItem task, List <ProcessItem> processes, StringBuilder msg) { // 重启后要把它设置为一直运行 Dal.Default.UpdateTaskType(task.id, task.runtype, RunType.Always); if (processes.Count > 0) { // 杀死进程 var killNum = ProcessHelper.KillProcesses(processes); Utils.Output(task.desc + " " + task.exepath + "已停止" + killNum.ToString() + "个,等1分钟后重启..."); foreach (ProcessItem item in processes) { msg.AppendFormat("\r\n\t\t{0}", item); } msg.Append("\r\n\t已停止,等1分钟,"); } else { Utils.Output(task.desc + " " + task.exepath + "未启动,等1分钟后启动..."); msg.Append("\r\n\t进程不存在,等1分钟,"); } Thread.Sleep(TimeSpan.FromMinutes(1)); var ret = ProcessHelper.CheckAndStartProcess(task.exepath, task.exepara); if (ret > 0) { task.NewPid = ret; msg.Append("\r\n\t重启完成,pid:" + ret.ToString()); } else { msg.Append("\r\n\t进程存在,未启动"); } Utils.Output(msg); return(ExeStatus.Running); }
// RunType.Restart static ExeStatus Restart(TaskItem task, List <ProcessItem> processes, StringBuilder msg) { // 重启后要把它设置为一直运行 Dal.Default.UpdateTaskType(task.id, task.runtype, RunType.Always); if (processes.Count > 0) { // 杀死进程 var killNum = ProcessHelper.KillProcesses(processes); msg.Append("\r\n\t已停止" + killNum.ToString() + "个,"); } else { msg.Append("\r\n\t进程不存在,"); } // 启动进程 var ret = ProcessHelper.CheckAndStartProcess(task.exepath, task.exepara); if (ret > 0) { task.NewPid = ret; msg.Append("重启完成,pid:" + ret.ToString()); if (processes.Count > 0) { foreach (ProcessItem item in processes) { msg.AppendFormat("\r\n\t\t{0}", item); } } } else { msg.Append("进程存在,未启动"); } Utils.Output(msg); return(ExeStatus.Running); }
/// <summary> /// 对程序立即进行的启动或停止操作 /// </summary> /// <param name="strArgs"></param> /// <returns></returns> static string ImmediateProcess(string strArgs) { // string args = ((int) type).ToString() + "\n" + path + "\n" + exepara; string[] args = strArgs.Split('\n'); if (args.Length < 3) { return("参数不足3个"); } int imtype; if (!int.TryParse(args[0], out imtype)) { return("无效的临时类型"); } string exepath = args[1]; // 防止出现 c:\\\\a.exe 或 c:/a.exe这样的路径,统一格式化成:c:\a.exe形式 exepath = Path.Combine(Path.GetDirectoryName(exepath) ?? "", Path.GetFileName(exepath) ?? ""); if (exepath.IndexOf('/') < 0 && exepath.IndexOf('\\') < 0) { string tmp = FindExeFromAllJob(exepath); if (string.IsNullOrEmpty(tmp)) { return("未找到对应job:" + exepath); } exepath = tmp; } if (!File.Exists(exepath)) { return("文件不存在:" + exepath); } string exepara = args[2]; int ret; switch ((ImmediateType)imtype) { default: return("不存在的临时类型"); case ImmediateType.Start: // 查找进程是否运行中,不在则启动 ret = ProcessHelper.CheckAndStartProcess(exepath, exepara); if (ret > 0) { return(exepath + " 成功启动, pid:" + ret.ToString()); } else { return(exepath + " 运行中,无需启动"); } case ImmediateType.Stop: var processes1 = ProcessItem.GetProcessByPath(exepath); ret = ProcessHelper.KillProcesses(processes1); if (ret > 0) { return(exepath + " 成功关闭个数:" + ret.ToString()); } else { return(exepath + " 未运行,无需停止"); } case ImmediateType.ReStart: string restartMsg; // 杀死进程 var processes2 = ProcessItem.GetProcessByPath(exepath); ret = ProcessHelper.KillProcesses(processes2); if (ret > 0) { restartMsg = exepath + " 成功关闭个数:" + ret.ToString(); } else { restartMsg = exepath + " 未启动"; } // 查找进程是否运行中,不在则启动 ret = ProcessHelper.CheckAndStartProcess(exepath, exepara); if (ret > 0) { return(restartMsg + " 重启完成,pid:" + ret.ToString()); } else { return(restartMsg + " 进程已存在"); } } }
static ExeStatus PerTime(TaskItem task, List <ProcessItem> processes, StringBuilder msg) { ExeStatus status; // 未设置定时运行参数时 if (!task.TaskPara.Any()) { status = ExeStatus.NoPara; // 更新任务运行状态 Dal.Default.UpdateTaskExeStatus(task.id, status); msg.Append("\r\n\t任务参数未配置,运行失败"); Utils.Output(msg.ToString()); return(status); } if (processes.Count <= 0) { status = ExeStatus.Stopped; } else { foreach (ProcessItem item in processes) { msg.AppendFormat("\r\n\t\t{0}", item); } status = ExeStatus.Running; } StringBuilder sbEndTime = new StringBuilder(); var now = DateTime.Now; bool? isrun = null; foreach (TimePara timepara in task.TaskPara) { if (task.runtype == RunType.PerWeek && timepara.WeekOrDay != (int)now.DayOfWeek) { continue; } if (task.runtype == RunType.PerMonth && timepara.WeekOrDay != now.Day) { continue; } // 启动小时必须是小于24的数,否则会出异常 int hour = timepara.StartHour;// == 24 ? 0 : timepara.StartHour; if (hour < 0) { hour = 0; } else if (hour >= 24) { hour = 0; } int min = timepara.StartMin; if (min > 59) { min = 59; } else if (min < 0) { min = 0; } DateTime starttime = new DateTime(now.Year, now.Month, now.Day, hour, min, 0); if (timepara.StartHour >= 24) { starttime = starttime.AddDays(1); } DateTime endtime = DateTime.MaxValue; // 为true表示启动时间是从数据库获取的 bool isTimeParaSeted = false; if (timepara.RunMinute > 0) { // 从数据库取得记录的结束时间(之所以保存到数据库,因为每次任务都是从db取得,每次StartTime都会被重置,导致隔日停止失败) timepara.StartTime = Dal.Default.GetTimePara(task.id, timepara); if (timepara.StartTime == default(DateTime)) { timepara.StartTime = starttime; } else { isTimeParaSeted = true; } endtime = timepara.StartTime.AddMinutes(timepara.RunMinute); sbEndTime.Append("\r\n\t" + timepara.StartTime.ToString("MM-dd_HH:mm") + "~" + endtime.ToString("MM-dd_HH:mm")); } // 任务已经处理过了 if (isrun != null) { continue; } // 前次运行时间比定时小 且 时间到了 且 进程没有运行 且 没过结束时间 //if (starttime > task.pidtime && starttime <= now && now < endtime) // 在启动时间之后,且在1分钟之内,启动它 if (starttime <= now && now < endtime && (now - starttime).TotalSeconds < 60) { isrun = true; // 记录开始时间,以便下次轮询判断结束 if (isTimeParaSeted) { Dal.Default.DelTimePara(task.id, timepara); } Dal.Default.AddTimePara(task.id, timepara); if (processes.Count <= 0) { var lastRunDiffSecond = (now - task.pidtime).TotalSeconds; if (lastRunDiffSecond > 60) { // 启动进程 var pid = ProcessHelper.CheckAndStartProcess(task.exepath, task.exepara); if (pid > 0) { task.NewPid = pid; msg.Append("任务成功启动,pid:" + pid.ToString()); } else { msg.Append("任务存在,启动失败"); } status = ExeStatus.Running; } else { msg.Append("\r\n\t任务1分钟只能启动1次"); status = ExeStatus.Stopped; } } else { msg.Append("\r\n\t" + processes.Count.ToString() + "个任务运行中"); status = ExeStatus.Running; } // 记录之,用于计算结束时间 if (timepara.RunMinute > 0) { timepara.StartTime = now; } // 不用break,是为了统计并输出结束时间日志 continue; } // now比endtime大,且在1分钟之内,停止它 if (endtime <= now && (now - endtime).TotalSeconds < 60 && processes.Count > 0) { isrun = false; // 结束完成,删除 开始时间记录,以便下次轮询重新计算 if (isTimeParaSeted) { Dal.Default.DelTimePara(task.id, timepara); } var killNum = ProcessHelper.KillProcesses(processes); if (killNum > 0) { msg.Append("\r\n\t任务成功终止" + killNum.ToString() + "个"); status = ExeStatus.Stopped; } else { msg.Append("\r\n\t任务终止失败"); } // 不用break,是为了统计并输出结束时间日志 //continue; } else if (endtime <= now && isTimeParaSeted) { // 清除上次启动时间,避免过期时间在以前,导致程序永远无法终止 Dal.Default.DelTimePara(task.id, timepara); } } if (isrun == null) { msg.Append("\r\n\t时间没到 " + (status == ExeStatus.Stopped ? "任务未启动" : "任务运行中")); } if (sbEndTime.Length > 0) { msg.Append("\r\n\t起止时间:" + sbEndTime); } Utils.Output(msg); return(status); }