Ejemplo n.º 1
0
        public static void ExecuteTask(SysTask task)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            try
            {
                SysTask taskLoaded = SysTaskAdapter.Instance.Load(task.TaskID);

                ISysTaskExecutor executor = SysTaskSettings.GetSettings().GetExecutor(taskLoaded.TaskType);

                Console.WriteLine("执行任务:ID={0}, Name={1}, Type={2}",
                                  taskLoaded.TaskID, taskLoaded.TaskTitle, taskLoaded.TaskType);

                executor.Execute(taskLoaded);

                SysAccomplishedTask accomplishedTask = SysAccomplishedTaskAdapter.Instance.Load(taskLoaded.TaskID);

                Assert.IsNotNull(accomplishedTask);

                if (accomplishedTask.StatusText.IsNotEmpty())
                {
                    Console.WriteLine(accomplishedTask.StatusText);
                }
            }
            finally
            {
                sw.Stop();
                Console.WriteLine("执行任务:ID={0}经过的时间为{1:#,##0毫秒}", task.TaskID, sw.ElapsedMilliseconds);
                Console.WriteLine();
            }
        }
Ejemplo n.º 2
0
        private static void ExecuteTasks(IEnumerable <DO.SysTask> tasks)
        {
            foreach (DO.SysTask task in tasks)
            {
                ISysTaskExecutor executor = SysTaskSettings.GetSettings().GetExecutor(task.TaskType);

                executor.Execute(task);
            }
        }
Ejemplo n.º 3
0
        private static void ExecuteTask(SysTask task)
        {
            try
            {
                ISysTaskExecutor executor = SysTaskSettings.GetSettings().GetExecutor(task.TaskType);

                executor.BeforeExecute(task);
                Task.Factory.StartNew(() => executor.Execute(task));
            }
            catch (System.Exception ex)
            {
                SysTaskAdapter.Instance.MoveToCompletedSysTask(task.TaskID, SysTaskStatus.Aborted, ex.GetRealException().ToString());
            }
        }
Ejemplo n.º 4
0
        public void InvokeServiceTaskTest()
        {
            //准备Job信息
            //调用SysTaskSettings.GetSettings().GetExecutor("InvokeService")执行服务
            //检验状态

            InvokeWebServiceJob job = new InvokeWebServiceJob()
            {
                JobID            = UuidHelper.NewUuidString(),
                JobType          = JobType.InvokeService,
                Category         = "测试权限中心服务",
                Description      = "仅用于测试",
                Enabled          = true,
                Name             = "任务和作业的单元测试",
                SvcOperationDefs = new Workflow.WfServiceOperationDefinitionCollection()
            };

            WfServiceOperationParameterCollection parameters = new WfServiceOperationParameterCollection();

            parameters.Add(new WfServiceOperationParameter()
            {
                Name = "callerID", Type = WfSvcOperationParameterType.RuntimeParameter, Value = "callerID"
            });

            job.SvcOperationDefs.Add(new Workflow.WfServiceOperationDefinition(new Workflow.WfServiceAddressDefinition(Workflow.WfServiceRequestMethod.Post, null,
                                                                                                                       "http://localhost/MCSWebApp/PermissionCenterServices/services/PermissionCenterToADService.asmx"), "GetVersion", parameters, string.Empty));
            InvokeWebServiceJobAdapter.Instance.Update(job);

            DO.SysTask task = new DO.SysTask()
            {
                TaskID     = UuidHelper.NewUuidString(),
                TaskTitle  = "测试任务",
                ResourceID = job.JobID
            };

            SysTaskAdapter.Instance.Update(task);

            ISysTaskExecutor executor = SysTaskSettings.GetSettings().GetExecutor("InvokeService");

            executor.Execute(task);

            var task2 = SysTaskAdapter.Instance.Load(task.TaskID);

            Assert.IsNull(task2);
        }
