/// <summary> /// 同步执行作业 /// </summary> /// <param name="task"></param> /// <returns>已经完成的任务</returns> public SysAccomplishedTask Execute(SysTask task) { SysAccomplishedTask result = null; Stopwatch sw = new Stopwatch(); sw.Start(); try { BeforeExecute(task); OnExecute(task); result = OnAfterExecute(task); } catch (System.Exception ex) { result = OnError(task, ex); } finally { sw.Stop(); Debug.WriteLine("执行任务,Duration={0:#,##0}ms, ID={1}, Name={2}, Type={3}", sw.ElapsedMilliseconds, task.TaskID, task.TaskTitle, task.TaskType); } return(result); }
/// <summary> /// 移动到已经完成系统任务中 /// </summary> /// <param name="taskID">被移动的任务的ID</param> /// <param name="status">重置任务的状态</param> public SysAccomplishedTask MoveToCompletedSysTask(SysTask task, SysTaskStatus status, string statusText) { SysAccomplishedTask result = null; PerformanceMonitorHelper.GetDefaultMonitor().WriteExecutionDuration(string.Format("MoveToAccomplished({0})", this.GetType().FullName), () => { Dictionary <string, object> context = new Dictionary <string, object>(); ORMappingItemCollection mappings = GetMappingInfo(context); WhereSqlClauseBuilder builder = ORMapping.GetWhereSqlClauseBuilderByPrimaryKey(task, mappings); ORMappingItemCollection mappingsCompleted = ORMapping.GetMappingInfo <SysAccomplishedTask>(); SysAccomplishedTask taskCompleted = new SysAccomplishedTask(task); taskCompleted.Status = status; taskCompleted.StatusText = statusText; StringBuilder sql = new StringBuilder(); sql.AppendFormat("DELETE FROM {0} WHERE {1}", mappingsCompleted.TableName, builder.ToSqlString(TSqlBuilder.Instance)); sql.Append(TSqlBuilder.Instance.DBStatementSeperator); sql.Append(GetMoveSysTaskSql(taskCompleted, context)); sql.Append(TSqlBuilder.Instance.DBStatementSeperator); sql.Append("IF @@ROWCOUNT = 0"); sql.Append(TSqlBuilder.Instance.DBStatementSeperator); sql.Append("\t" + ORMapping.GetInsertSql(taskCompleted, mappingsCompleted, TSqlBuilder.Instance)); sql.Append(TSqlBuilder.Instance.DBStatementSeperator); sql.AppendFormat("DELETE FROM {0} WHERE {1}", mappings.TableName, builder.ToSqlString(TSqlBuilder.Instance)); using (TransactionScope scope = TransactionScopeFactory.Create()) { DbHelper.RunSql(sql.ToString(), this.GetConnectionName()); scope.Complete(); } result = taskCompleted; }); return(result); }
/// <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); } } }
private string GetMoveSysTaskSql(SysAccomplishedTask taskCompleted, Dictionary <string, object> context) { ORMappingItemCollection mappings = GetMappingInfo(context); ORMappingItemCollection mappingsCompleted = ORMapping.GetMappingInfo <SysAccomplishedTask>(); InsertSqlClauseBuilder insertBuilder = ORMapping.GetInsertSqlClauseBuilder(taskCompleted, "EndTime", "Status", "StatusText"); string[] fields = insertBuilder.GetAllDataFields(); string affectedFields = string.Join(",", fields); string sql = string.Format("INSERT INTO {0}({1}, END_TIME, STATUS, STATUS_TEXT) SELECT {2}, GETDATE() AS END_TIME, {3} AS STATUS, {4} AS STATUS_TEXT FROM {5} WHERE TASK_GUID = {6}", mappingsCompleted.TableName, affectedFields, affectedFields, TSqlBuilder.Instance.CheckUnicodeQuotationMark(taskCompleted.Status.ToString()), TSqlBuilder.Instance.CheckUnicodeQuotationMark(taskCompleted.StatusText), mappings.TableName, TSqlBuilder.Instance.CheckUnicodeQuotationMark(taskCompleted.TaskID)); return(sql); }