/// <summary> /// 任务的资源释放 /// </summary> /// <param name="taskid"></param> /// <param name="taskruntimeinfo"></param> /// <returns></returns> private bool DisposeTask(int taskid, NodeTaskRuntimeInfo taskruntimeinfo, bool isforceDispose) { if (taskruntimeinfo != null && taskruntimeinfo.DllTask != null) { try { taskruntimeinfo.DllTask.Dispose(); taskruntimeinfo.DllTask = null; } catch (TaskSafeDisposeTimeOutException ex) { LogHelper.AddNodeError("强制资源释放之任务资源释放", ex); if (isforceDispose == false) { throw ex; } } }
/// <summary> /// 将任务移入任务池 /// </summary> /// <param name="taskid"></param> /// <param name="taskruntimeinfo"></param> /// <returns></returns> public bool Add(string taskid, NodeTaskRuntimeInfo taskruntimeinfo) { lock (_locktag) { if (!TaskRuntimePool.ContainsKey(taskid)) { JobDetail jobDetail = new JobDetail(taskruntimeinfo.TaskModel.id.ToString(), taskruntimeinfo.TaskModel.categoryid.ToString(), typeof(TaskJob));// 任务名,任务组,任务执行类 var trigger = CornFactory.CreateTigger(taskruntimeinfo); _sched.ScheduleJob(jobDetail, trigger); TaskRuntimePool.Add(taskid, taskruntimeinfo); return(true); } return(false); } }
/// <summary> /// 任务的开启 /// </summary> /// <param name="taskid"></param> /// <returns></returns> public bool Start(int taskid) { var taskruntimeinfo = TaskPoolManager.CreateInstance().Get(taskid.ToString()); if (taskruntimeinfo != null) { throw new Exception("任务已在运行中"); } taskruntimeinfo = new NodeTaskRuntimeInfo(); taskruntimeinfo.TaskLock = new TaskLock(); SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) => { tb_task_dal taskdal = new tb_task_dal(); taskruntimeinfo.TaskModel = taskdal.Get(c, taskid); tb_version_dal versiondal = new tb_version_dal(); taskruntimeinfo.TaskVersionModel = versiondal.GetCurrentVersion(c, taskid, taskruntimeinfo.TaskModel.taskversion); }); string filelocalcachepath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalConfig.TaskDllCompressFileCacheDir + @"\" + taskruntimeinfo.TaskModel.id + @"\" + taskruntimeinfo.TaskModel.taskversion + @"\" + taskruntimeinfo.TaskVersionModel.zipfilename; string fileinstallpath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalConfig.TaskDllDir + @"\" + taskruntimeinfo.TaskModel.id; string fileinstallmainclassdllpath = fileinstallpath + @"\" + taskruntimeinfo.TaskModel.taskmainclassdllfilename; string taskshareddlldir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalConfig.TaskSharedDllsDir; BSF.Tool.IOHelper.CreateDirectory(filelocalcachepath); BSF.Tool.IOHelper.CreateDirectory(fileinstallpath); System.IO.File.WriteAllBytes(filelocalcachepath, taskruntimeinfo.TaskVersionModel.zipfile); CompressHelper.UnCompress(filelocalcachepath, fileinstallpath); //拷贝共享程序集 BSF.Tool.IOHelper.CopyDirectory(taskshareddlldir, fileinstallpath); try { new TaskAssemblyRedirect().TryRebulidDll(fileinstallmainclassdllpath, taskruntimeinfo.TaskModel.taskmainclassnamespace); var dlltask = new AppDomainLoader <BaseDllTask>().Load(fileinstallmainclassdllpath, taskruntimeinfo.TaskModel.taskmainclassnamespace, out taskruntimeinfo.Domain); var sdktaskmodel = new BSF.BaseService.TaskManager.Model.tb_task_model(); PropertyHelper.Copy(taskruntimeinfo.TaskModel, sdktaskmodel); dlltask.SystemRuntimeInfo = new TaskSystemRuntimeInfo() { TaskConnectString = GlobalConfig.TaskDataBaseConnectString, TaskModel = sdktaskmodel }; //加载AppConfig配置 var appconfig = new TaskAppConfigInfo(); if (!string.IsNullOrEmpty(taskruntimeinfo.TaskModel.taskappconfigjson)) { appconfig = new BSF.Serialization.JsonProvider().Deserialize <TaskAppConfigInfo>(taskruntimeinfo.TaskModel.taskappconfigjson); } SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) => { tb_config_dal configdal = new tb_config_dal(); var cs = configdal.GetList(c); foreach (var o in cs) { if (!appconfig.ContainsKey(o.configkey)) { appconfig.Add(o.configkey, o.configvalue); } } }); dlltask.AppConfig = appconfig; taskruntimeinfo.DllTask = dlltask; bool r = TaskPoolManager.CreateInstance().Add(taskid.ToString(), taskruntimeinfo); SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) => { tb_task_dal taskdal = new tb_task_dal(); taskdal.UpdateTaskState(c, taskid, (int)EnumTaskState.Running); }); LogHelper.AddTaskLog("节点开启任务成功", taskid); return(r); } catch (Exception exp) { DisposeTask(taskid, taskruntimeinfo, true); throw exp; } }