예제 #1
0
        protected virtual async Task <TaskResult> CheckDependenciesAsync(int orderId)
        {
            TaskResult taskResult = new TaskResult
            {
                ResultStatus = SmsTaskStatus.Success
            };

            if (DependencyTasks.Count == 0)
            {
                return(taskResult);
            }

            foreach (var dependency in DependencyTasks)
            {
                taskResult = await CheckDependencyAsync(orderId, dependency);

                if (taskResult.ResultStatus == SmsTaskStatus.WaitForDependency)
                {
                    break;
                }
            }

            return(taskResult);
        }
예제 #2
0
        protected virtual async Task StartOrderTasksAsync()
        {
            PortalOrder order = await GetNextOrderAsync();

            StringBuilder message  = new StringBuilder();
            string        taskName = string.Empty;

            while (order != null)
            {
                var lastTaskStatus = SmsTaskStatus.Success;

                message.Remove(0, message.Length);
                OnDisplayMessage($"[Order:{order.OrderId}]:start processing");
                try
                {
                    await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                    {
                        OrderId     = order.OrderId,
                        Comment     = "Start Processing Order",
                        CreatedTime = DateTime.Now,
                        CreatedBy   = LogUserName
                    });

                    foreach (SmsBaseTask task in Tasks)
                    {
                        if (!CancellationToken.IsCancellationRequested)
                        {
                            taskName = task.TaskName;

                            await TaskRepository.UpdateOrderStatusAsync(order.OrderId,
                                                                        $"SmsTaskStatus.Processing:{task.TaskName}",
                                                                        LogUserName);

                            TaskResult result = await task.ProcessTaskActionsAsync(order.OrderId);

                            lastTaskStatus = result.ResultStatus;

                            if (result.ResultStatus != SmsTaskStatus.Success &&
                                result.ResultStatus != SmsTaskStatus.CouldRunNext)
                            {
                                message.AppendLine(JsonConvert.SerializeObject(result));
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    lastTaskStatus = SmsTaskStatus.Error;
                    message.Append($"{ex.Message} : \r\n {ex.StackTrace}");
                    if (ex.InnerException != null)
                    {
                        message.Append($"\r\n{ex.InnerException.Message}\r\n {ex.InnerException.StackTrace}");
                    }
                }

                OnDisplayMessage($"[Order:{order.OrderId}]:{lastTaskStatus.ToString()}");

                if (lastTaskStatus != SmsTaskStatus.WaitForDependency)
                {
                    await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                    {
                        OrderId     = order.OrderId,
                        Comment     = $"Finished Processing Order, result:{lastTaskStatus}",
                        Details     = message.ToString(),
                        CreatedTime = DateTime.Now,
                        CreatedBy   = LogUserName
                    });
                }

                if (lastTaskStatus == SmsTaskStatus.Error)
                {
                    await EmailSender.SendEmailAsync(
                        $"[Order id: {order.OrderId}] Task Error: {taskName}",
                        message.ToString());
                }

                //update order status
                await TaskRepository.UpdateOrderStatusAsync(order.OrderId,
                                                            (lastTaskStatus == SmsTaskStatus.Error
                        ? $"{lastTaskStatus}:{taskName}"
                        : lastTaskStatus.ToString()), LogUserName);

                if (CancellationToken.IsCancellationRequested)
                {
                    break;
                }
                else
                {
                    order = await GetNextOrderAsync();
                }
            }
        }
예제 #3
0
        public async Task <TaskResult> ProcessTaskActionsAsync(int orderId)
        {
            OnDisplayMessage($"[Order:{orderId}]start processing {TaskName}");

            if (CancellationToken.IsCancellationRequested)
            {
                return(new TaskResult
                {
                    ResultStatus = SmsTaskStatus.TaskCancelled
                });
            }

            await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
            {
                OrderId     = orderId,
                Comment     = "Start Order Prechecking",
                CreatedTime = DateTime.Now,
                CreatedBy   = LogUserName
            });

            TaskResult result = await TaskPrecheckAsync(orderId);

            if (result.ResultStatus == SmsTaskStatus.WaitForDependency)
            {
                if (!await TaskRepository.AnyOrderActionLogAsync(orderId))
                {
                    //check dependency only need save it once, otherwise too many check dependency logs
                    await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                    {
                        OrderId     = orderId,
                        Comment     = result.Message,
                        CreatedTime = DateTime.Now,
                        CreatedBy   = LogUserName
                    });
                }
            }
            else
            {
                await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                {
                    OrderId     = orderId,
                    Comment     = "Start Order Prechecking",
                    Details     = JsonConvert.SerializeObject(result),
                    CreatedTime = DateTime.Now,
                    CreatedBy   = LogUserName
                });
            }

            if (result.ResultStatus != SmsTaskStatus.Success && result.ResultStatus != SmsTaskStatus.CouldRunNext)
            {
                return(result);
            }

            var tasks = await TaskRepository.GetOrderTasksAsync(orderId, TaskName);

            var lastTaskStatus = SmsTaskStatus.Success;

            //update
            OnDisplayMessage($"[Order:{orderId}, Task:{TaskName}]:start processing");
            StringBuilder message = new StringBuilder();

            foreach (var task in tasks)
            {
                if (!CancellationToken.IsCancellationRequested)
                {
                    TaskResult actionResult = await ProcessTaskActionAsync(task);

                    lastTaskStatus = actionResult.ResultStatus;
                    if (lastTaskStatus != SmsTaskStatus.Success && lastTaskStatus != SmsTaskStatus.CouldRunNext)
                    {
                        message.AppendLine(actionResult.Message);
                        break;
                    }
                }
            }

            if (lastTaskStatus == SmsTaskStatus.Success || lastTaskStatus == SmsTaskStatus.CouldRunNext)
            {
                result = await TaskPostcheckAsync(orderId);

                lastTaskStatus = result.ResultStatus;
                message.AppendLine(result.Message);

                await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                {
                    OrderId     = orderId,
                    Details     = result.Message,
                    Comment     = $"PostCheck:{lastTaskStatus}",
                    CreatedTime = DateTime.Now,
                    CreatedBy   = LogUserName
                });
            }

            return(new TaskResult
            {
                ResultStatus = lastTaskStatus,
                Message = message.ToString()
            });
        }
