protected override void Run() { var res = NodeProxy.PostToServer <EmptyResponse, UpdateNodeMonitorRequest>(ProxyUrl.UpdateNodeMonitorRequest_Url, new UpdateNodeMonitorRequest() { MonitorClassName = this.GetType().Name, MonitorStatus = (int)MonitorStatus.Monitoring, NodeId = GlobalNodeConfig.NodeID, Source = Source.Node }); if (res.Status != ResponesStatus.Success) { LogProxy.AddNodeErrorLog(this.GetType().Name + "监控组件上报监控信息失败,请求地址:" + ProxyUrl.UpdateNodeMonitorRequest_Url + ",返回结果:" + JsonConvert.SerializeObject(res)); } foreach (var taskruntimeinfo in TaskPoolManager.CreateInstance().GetList()) { try { if (taskruntimeinfo == null) { continue; } string fileinstallpath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalNodeConfig.TaskDllDir + @"\" + taskruntimeinfo.TaskModel.id; double dirsizeM = -1; if (System.IO.Directory.Exists(fileinstallpath)) { long dirsize = IOHelper.DirSize(new DirectoryInfo(fileinstallpath)); dirsizeM = (double)dirsize / 1024 / 1024; } try { if (taskruntimeinfo.Domain != null) { //上报性能 AddPerformanceRequest addperformance = new AddPerformanceRequest() { NodeId = GlobalNodeConfig.NodeID, Cpu = taskruntimeinfo.Domain.MonitoringTotalProcessorTime.TotalSeconds.ToString(), Memory = ((double)taskruntimeinfo.Domain.MonitoringSurvivedMemorySize / 1024 / 1024).ToString(),//AppDomain当前正在使用的字节数(只保证在上一次垃圾回收时是准确的) InstallDirsize = dirsizeM.ToString(), TaskId = taskruntimeinfo.TaskModel.id, Lastupdatetime = (DateTime.Now).ToString(), Source = Source.Node, }; var r = NodeProxy.PostToServer <EmptyResponse, AddPerformanceRequest>(ProxyUrl.AddPerformance_Url, addperformance); if (r.Status != ResponesStatus.Success) { LogProxy.AddTaskErrorLog("任务性能上报出错,请求地址:" + ProxyUrl.AddPerformance_Url + ",请求内容:" + JsonConvert.SerializeObject(addperformance) + ",返回结果:" + JsonConvert.SerializeObject(r), taskruntimeinfo.TaskModel.id); } } } catch (Exception ex) { LogProxy.AddTaskErrorLog("任务性能监控时出错:taskid=" + taskruntimeinfo.TaskModel.id + ",异常信息:" + JsonConvert.SerializeObject(ex), taskruntimeinfo.TaskModel.id); } } catch (Exception exp) { LogProxy.AddNodeErrorLog("任务性能监控异常,异常信息:" + JsonConvert.SerializeObject(exp)); } } }
//[DllImport("psapi.dll")] // public static extern int EmptyWorkingSet(IntPtr hwProc); /// <summary> /// 任务的资源释放 /// </summary> /// <param name="taskid"></param> /// <param name="taskruntimeinfo"></param> /// <returns></returns> public static bool DisposeTask(int taskid, NodeTaskRunTimeInfo taskruntimeinfo, bool isforceDispose,Action<string> ShowCommandLog) { try { if (taskruntimeinfo != null && taskruntimeinfo.DllTask != null) try { taskruntimeinfo.DllTask = null; } catch (Exception ex) { if (ShowCommandLog != null) { ShowCommandLog("强制资源释放之任务资源释放异常,异常信息:" + JsonConvert.SerializeObject(ex)); LogProxy.AddTaskErrorLog("强制资源释放之任务资源释放异常,异常信息:" + JsonConvert.SerializeObject(ex), taskid); } } if (taskruntimeinfo != null && taskruntimeinfo.Domain != null) try { new AppDomainLoaderHelper<AbstractTask>().UnLoad(taskruntimeinfo.Domain); taskruntimeinfo.Domain = null; GC.Collect(); } catch (Exception e) { if (ShowCommandLog != null) { ShowCommandLog("强制资源释放之应用程序域释放异常,异常信息:" + JsonConvert.SerializeObject(e)); LogProxy.AddTaskErrorLog("强制资源释放之应用程序域释放异常,异常信息:" + JsonConvert.SerializeObject(e), taskid); } } if (TaskPoolManager.CreateInstance().Get(taskid.ToString()) != null) try { TaskPoolManager.CreateInstance().Remove(taskid.ToString()); } catch (Exception e) { if (ShowCommandLog != null) { ShowCommandLog("强制资源释放之任务池释放异常,异常信息:" + JsonConvert.SerializeObject(e)); LogProxy.AddTaskErrorLog("强制资源释放之任务池释放异常,异常信息:" + JsonConvert.SerializeObject(e), taskid); } } if (ShowCommandLog != null) ShowCommandLog("节点已对任务进行资源释放完成,任务id:" + taskid); return true; } catch(Exception ex) { LogProxy.AddTaskErrorLog("释放任务资源异常,异常信息:" + JsonConvert.SerializeObject(ex), taskid); return false; } }
public void Execute(JobExecutionContext context) { //lock (lockobj) //{ int taskid = Convert.ToInt32(context.JobDetail.Name); try { var taskruntimeinfo = TaskPoolManager.CreateInstance().Get(taskid.ToString()); taskruntimeinfo.DllTask.ClearLog(); if (taskruntimeinfo == null || taskruntimeinfo.DllTask == null) { // LogHelper.AddTaskError("当前任务信息为空引用", taskid, new Exception()); return; } taskruntimeinfo.TaskLock.Invoke(() => { // int runCount = 0; try { int taskid2 = taskruntimeinfo.TaskModel.id; int taskversionid = taskruntimeinfo.TaskVersionModel.id; string nextrunTime = Convert.ToDateTime(context.NextFireTimeUtc).ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"); nextrunTime = nextrunTime.IndexOf("0001-01") > -1 ? "2099-12-30" : nextrunTime; if (taskruntimeinfo.TaskModel.ispauseschedule == 0) //等于0 说明没有停止调度,否则停止调度 { taskruntimeinfo.DllTask.TryRunTask(nextrunTime, taskruntimeinfo); //执行完,判断是否需要释放 try { if (taskruntimeinfo.TaskModel.tasktype == (int)TaskType.OnceTask) { bool disposeflag = TaskDisposer.DisposeTask(taskid2, taskruntimeinfo, false, null); if (disposeflag) //如果释放成功则上报 { var req = new UpdateTaskScheduleStatusRequest() { NodeId = GlobalNodeConfig.NodeID, Source = Source.Node, ScheduleStatus = Model.enums.TaskScheduleStatus.StopSchedule, TaskId = taskid2, TaskVersionId = taskversionid }; var r2 = NodeProxy.PostToServer <EmptyResponse, UpdateTaskScheduleStatusRequest>(ProxyUrl.UpdateTaskScheduleStatus_Url, req); if (r2.Status != ResponesStatus.Success) { LogProxy.AddTaskErrorLog("更新任务调度状态(停止调度)失败,请求Url:" + ProxyUrl.UpdateTaskScheduleStatus_Url + ",请求参数:" + JsonConvert.SerializeObject(req) + ",返回参数:" + JsonConvert.SerializeObject(r2), taskid); } } else { LogProxy.AddTaskErrorLog("taskid=" + taskid + ",释放单次执行任务资源失败", taskid); } } } catch (Exception ex) { LogProxy.AddTaskErrorLog("taskid=" + taskid + ",释放单次执行任务资源异常:" + JsonConvert.SerializeObject(ex), taskid); } } } catch (Exception exp) { LogProxy.AddTaskErrorLog("任务:" + taskid + ",TaskJob回调时执行异常,异常信息:" + JsonConvert.SerializeObject(exp), taskid); } }); } catch (Exception exp) { LogProxy.AddTaskErrorLog("任务调度组件回调时发生严重异常,异常信息:" + JsonConvert.SerializeObject(exp), taskid); } }