/// <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;
            }
        }
示例#3
0
        /// <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>
        /// 加入pool manger
        /// </summary>
        /// <param name="taskid"></param>
        /// <param name="taskruntimeinfo"></param>
        /// <returns></returns>
        public bool AddInstance(string taskid, NodeTaskRuntimeInfo taskruntimeinfo)


        {
            lock (_locktag)
            {
                if (!TaskRuntimePool.ContainsKey(taskid))
                {
                    //taskruntimeinfo.DllTask.TryRun();

                    TaskRuntimePool.Add(taskid, taskruntimeinfo);
                    return(true);
                }

                return(false);
            }
        }
        private static bool StartProcess(int taskid, NodeTaskRuntimeInfo taskruntimeinfo, Process result)
        {
            taskruntimeinfo.Process = result;

            /*  AppDomain.CurrentDomain.DomainUnload += (s, e) =>
             *      {
             *          result.Kill();
             *          result.WaitForExit();
             *      };
             *      AppDomain.CurrentDomain.ProcessExit += (s, e) =>
             *      {
             *          result.Kill();
             *          result.WaitForExit();
             *      };
             *      AppDomain.CurrentDomain.UnhandledException += (s, e) =>
             *      {
             *          result.Kill();
             *          result.WaitForExit();
             *      };
             */
            //  Task a = Task.Factory.StartNew(() =>
            // {

            bool isStart = result.Start();

            ChildProcessTracker.AddProcess(result);



            Task.Factory.StartNew(() =>
            {
                while (!result.StandardOutput.EndOfStream)
                {
                    string line = result.StandardOutput.ReadLine();
                    // do something with line
                    LogHelper.AddTaskLog(line, taskid);
                }
            });
            bool r = TaskPoolManager.CreateInstance().AddInstance(taskid.ToString(), taskruntimeinfo);

            return(r);
        }
 private static void KillProcess(string taskId, NodeTaskRuntimeInfo taskruntimeinfo)
 {
     if (taskruntimeinfo.Process != null)
     {
         try
         {
             taskruntimeinfo.Process.Kill();
             taskruntimeinfo.Process.WaitForExit();
         }
         catch (Exception ex)
         {
             LogHelper.AddNodeError($"kill process{ex.Message}", ex);
         }
     }
     try
     {
         TaskPoolManager.CreateInstance().Remove(taskId);
     }
     catch (Exception e)
     {
         LogHelper.AddNodeError("强制资源释放之任务池释放", e);
     }
 }
 public static Trigger CreateTigger(NodeTaskRuntimeInfo taskruntimeinfo)
 {
     if (taskruntimeinfo.TaskModel.taskcron.Contains("["))
     {
         var customcorn = CustomCornFactory.GetCustomCorn(taskruntimeinfo.TaskModel.taskcron);
         customcorn.Parse();
         if (customcorn is SimpleCorn || customcorn is RunOnceCorn)
         {
             var simplecorn = customcorn as SimpleCorn;
             // 定义调度触发规则,比如每1秒运行一次,共运行8次
             SimpleTrigger simpleTrigger = new SimpleTrigger(taskruntimeinfo.TaskModel.id.ToString(), taskruntimeinfo.TaskModel.categoryid.ToString());
             if (simplecorn.ConInfo.StartTime != null)
                 simpleTrigger.StartTimeUtc = simplecorn.ConInfo.StartTime.Value.ToUniversalTime();
             //else
             //    simpleTrigger.StartTimeUtc = DateTime.Now.ToUniversalTime();
             if (simplecorn.ConInfo.EndTime != null)
                 simpleTrigger.EndTimeUtc = simplecorn.ConInfo.EndTime.Value.ToUniversalTime();
             if (simplecorn.ConInfo.RepeatInterval != null)
                 simpleTrigger.RepeatInterval = TimeSpan.FromSeconds(simplecorn.ConInfo.RepeatInterval.Value);
             else
                 simpleTrigger.RepeatInterval = TimeSpan.FromSeconds(1);
             if (simplecorn.ConInfo.RepeatCount != null)
                 simpleTrigger.RepeatCount = simplecorn.ConInfo.RepeatCount.Value - 1;//因为任务默认执行一次,所以减一次
             else
                 simpleTrigger.RepeatCount = int.MaxValue;//不填,则默认最大执行次数
             return simpleTrigger;
         }
         return null;
     }
     else
     {
         CronTrigger trigger = new CronTrigger(taskruntimeinfo.TaskModel.id.ToString(), taskruntimeinfo.TaskModel.categoryid.ToString());// 触发器名,触发器组  
         trigger.CronExpressionString = taskruntimeinfo.TaskModel.taskcron;// 触发器时间设定  
         return trigger;
     }
 }
        /// <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);
                //taskruntimeinfo.ProcessId=taskdal.GetProcess(c, taskid);
            });
            //如果异常退出,进程后没有更新

            /*if (taskruntimeinfo.TaskModel.task_type == TaskType.Service.Code)
             * {
             *
             * }*/
            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;
            string shelldlldir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalConfig.TaskShellDir;

            XXF.Common.IOHelper.CreateDirectory(filelocalcachepath);
            XXF.Common.IOHelper.CreateDirectory(fileinstallpath);
            // File.WriteAllBytes(filelocalcachepath, taskruntimeinfo.TaskVersionModel.zipfile);
            FileStream fs = new FileStream(filelocalcachepath, FileMode.Create, FileAccess.ReadWrite);

            SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
            {
                tb_version_dal versiondal = new tb_version_dal();
                versiondal.FillByteToFile(c, taskid, taskruntimeinfo.TaskModel.taskversion, fs);
            });
            fs.Close();
            if (Directory.Exists(fileinstallpath))
            {
                File.SetAttributes(fileinstallpath, FileAttributes.Normal);
                Directory.Delete(fileinstallpath, true);
            }
            CompressHelper.UnCompress(filelocalcachepath, fileinstallpath);
            //拷贝共享程序集

            XXF.Common.IOHelper.CopyDirectory(taskshareddlldir, fileinstallpath);
            //如果是module



            if (taskruntimeinfo.TaskModel.IsModule)
            {
                XXF.Common.IOHelper.CopyDirectory(shelldlldir, fileinstallpath);
            }
            LogHelper.AddTaskLog($"原程序集版本:{taskruntimeinfo.TaskVersionModel.assemblyversion}", taskid);
            //LogHelper.AddTaskLog($"程序集文件:{fileinstallmainclassdllpath}",taskid);
            string assemblyVersion = null;

            byte[] bytes = Encoding.Default.GetBytes(taskruntimeinfo.TaskModel
                                                     .taskappconfigjson);
            string jsonConfig = Convert.ToBase64String(bytes);
            bool   r          = false;

            if (taskruntimeinfo.TaskModel.task_type == TaskType.Service.Code)
            {
                bool            is_module = taskruntimeinfo.TaskModel.IsModule;
                IProcessBuilder builder;
                //当
                //
                try
                {
                    // string assemblyVersion = GetAssemblyVersion(fileinstallmainclassdllpath);
                    Process result;

                    string flag = taskruntimeinfo.TaskModel.ServiceFlag;



                    var startupParam = new ProcessStartupParam()
                    {
                        Flag             = flag, FileName = fileinstallmainclassdllpath,
                        FilePatten       = taskruntimeinfo.TaskModel.taskmainclassdllfilename,
                        Config           = jsonConfig, WorkDir = fileinstallpath,
                        Cron             = taskruntimeinfo.TaskModel.taskcron,
                        NameSpace        = taskruntimeinfo.TaskModel.taskmainclassnamespace,
                        TaskDbConnection = GlobalConfig.TaskDataBaseConnectString,
                        TaskModel        = taskruntimeinfo.TaskModel,
                        AppConfig        = new XXF.Serialization.JsonHelper().Deserialize <TaskAppConfigInfo>(taskruntimeinfo
                                                                                                              .TaskModel
                                                                                                              .taskappconfigjson)
                    };

                    builder = ProcessStart.GetInstance().GetBuilder(startupParam);
                    builder.GetMainFileName();
                    assemblyVersion = builder.GetAssemblyVersion();
                    //     taskruntimeinfo.TaskModel.taskmainclassdllfilename = builder.StartupParam.FileName;
                    result = builder.StartProcess();



                    r = StartProcess(taskid, taskruntimeinfo, result);
                }
                catch (Exception ex)
                {
                    LogHelper.AddTaskLog($"节点开启任务失败{ex.Message}", taskid);
                    throw;
                }
                // bool r = TaskPoolManager.CreateInstance().Add(taskid.ToString(), taskruntimeinfo);
                SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
                {
                    tb_task_dal taskdal = new tb_task_dal();
                    //更新类型
                    taskdal.Edit(c, taskruntimeinfo.TaskModel);
                    taskdal.UpdateTaskState(c, taskid, (int)EnumTaskState.Running);
                    taskdal.UpdateProcess(c, taskid, taskruntimeinfo.Process.Id);
                    //程序集版本更新
                    if (!string.IsNullOrEmpty(assemblyVersion))
                    {
                        if (taskruntimeinfo.TaskVersionModel.assemblyversion != assemblyVersion)
                        {
                            taskruntimeinfo.TaskVersionModel.assemblyversion = assemblyVersion;
                            tb_version_dal versiondal = new tb_version_dal();
                            versiondal.UpdateAssemblyVersion(c, taskruntimeinfo.TaskVersionModel.id, assemblyVersion);
                        }
                    }
                });
                //
                if (taskruntimeinfo.TaskModel.IsRegister == 1)
                {
                    ConsulRegisteration item = _consulRegisterMgr.Parse(taskruntimeinfo.TaskModel);
                    item.Service = builder.GetService();
                    taskruntimeinfo.RegistService = item;
                    _consulRegisterMgr.Register(item);
                }

                LogHelper.AddTaskLog("节点开启任务成功", taskid);
                return(r);
            }
            else
            {
                //以dll加载的方式处理,基本已废弃
                assemblyVersion = GetAssemblyVersion(fileinstallmainclassdllpath);

                if (!string.IsNullOrEmpty(taskruntimeinfo.TaskModel.ServiceFlag))
                {
                    return(StartTaskProcess(taskid, taskruntimeinfo, fileinstallmainclassdllpath,
                                            fileinstallpath, jsonConfig, assemblyVersion));
                }
                else
                {
                    try
                    {
                        var dlltask = new AppDomainLoader <BaseDllTask>().Load(fileinstallmainclassdllpath,
                                                                               taskruntimeinfo.TaskModel.taskmainclassnamespace, out taskruntimeinfo.Domain);
                        //  dlltask.Domain = taskruntimeinfo.Domain;
                        var sdktaskmodel = new XXF.BaseService.TaskManager.model.tb_task_model();
                        PropertyHelper.Copy(taskruntimeinfo.TaskModel, sdktaskmodel);
                        dlltask.SystemRuntimeInfo = new TaskSystemRuntimeInfo()
                        {
                            TaskConnectString = GlobalConfig.TaskDataBaseConnectString,
                            TaskModel         = sdktaskmodel
                        };

                        dlltask.AppConfig = new TaskAppConfigInfo();
                        if (!string.IsNullOrEmpty(taskruntimeinfo.TaskModel.taskappconfigjson))
                        {
                            dlltask.AppConfig =
                                new XXF.Serialization.JsonHelper().Deserialize <TaskAppConfigInfo>(taskruntimeinfo
                                                                                                   .TaskModel
                                                                                                   .taskappconfigjson);
                        }

                        taskruntimeinfo.DllTask = dlltask;

                        /*   if (dlltask is IMicroService)
                         * {
                         *     taskruntimeinfo.TaskModel.task_type = TaskType.Service.Code;
                         * }
                         * else
                         * {
                         *     taskruntimeinfo.TaskModel.task_type = TaskType.Task.Code;
                         *
                         * }*/

                        r = TaskPoolManager.CreateInstance().Add(taskid.ToString(), taskruntimeinfo);
                        SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
                        {
                            tb_task_dal taskdal = new tb_task_dal();
                            //更新类型
                            taskdal.Edit(c, taskruntimeinfo.TaskModel);
                            taskdal.UpdateTaskState(c, taskid, (int)EnumTaskState.Running);
                            //程序集版本更新
                            if (!string.IsNullOrEmpty(assemblyVersion))
                            {
                                if (taskruntimeinfo.TaskVersionModel.assemblyversion != assemblyVersion)
                                {
                                    taskruntimeinfo.TaskVersionModel.assemblyversion = assemblyVersion;
                                    tb_version_dal versiondal = new tb_version_dal();
                                    versiondal.UpdateAssemblyVersion(c, taskruntimeinfo.TaskVersionModel.id,
                                                                     assemblyVersion);
                                }
                            }
                        });

                        LogHelper.AddTaskLog("节点开启任务成功", taskid);
                        return(r);
                    }
                    catch (Exception exp)
                    {
                        DisposeTask(taskid, taskruntimeinfo, true);
                        throw exp;
                    }
                }
            }
        }
        private bool StartTaskProcess(int taskid, NodeTaskRuntimeInfo taskruntimeinfo, string
                                      fileinstallmainclassdllpath, string fileinstallpath, string jsonConfig,
                                      string assemblyVersion)
        {
            bool is_module = taskruntimeinfo.TaskModel.IsModule;

            //当
            //
            try
            {
                Process result;

                string flag = taskruntimeinfo.TaskModel.ServiceFlag;



                var startupParam = new ProcessStartupParam()
                {
                    Flag             = flag,
                    FileName         = fileinstallmainclassdllpath,
                    Config           = jsonConfig,
                    WorkDir          = fileinstallpath,
                    Cron             = taskruntimeinfo.TaskModel.taskcron,
                    NameSpace        = taskruntimeinfo.TaskModel.taskmainclassnamespace,
                    TaskDbConnection = GlobalConfig.TaskDataBaseConnectString,
                    TaskModel        = taskruntimeinfo.TaskModel
                };

                result = ProcessStart.GetInstance().Load(startupParam);



                StartProcess(taskid, taskruntimeinfo, result);
            }
            catch (Exception ex)
            {
                LogHelper.AddTaskLog($"节点开启任务失败{ex.Message}", taskid);
                throw;
            }
            bool r = TaskPoolManager.CreateInstance().Add(taskid.ToString(), taskruntimeinfo);

            SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
            {
                tb_task_dal taskdal = new tb_task_dal();
                //更新类型
                taskdal.Edit(c, taskruntimeinfo.TaskModel);
                taskdal.UpdateTaskState(c, taskid, (int)EnumTaskState.Running);
                taskdal.UpdateProcess(c, taskid, taskruntimeinfo.Process.Id);
                //程序集版本更新
                if (!string.IsNullOrEmpty(assemblyVersion))
                {
                    if (taskruntimeinfo.TaskVersionModel.assemblyversion != assemblyVersion)
                    {
                        taskruntimeinfo.TaskVersionModel.assemblyversion = assemblyVersion;
                        tb_version_dal versiondal = new tb_version_dal();
                        versiondal.UpdateAssemblyVersion(c, taskruntimeinfo.TaskVersionModel.id, assemblyVersion);
                    }
                }
            });
            //

            LogHelper.AddTaskLog("节点开启任务成功", taskid);
            return(r);
        }
        /// <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;

            XXF.Common.IOHelper.CreateDirectory(filelocalcachepath);
            XXF.Common.IOHelper.CreateDirectory(fileinstallpath);
            System.IO.File.WriteAllBytes(filelocalcachepath, taskruntimeinfo.TaskVersionModel.zipfile);

            CompressHelper.UnCompress(filelocalcachepath, fileinstallpath);
            //拷贝共享程序集
            XXF.Common.IOHelper.CopyDirectory(taskshareddlldir, fileinstallpath);
            try
            {
                var dlltask      = new AppDomainLoader <BaseDllTask>().Load(fileinstallmainclassdllpath, taskruntimeinfo.TaskModel.taskmainclassnamespace, out taskruntimeinfo.Domain);
                var sdktaskmodel = new XXF.BaseService.TaskManager.model.tb_task_model();
                PropertyHelper.Copy(taskruntimeinfo.TaskModel, sdktaskmodel);
                dlltask.SystemRuntimeInfo = new TaskSystemRuntimeInfo()
                {
                    TaskConnectString = GlobalConfig.TaskDataBaseConnectString,
                    TaskModel         = sdktaskmodel
                };

                dlltask.AppConfig = new TaskAppConfigInfo();
                if (!string.IsNullOrEmpty(taskruntimeinfo.TaskModel.taskappconfigjson))
                {
                    dlltask.AppConfig = new XXF.Serialization.JsonHelper().Deserialize <TaskAppConfigInfo>(taskruntimeinfo.TaskModel.taskappconfigjson);
                }
                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;
            }
        }
        /// <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;
             
            XXF.Common.IOHelper.CreateDirectory(filelocalcachepath);
            XXF.Common.IOHelper.CreateDirectory(fileinstallpath);
            System.IO.File.WriteAllBytes(filelocalcachepath, taskruntimeinfo.TaskVersionModel.zipfile);

            CompressHelper.UnCompress(filelocalcachepath, fileinstallpath);
            //拷贝共享程序集
            XXF.Common.IOHelper.CopyDirectory(taskshareddlldir, fileinstallpath);
            try
            {
                var dlltask = new AppDomainLoader<BaseDllTask>().Load(fileinstallmainclassdllpath, taskruntimeinfo.TaskModel.taskmainclassnamespace, out taskruntimeinfo.Domain);
                var sdktaskmodel = new XXF.BaseService.TaskManager.model.tb_task_model();
                PropertyHelper.Copy(taskruntimeinfo.TaskModel, sdktaskmodel);
                dlltask.SystemRuntimeInfo = new TaskSystemRuntimeInfo()
                {
                    TaskConnectString = GlobalConfig.TaskDataBaseConnectString,
                    TaskModel = sdktaskmodel
                };

                dlltask.AppConfig = new TaskAppConfigInfo();
                if (!string.IsNullOrEmpty(taskruntimeinfo.TaskModel.taskappconfigjson))
                {
                    dlltask.AppConfig = new XXF.Serialization.JsonHelper().Deserialize<TaskAppConfigInfo>(taskruntimeinfo.TaskModel.taskappconfigjson);
                }
                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;
            }
        }
 /// <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;
         }
         catch (Exception e) { LogHelper.AddNodeError("强制资源释放之任务资源释放", e); }
     if (taskruntimeinfo != null && taskruntimeinfo.Domain != null)
         try { new AppDomainLoader<BaseDllTask>().UnLoad(taskruntimeinfo.Domain); taskruntimeinfo.Domain = null; }
         catch (Exception e) { LogHelper.AddNodeError("强制资源释放之应用程序域释放", e); }
     if (TaskPoolManager.CreateInstance().Get(taskid.ToString()) != null)
         try { TaskPoolManager.CreateInstance().Remove(taskid.ToString()); }
         catch (Exception e) { LogHelper.AddNodeError("强制资源释放之任务池释放", e); }
     LogHelper.AddTaskLog("节点已对任务进行资源释放", taskid);
     return true;
 }