예제 #4
0
        public async Task <TaskResult> ProcessAsync(int taskId)
        {
            var task = await TaskRepository.GetTaskAsync(taskId);

            if (task == null)
            {
                OnDisplayMessage($"[Task:{taskId}]Could not find Task information");
                return(new TaskResult
                {
                    ResultStatus = SmsTaskStatus.Error,
                    Message = $"[Task:{taskId}]Could not find Task information"
                });
            }

            OnDisplayMessage($"[{task.OrderId}.{task.OrderTaskId}]start processing {TaskActionName}");

            TaskResult result = await TaskActionPrecheckAsync(taskId);

            if (result.ResultStatus != SmsTaskStatus.WaitForDependency)
            {
                //check dependency only need save it once, otherwise too many check dependency logs
                await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                {
                    OrderId     = task.OrderId,
                    OrderTaskId = taskId,
                    Comment     = $"{TaskActionName} Precheck result: {result.ResultStatus.ToString()}",
                    Details     = result.Message,
                    CreatedTime = DateTime.Now,
                    CreatedBy   = LogUserName
                });
            }

            if (result.ResultStatus != SmsTaskStatus.Success && result.ResultStatus != SmsTaskStatus.CouldRunNext)
            {
                return(result);
            }

            var actions = await TaskRepository.GetTaskActionsAsync(taskId, TaskActionName);

            //
            OnDisplayMessage($"[Order:{task.OrderId}, Task:{taskId}]{TaskActionName} :start processing");
            StringBuilder message = new StringBuilder();

            var lastActionStatus = SmsTaskStatus.Success;

            foreach (var action in actions)
            {
                if (await PreviouseRunAsync(taskId))
                {
                    continue;
                }

                TaskResult actionResult = await ProcessTaskActionAsync(action.OrderTaskActionId);

                await TaskRepository.UpdateTaskActionStatusAsync(action.OrderTaskActionId, actionResult.ResultStatus.ToString(),
                                                                 actionResult.RequestData, actionResult.ResponseData, LogUserName);

                await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                {
                    OrderId           = task.OrderId,
                    OrderTaskId       = taskId,
                    OrderTaskActionId = action.OrderTaskActionId,
                    Comment           = JsonConvert.SerializeObject(actionResult),
                    Details           = result.Message,
                    CreatedTime       = DateTime.Now,
                    CreatedBy         = LogUserName
                });


                lastActionStatus = actionResult.ResultStatus;

                var addNextActions = await CreateFollowingTaskActionsIfNeededAsync(action);

                await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                {
                    OrderId           = task.OrderId,
                    OrderTaskId       = taskId,
                    OrderTaskActionId = action.OrderTaskActionId,
                    Comment           = $"Create following tasks:{addNextActions.ResultStatus.ToString()}",
                    Details           = addNextActions.Message,
                    CreatedTime       = DateTime.Now,
                    CreatedBy         = LogUserName
                });

                if (lastActionStatus != SmsTaskStatus.Success && lastActionStatus != SmsTaskStatus.CouldRunNext)
                {
                    message.AppendLine(actionResult.Message);
                    break;
                }
            }

            if (lastActionStatus == SmsTaskStatus.Success || lastActionStatus == SmsTaskStatus.CouldRunNext)
            {
                result = await TaskActionPostcheckAsync(taskId);

                lastActionStatus = result.ResultStatus;
                message.AppendLine(result.Message);

                await TaskRepository.InsertOrderActionLogAsync(new OrderActionLog
                {
                    OrderId     = task.OrderId,
                    OrderTaskId = taskId,
                    Comment     = $"{TaskActionName} Postcheck result: {result.ResultStatus.ToString()}",
                    Details     = result.Message,
                    CreatedTime = DateTime.Now,
                    CreatedBy   = LogUserName
                });
            }

            result.ResultStatus = lastActionStatus;
            result.Message      = message.ToString();
            return(result);
        }