protected virtual void OnUnhandledException(Exception ex, ExecutedNotifyEventArgs agrs) { ExceptionEventArgs t = new ExceptionEventArgs(ex, agrs.Folder, agrs.TaskID, agrs.Description) { ExecuteDuration = agrs.ExecuteDuration, ExecutePath = agrs.ExecutePath, ExecuteResult = agrs.ExecuteResult, ExecuteStartTime = agrs.ExecuteStartTime, LastExecuteTime = agrs.LastExecuteTime, NextExecuteTime = agrs.NextExecuteTime, ServerName = agrs.ServerName }; Logger.Error(t); ExceptionEventHandler handler = m_UnhandledException; if (handler != null) { try { handler(this, t); } catch (Exception ex1) { Logger.Error(m_FolderPath, TaskID, ex1, "执行SpTaskExecutor.OnUnhandledException时出错"); } } }
protected virtual void OnExecutedNotify(ExecutedNotifyEventArgs args) { Logger.Info(args); EventHandler <ExecutedNotifyEventArgs> handler = m_ExecutedNotify; if (handler != null) { handler.BeginInvoke(this, args, new AsyncCallback(o => { EventHandler <ExecutedNotifyEventArgs> h = o.AsyncState as EventHandler <ExecutedNotifyEventArgs>; if (h != null) { try { h.EndInvoke(o); } catch (Exception ex1) { Logger.Error(m_FolderPath, TaskID, ex1, "执行SpTaskExecutor.OnExecutedNotify时出错"); } } }), handler); //handler(this, args); } }
private void CallLoopMethod(ExecutedNotifyEventArgs agrs) { if (m_HasLoopMethod) { Stopwatch timer = new Stopwatch(); Exception exc = null; try { timer.Start(); Invoker.MethodInvoke(m_Executor, m_TaskInfo.LoopMethodName.Trim()); } catch (Exception ex) { exc = ex; } finally { timer.Stop(); agrs.ExecuteDuration = timer.Elapsed.TotalMilliseconds; if (exc != null) { agrs.Description = "在Task循环执行处执行" + m_TaskInfo.TypeFullName + "." + m_TaskInfo.LoopMethodName + "方法时出错"; agrs.ExecuteResult = ExecuteResultEnum.Failed; OnUnhandledException(exc, agrs); } else { agrs.Description = "在Task循环执行处执行" + m_TaskInfo.TypeFullName + "." + m_TaskInfo.LoopMethodName + "方法成功"; agrs.ExecuteResult = ExecuteResultEnum.Successed; OnExecutedNotify(agrs); } } } }
private void InnerExecute(object state) { if (m_IsStop) { return; } if (!m_IsOpenMethodExecuted) { if (m_IsStop) { return; } CallOpenMethod(); m_IsOpenMethodExecuted = true; m_LastLoopExecuteTime = DateTime.Now; } if (!m_HasLoopMethod || m_IsStop) { return; } ScheduleActionEnum action; DateTime? nextRunTime = null; try { action = m_Scheduler.GetAction(m_LastLoopExecuteTime, DateTime.Now, out nextRunTime); } catch (Exception ex) { OnUnhandledException(ex, "在Task循环执行处执行" + m_Scheduler.GetType().FullName + ".GetAction时发生错误"); return; } if (action == ScheduleActionEnum.End || m_IsStop) { return; } if (action == ScheduleActionEnum.Run) { ExecutedNotifyEventArgs agrs = new ExecutedNotifyEventArgs(); agrs.ExecutePath = this.m_ExecutePath; agrs.ServerName = m_ServerName; agrs.TaskID = m_TaskInfo.ID; agrs.Folder = m_FolderPath; agrs.LastExecuteTime = m_LastLoopExecuteTime == default(DateTime) ? null : new DateTime?(m_LastLoopExecuteTime); agrs.NextExecuteTime = nextRunTime; m_LastLoopExecuteTime = DateTime.Now; agrs.ExecuteStartTime = m_LastLoopExecuteTime; CallLoopMethod(agrs); } if (m_IsStop) { return; } if (m_Timer != null) { m_Timer.Change(m_PollingIntervalMilliseconds, Timeout.Infinite); } }
protected virtual void OnExecutedNotify(object sender, ExecutedNotifyEventArgs args) { EventHandler <ExecutedNotifyEventArgs> handler = m_ExecutedNotify; if (handler != null) { handler(sender, args); } }
protected virtual void OnExecutedNotify(ExecutedNotifyEventArgs args) { EventHandler <ExecutedNotifyEventArgs> handler = m_ExecutedNotify; if (handler != null) { try { handler(this, args); } catch (Exception ex1) { Logger.Error(m_FolderPath, TaskID, ex1, "执行AppDomainTaskExecutor.OnExecutedNotify时出错"); } } }
public static void Info(ExecutedNotifyEventArgs e) { string log = string.Format("任务\"{0}\"执行完毕\r\n执行结果: {1} - {2}\r\n开始时间: {3}\r\n执行耗时: {4}ms\r\n下次执行: {5}", e.TaskID, e.ExecuteResult, e.Description, e.ExecuteStartTime, e.ExecuteDuration, e.NextExecuteTime); WriteLog(e.Folder, "HostLog", log); }
private void ExecSP(ExecutedNotifyEventArgs agrs) { Stopwatch timer = new Stopwatch(); Exception exc = null; string timestampFileName = null; DateTime now = DateTime.Now; try { List <SqlParameter> list = new List <SqlParameter>(); if (m_TaskInfo.SpParamList != null && m_TaskInfo.SpParamList.SpParams != null && m_TaskInfo.SpParamList.SpParams.Count > 0) { foreach (var param in m_TaskInfo.SpParamList.SpParams) { object val; if (param.Type == "now") { val = now; } else if (param.Type == "last_run_time") { timestampFileName = param.Value; if (string.IsNullOrWhiteSpace(timestampFileName)) { timestampFileName = m_TaskInfo.ID + ".ts"; } val = ReadTimestamp(timestampFileName); } else { Type type = Type.GetType(param.Type); val = GetValueByType(type, param.Value); } string pn = param.Name; if (pn.StartsWith("@") == false) { pn = "@" + pn; } list.Add(new SqlParameter(pn, val)); } } using (SqlConnection conn = new SqlConnection(m_TaskInfo.ConnectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(m_TaskInfo.SpName, conn)) { cmd.CommandType = CommandType.StoredProcedure; foreach (var p in list) { cmd.Parameters.Add(p); } cmd.ExecuteNonQuery(); } conn.Close(); } if (string.IsNullOrWhiteSpace(timestampFileName) == false) { SaveTimestamp(now, timestampFileName); } } catch (Exception ex) { exc = ex; } finally { timer.Stop(); agrs.ExecuteDuration = timer.Elapsed.TotalMilliseconds; if (exc != null) { agrs.Description = "在Task循环执行处执行存储过程" + m_TaskInfo.SpName + "时发生错误"; agrs.ExecuteResult = ExecuteResultEnum.Failed; OnUnhandledException(exc, agrs); } else { agrs.Description = "在Task循环执行处执行存储过程" + m_TaskInfo.SpName + "方法成功"; agrs.ExecuteResult = ExecuteResultEnum.Successed; OnExecutedNotify(agrs); } } }
private void RunExeFile(ExecutedNotifyEventArgs agrs) { Process proc = null; Stopwatch timer = new Stopwatch(); Exception exc = null; try { StringBuilder outputData = new StringBuilder(); proc = new Process(); proc.StartInfo.FileName = m_ExePath; if (m_TaskInfo.Args != null && m_TaskInfo.Args.Trim().Length > 0) { proc.StartInfo.Arguments = m_TaskInfo.Args.Trim(); } proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(proc.StartInfo.FileName); proc.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { outputData.AppendLine(e.Data); }; timer.Start(); proc.Start(); proc.BeginOutputReadLine(); if (!proc.WaitForExit(m_RunningTimeout)) { proc.Kill(); exc = new TimeoutException("任务执行超时(超过了24小时),已被自动终止"); } if (proc.ExitCode != 0) { string error = proc.StandardError.ReadToEnd(); exc = new ApplicationException(error); } else { agrs.Description = outputData.ToString(); } } catch (Exception ex) { exc = ex; } finally { timer.Stop(); if (proc != null) { try { proc.Close(); } catch { } } agrs.ExecuteDuration = timer.Elapsed.TotalMilliseconds; if (exc != null) { agrs.Description = "在Task开启新进程执行文件" + m_ExePath + "时发生错误"; agrs.ExecuteResult = ExecuteResultEnum.Failed; OnUnhandledException(exc, agrs); } else { agrs.Description = "Task开启新进程执行文件" + m_ExePath + "成功!Console上输出:" + agrs.Description; agrs.ExecuteResult = ExecuteResultEnum.Successed; OnExecutedNotify(agrs); } } }