Ejemplo n.º 5
0
        private static void ExecuteTask(SysTask task)
        {
            try
            {
                //2014-4-19 by haoyk
                //当作业还没执行完成,将不再执行此任务
                if (task.Status != SysTaskStatus.Running)
                {
                    ISysTaskExecutor executor = SysTaskSettings.GetSettings().GetExecutor(task.TaskType);

                    executor.BeforeExecute(task);
                    Task.Factory.StartNew(() => executor.Execute(task));
                }
            }
            catch (System.Exception ex)
            {
                SysTaskAdapter.Instance.MoveToCompletedSysTask(task.TaskID, SysTaskStatus.Aborted, ex.GetRealException().ToString());
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 执行并且验证Task的返回结果
        /// </summary>
        /// <param name="func"></param>
        private static void ExecuteAndAssertTask(Func <DO.SysTask> func)
        {
            func.NullCheck("func");

            DO.SysTask task = func();

            DO.SysTask taskLoaded = SysTaskAdapter.Instance.Load(task.TaskID);

            ISysTaskExecutor executor = SysTaskSettings.GetSettings().GetExecutor(taskLoaded.TaskType);

            executor.Execute(taskLoaded);

            SysAccomplishedTask accomplishedTask = SysAccomplishedTaskAdapter.Instance.Load(taskLoaded.TaskID);

            Assert.IsNotNull(accomplishedTask);

            Console.WriteLine(accomplishedTask.StatusText);
            Assert.AreEqual(SysTaskStatus.Completed, accomplishedTask.Status);
        }
Ejemplo n.º 7
0
        public void InvokeServiceTaskExecuteTest()
        {
            DO.InvokeServiceTask task = PrepareTask();

            DO.InvokeServiceTaskAdapter.Instance.Update(task);

            DO.SysTask taskLoaded = DO.SysTaskAdapter.Instance.Load(task.TaskID);

            ISysTaskExecutor executor = SysTaskSettings.GetSettings().GetExecutor(taskLoaded.TaskType);

            executor.Execute(taskLoaded);

            Console.WriteLine(WfServiceInvoker.InvokeContext[task.SvcOperationDefs.FirstOrDefault().RtnXmlStoreParamName]);

            SysAccomplishedTask accomplishedTask = SysAccomplishedTaskAdapter.Instance.Load(taskLoaded.TaskID);

            Assert.IsNotNull(accomplishedTask);
            Assert.AreEqual(taskLoaded.Data, accomplishedTask.Data);

            Console.WriteLine(accomplishedTask.StatusText);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 执行任务
        /// </summary>
        public void ExecuteTask()
        {
            this.Status = SysTaskActivityStatus.Running;

            if (this.Task != null)
            {
                try
                {
                    //不需要保存在待办任务中
                    //SysTaskAdapter.Instance.Update(this.Task);

                    ISysTaskExecutor executor = SysTaskSettings.GetSettings().GetExecutor(this.Task.TaskType);

                    SysAccomplishedTask accomplishedTask = executor.Execute(this.Task);

                    //SysAccomplishedTask accomplishedTask = SysAccomplishedTaskAdapter.Instance.Load(this.Task.TaskID);

                    if (accomplishedTask != null && accomplishedTask.Status == SysTaskStatus.Aborted)
                    {
                        throw new ApplicationException(string.Format("执行ID为{0}的任务失败,错误为\n{1}",
                                                                     accomplishedTask.TaskID, accomplishedTask.StatusText));
                    }
                }
                catch (System.Exception ex)
                {
                    DateTime now = SNTPClient.AdjustedTime;

                    this.Status     = SysTaskActivityStatus.Aborted;
                    this.EndTime    = now;
                    this.StatusText = ex.ToString();

                    SysTaskProcessRuntime.ProcessContext.AffectedActivities.AddOrReplace(this);

                    this.Process.Status  = SysTaskProcessStatus.Aborted;
                    this.Process.EndTime = now;
                    SysTaskProcessRuntime.ProcessContext.AffectedProcesses.AddOrReplace(this.Process);
                }
            }
        }
Ejemplo n.º 9
0
        public void InvokeHugeServiceTasksTest()
        {
            SysTaskAdapter.Instance.ClearAll();
            SysAccomplishedTaskAdapter.Instance.ClearAll();

            InvokeWebServiceJob job = new InvokeWebServiceJob()
            {
                JobID            = UuidHelper.NewUuidString(),
                JobType          = JobType.InvokeService,
                Category         = "测试权限中心服务",
                Description      = "仅用于测试",
                Enabled          = true,
                Name             = "任务和作业的单元测试",
                SvcOperationDefs = new Workflow.WfServiceOperationDefinitionCollection()
            };

            job.SvcOperationDefs.Add(new Workflow.WfServiceOperationDefinition(new Workflow.WfServiceAddressDefinition(Workflow.WfServiceRequestMethod.Post, null, "http://localhost/MCSWebApp/PermissionCenterServices/services/PermissionCenterToADService.asmx"), "GetVersion", null, string.Empty));
            InvokeWebServiceJobAdapter.Instance.Update(job);

            for (int i = 0; i < 400; i++)
            {
                DO.SysTask task = new DO.SysTask()
                {
                    TaskID     = UuidHelper.NewUuidString(),
                    TaskTitle  = "测试任务" + i,
                    ResourceID = job.JobID,
                    TaskType   = "InvokeService"
                };

                SysTaskAdapter.Instance.Update(task);
            }

            int count = 0;
            var tasks = SysTaskAdapter.Instance.FetchNotRuningSysTasks(800, m =>
            {
                SysTaskAdapter.Instance.UpdateStatus(m.TaskID, SysTaskStatus.Running);
            });
            Thread thread = Thread.CurrentThread;

            Assert.AreEqual(400, tasks.Count);

            Debug.WriteLine(DateTime.Now + "开始执行");
            Stopwatch watch = new Stopwatch();

            watch.Start();

            var result = System.Threading.Tasks.Parallel.ForEach <DO.SysTask>(tasks, t =>
            {
                if (t.TaskType == "InvokeService")
                {
                    Debug.WriteLine(DateTime.Now + "当前执行:" + System.Threading.Interlocked.Increment(ref count) + "  主线程状态" + thread.ThreadState + "当前线程ID:" + Thread.CurrentThread.ManagedThreadId);
                    ISysTaskExecutor exec = SysTaskSettings.GetSettings().GetExecutor("InvokeService");
                    exec.Execute(t);
                }
            });

            watch.Stop();

            Debug.WriteLine(string.Format("已完成所有任务: {0} 耗时 {1}ms", result.IsCompleted, watch.ElapsedMilliseconds));
            Debug.WriteLine(DateTime.Now + "主线程执行结束");
        }
Ejemplo n.º 10
0
        public void InvokeHugeServiceTasksTest2()
        {
            SysTaskAdapter.Instance.ClearAll();
            SysAccomplishedTaskAdapter.Instance.ClearAll();

            InvokeWebServiceJob job = new InvokeWebServiceJob()
            {
                JobID            = UuidHelper.NewUuidString(),
                JobType          = JobType.InvokeService,
                Category         = "测试权限中心服务",
                Description      = "仅用于测试",
                Enabled          = true,
                Name             = "任务和作业的单元测试",
                SvcOperationDefs = new Workflow.WfServiceOperationDefinitionCollection()
            };

            job.SvcOperationDefs.Add(new Workflow.WfServiceOperationDefinition(new Workflow.WfServiceAddressDefinition(Workflow.WfServiceRequestMethod.Post, null, "http://localhost/MCSWebApp/PermissionCenterServices/services/PermissionCenterToADService.asmx"), "GetVersion", null, string.Empty));
            InvokeWebServiceJobAdapter.Instance.Update(job);

            for (int i = 0; i < 1500; i++)
            {
                DO.SysTask task = new DO.SysTask()
                {
                    TaskID     = UuidHelper.NewUuidString(),
                    TaskTitle  = "测试任务" + i,
                    ResourceID = job.JobID,
                    TaskType   = "InvokeService"
                };

                SysTaskAdapter.Instance.Update(task);
            }

            int count = 0;

            var tasks = SysTaskAdapter.Instance.FetchNotRuningSysTasks(-1, m =>
            {
                SysTaskAdapter.Instance.UpdateStatus(m.TaskID, SysTaskStatus.Running);
            });

            Debug.WriteLine(DateTime.Now + "开始执行");
            Stopwatch watch = new Stopwatch();

            watch.Start();
            using (AutoResetEvent eventAuto = new AutoResetEvent(false))
            {
                foreach (var task in tasks)
                {
                    ThreadPool.QueueUserWorkItem(item =>
                    {
                        DO.SysTask curTask = (DO.SysTask)item;
                        if (curTask.TaskType == "InvokeService")
                        {
                            try
                            {
                                ISysTaskExecutor exec = SysTaskSettings.GetSettings().GetExecutor("InvokeService");
                                exec.Execute(curTask);
                            }
                            catch (Exception exx)
                            {
                                Debug.WriteLine("出现了意外的错误" + exx.ToString());
                            }
                            finally
                            {
                                int newCount = System.Threading.Interlocked.Increment(ref count);
                                Debug.WriteLine(DateTime.Now + "当前执行:" + newCount + "当前线程ID:" + Thread.CurrentThread.ManagedThreadId);
                                eventAuto.Set();
                            }
                        }
                    }, task);
                }

                int workerThreads, completionPortThreads;
                ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);

                Debug.WriteLine("线程池可用线程数" + workerThreads);

                while (count != tasks.Count)
                {
                    eventAuto.WaitOne();
                }
            }

            watch.Stop();

            Debug.WriteLine(string.Format("耗时 {0}ms", watch.ElapsedMilliseconds));
            Debug.WriteLine(DateTime.Now + "主线程执行结束");
        }