Exemple #1
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="taskId"></param>
 /// <param name="info"></param>
 /// <returns></returns>
 public bool Add(string taskId, NodeTaskRuntimeInfo info)
 {
     lock (_Lock)
     {
         if (!TaskRuntimePool.ContainsKey(taskId))
         {
             JobDetailImpl jobDetail = new JobDetailImpl(info.Task.Id.ToString(), info.Task.Category.Id.ToString(), typeof(TaskJob));
             var trigger = Tools.CronFactory.CreateTrigger(info);
             _Sched.ScheduleJob(jobDetail, trigger);
             TaskRuntimePool.Add(taskId, info);
             return true;
         }
         return false;
     }
 }
Exemple #2
0
 public static ITrigger CreateTrigger(NodeTaskRuntimeInfo info)
 {
     return null;
 }
Exemple #3
0
        /// <summary>
        /// 开启任务
        /// </summary>
        /// <param name="taskId"></param>
        public bool Start(int taskId)
        {
            var taskRuntime = TaskPoolManager.GetInstance().Get(taskId.ToString());
            if (taskRuntime != null)
            {
                throw new Exception("任务[" + taskRuntime.Task.TaskName + "]已在运行中");
            }

            taskRuntime = new NodeTaskRuntimeInfo();
            taskRuntime.TaskLock = new TaskLock();
            taskRuntime.Task = _Service.TaskService.GetTask(taskId);

            string localPath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\')
                                + "\\" + GlobalConfig.TaskDLLCacheDir
                                + "\\" + taskRuntime.Task.Id
                                + "\\" + taskRuntime.Task.TaskVersion.Id
                                + "\\" + taskRuntime.Task.TaskVersion.FileName;
            string installPath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\')
                                + "\\" + GlobalConfig.TaskDLLDir
                                + "\\" + taskRuntime.Task.Id;
            string sharePath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\')
                                + "\\" + GlobalConfig.TaskDLLShareDir;
            string mainClassPath = installPath + "\\" + taskRuntime.Task.TaskMainClassDLLName;

            IOHelper.DirMake(localPath);
            IOHelper.DirMake(installPath);
            File.WriteAllBytes(localPath, taskRuntime.Task.TaskVersion.ZipFile);
            CompressHelper.UnCompress(localPath, installPath);
            IOHelper.DirCopy(sharePath, installPath);

            try
            {
                var taskDLL = new AppDomainLoader<BaseTask>().Load(mainClassPath,
                            taskRuntime.Task.TaskMainClassNameSpace,
                            out taskRuntime.Domain);
                taskDLL.TaskRuntimeInfo = new TaskRuntimeInfo()
                {
                    TaskConnectString = GlobalConfig.TaskConnectString,
                    Task = new TaskManager.Model.Task()
                    {
                        Id = taskRuntime.Task.Id,
                        TaskName = taskRuntime.Task.TaskName,
                        Node_Id = taskRuntime.Task.Node.Id,
                        TaskCreateTime = taskRuntime.Task.TaskCreateTime,
                        TaskUpdateTime = taskRuntime.Task.TaskUpdateTime,
                        TaskLastStartTime = taskRuntime.Task.TaskLastStartTime,
                        TaskLastEndTime = taskRuntime.Task.TaskLastEndTime,
                        TaskLastErrorTime = taskRuntime.Task.TaskLastErrorTime,
                        TaskRunCount = taskRuntime.Task.TaskRunCount,
                        TaskErrorCount = taskRuntime.Task.TaskErrorCount,
                        TaskState = (int)taskRuntime.Task.TaskState,
                        TaskVersion_Id = taskRuntime.Task.TaskVersion.Id,
                        TaskConfiguration = taskRuntime.Task.TaskConfiguration,
                        TaskCron = taskRuntime.Task.TaskCron,
                        TaskMainClassDLLName = taskRuntime.Task.TaskMainClassDLLName,
                        TaskMainClassNameSpace = taskRuntime.Task.TaskMainClassNameSpace,
                        Creator_Id = taskRuntime.Task.Creator.Id,
                        CreateDate = taskRuntime.Task.CreateDate,
                        Remark = taskRuntime.Task.Remark
                    }
                };
                taskDLL.AppInfo = new TaskAppInfo();
                if (taskRuntime.Task.TaskConfiguration.IsNotNull())
                {
                    taskDLL.AppInfo = new JsonHelper().Deserialize<TaskAppInfo>(taskRuntime.Task.TaskConfiguration);
                }
                taskRuntime.TaskDLL = taskDLL;

                bool result = TaskPoolManager.GetInstance().Add(taskId.ToString(), taskRuntime);
                if (result)
                {
                    _Service.TaskService.UpdateTaskState(taskId, DataObject.Constants.TaskState.Running);
                }
                _Service.LogService.AddNodeLog(taskRuntime.Task.Node.Id,
                    "节点[" + taskRuntime.Task.Node.NodeName + "]开启任务[" + taskRuntime.Task.TaskName + "]成功!");
                return result;
            }
            catch (Exception ex)
            {
                Dispose(taskId, taskRuntime, true);
                throw ex;
            }
        }
Exemple #4
0
 /// <summary>
 /// 资源释放
 /// </summary>
 /// <param name="taskId"></param>
 /// <param name="info">任务运行时信息</param>
 /// <param name="isForce">强制释放标识</param>
 /// <returns></returns>
 protected bool Dispose(int taskId, NodeTaskRuntimeInfo info, bool isForce)
 {
     if (info != null && info.TaskDLL != null)
     {
         try
         {
             info.TaskDLL.Dispose();
             info.TaskDLL = null;
         }
         catch (TaskDisposeTimeOutException e)
         {
             _Service.LogService.AddNodeError(GlobalConfig.NodeId, "强制释放任务资源", e);
             if (!isForce) throw e;
         }
         catch (Exception ex)
         {
             _Service.LogService.AddNodeError(GlobalConfig.NodeId, "强制释放任务资源", ex);
         }
     }
     if (info != null && info.Domain != null)
     {
         try
         {
             new AppDomainLoader<BaseTask>().UnLoad(info.Domain);
             info.Domain = null;
         }
         catch (Exception e)
         {
             _Service.LogService.AddNodeError(GlobalConfig.NodeId, "强制释放应用程序域", e);
         }
     }
     if (TaskPoolManager.GetInstance().Get(taskId.ToString()) != null)
     {
         try
         {
             TaskPoolManager.GetInstance().Remove(taskId.ToString());
         }
         catch (Exception e)
         {
             _Service.LogService.AddNodeError(GlobalConfig.NodeId, "强制是否任务池", e);
         }
     }
     _Service.LogService.AddTaskLog(taskId, "节点[" + GlobalConfig.NodeId + "]已对任务[" + taskId + "]进行资源释放");
     return true;
 }