Beispiel #1
0
        public bool Stop(long taskid)
        {
            NodeTaskRuntimeInfo nodetask = TaskPool.Instance().Get(taskid.ToString());

            if (nodetask == null)
            {
                LogHelper.WriteInfo("" + taskid + "任务没有在运行");
                return(false);
            }
            if (Dispose(taskid, nodetask))
            {
                try
                {
                    TaskDal   taskdal = new TaskDal();
                    TaskModel model   = taskdal.GetById(GlobalConfig.TaskDataBaseConnectString, taskid.ToString());
                    model.TaskState    = 0;
                    model.TaskStopTime = DateTime.Now;
                    taskdal.EditTask(GlobalConfig.TaskDataBaseConnectString, model);
                    return(true);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteError(ex);
                    return(false);
                }
            }
            return(false);
        }
Beispiel #2
0
 public bool Dispose(long taskid, NodeTaskRuntimeInfo nodetask)
 {
     if (nodetask != null && nodetask.Domain != null)
     {
         try
         {
             TaskPool.Instance().Remove(taskid.ToString());
             AppDomain.Unload(nodetask.Domain);
             return(true);
         }
         catch (Exception ex)
         {
             LogHelper.WriteError(ex);
             return(false);
         }
     }
     return(false);
 }
Beispiel #3
0
        /// <summary>
        /// 任务开启
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns></returns>
        public bool Start(long taskid)
        {
            LogHelper.WriteInfo("启动" + taskid + "任务");
            try
            {
                NodeTaskRuntimeInfo nodetask = TaskPool.Instance().Get(taskid.ToString());
                if (nodetask != null)
                {
                    LogHelper.WriteInfo("" + taskid + "任务已经开启");
                    return(false);
                }
                TaskDal taskdal = new TaskDal();
                nodetask           = new NodeTaskRuntimeInfo();
                nodetask.Tasklock  = new TaskLock();
                nodetask.TaskModel = taskdal.GetById(GlobalConfig.TaskDataBaseConnectString, taskid.ToString());
                string filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, GlobalConfig.TaskDLL, nodetask.TaskModel.Id.ToString());  //任务dll在节点service存放地址
                string srcpath  = Path.Combine(nodetask.TaskModel.TaskClassPath, nodetask.TaskModel.TaskFileName);
                DeCompressionHelper.UnCompress(srcpath, filepath);

                AppDomainSetup setup = new AppDomainSetup();
                setup.ShadowCopyFiles = "true";
                setup.ApplicationBase = System.IO.Path.GetDirectoryName(filepath);

                var         appdomain = AppDomain.CreateDomain(Path.Combine(filepath, nodetask.TaskModel.TaskClassNamespace), null, setup);
                BaseTaskDLL taskdll   = (BaseTaskDLL)appdomain.CreateInstanceFromAndUnwrap(Path.Combine(filepath, nodetask.TaskModel.TaskFileName), nodetask.TaskModel.TaskClassNamespace);

                nodetask.TaskDLL = taskdll;

                TaskPool.Instance().Add(taskid.ToString(), nodetask);

                LogHelper.WriteInfo("节点开启成功");
                return(true);
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(ex);
                return(false);
            }
        }
Beispiel #4
0
        public void Execute(JobExecutionContext context)
        {
            string taskid = context.JobDetail.Name;
            NodeTaskRuntimeInfo nodetask = TaskPool.Instance().Get(taskid);

            LogHelper.WriteInfo("TaskJob运行Execute;context.JobDetail.Name" + context.JobDetail.Name + "nodetask" + nodetask);
            nodetask.Tasklock.Invoke(() =>
            {
                try
                {
                    nodetask.TaskDLL.StartRun();
                }
                catch (Exception exp)
                {
                    LogHelper.WriteError("任务" + taskid + "TaskJob回调时执行失败");
                }
            });
            //lock (lock_node)
            //{
            //    LogHelper.WriteDebug("准备运行一次:" + nodetask.TaskDLL.GetType().ToString());
            //    //nodetask.TaskDLL.StartRun();
            //}
        }