コード例 #1
0
ファイル: Saga.cs プロジェクト: wangscript007/Pole
        private async Task RecursiveCompensateActivity(ActivityWapper activityWapper)
        {
            var activityId = activityWapper.Id;

            if (activityWapper.ActivityStatus == ActivityStatus.ExecutingOvertime)
            {
                activityWapper.OvertimeCompensateTimes++;
            }
            else
            {
                activityWapper.CompensateTimes++;
            }
            try
            {
                if (activityWapper.ActivityStatus != ActivityStatus.ExecutingOvertime)
                {
                    activityWapper.ActivityStatus = ActivityStatus.Compensating;
                }
                await activityWapper.InvokeCompensate();

                if (activityWapper.ActivityStatus == ActivityStatus.ExecutingOvertime)
                {
                    // 超时 补偿次数已到
                    var isCompensated = activityWapper.OvertimeCompensateTimes >= poleSagasOption.MaxOvertimeCompensateTimes;
                    if (isCompensated)
                    {
                        activityWapper.ActivityStatus = ActivityStatus.Compensated;
                    }
                    await eventSender.ActivityOvertimeCompensated(activityId, isCompensated);
                }
                else
                {
                    activityWapper.ActivityStatus = ActivityStatus.Compensated;
                    await eventSender.ActivityCompensated(activityId);
                }
                var compensateActivity = GetNextCompensateActivity();
                if (compensateActivity == null)
                {
                    return;
                }
                await RecursiveCompensateActivity(compensateActivity);
            }
            catch (Exception exception)
            {
                activityWapper.ActivityStatus = ActivityStatus.CompensateAborted;
                // todo: 超时操作 如果出错可能 减少 补偿次数 这里 先不做处理
                if (activityWapper.CompensateTimes >= poleSagasOption.MaxCompensateTimes)
                {
                    // 此时 结束 saga 并且设置状态 为 Error
                    await eventSender.ActivityCompensateAborted(activityId, Id, exception.InnerException != null?exception.InnerException.Message + exception.StackTrace : exception.Message + exception.StackTrace);
                }
                else
                {
                    await eventSender.ActivityCompensateAborted(activityId, string.Empty, exception.InnerException != null?exception.InnerException.Message + exception.StackTrace : exception.Message + exception.StackTrace);
                }
            }
        }