コード例 #1
0
        public static ProcessStart GetInstance()
        {
            if (_instance == null)
            {
                _instance = new ProcessStart();
            }

            return(_instance);
        }
コード例 #2
0
        /// <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;
                    }
                }
            }
        }
コード例 #3
0
        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);
        }