public NodeTaskRunTimeInfo Create(string taskid) { NodeTaskRunTimeInfo taskruntimeinfo = TaskPoolManager.CreateInstance().Get(taskid.ToString()); if (taskruntimeinfo != null) { throw new Exception("任务已在运行中"); } taskruntimeinfo = new NodeTaskRunTimeInfo(); taskruntimeinfo.TaskLock = new TaskLock(); // taskruntimeinfo.TaskModel = task; return(taskruntimeinfo); }
//[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); } }