Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

丁松杰 / Pole

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • Pole
  • src
  • Pole.Sagas.Client
  • EventSender.cs
Find file
BlameHistoryPermalink
  • dingsongjie's avatar
    完善功能 · 92eeb084
    dingsongjie committed 5 years ago
    92eeb084
EventSender.cs 5.08 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
using Grpc.Net.Client;
using Microsoft.Extensions.Options;
using Pole.Sagas.Client.Abstraction;
using Pole.Sagas.Core.Abstraction;
using Pole.Sagas.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using static Pole.Sagas.Server.Grpc.Saga;

namespace Pole.Sagas.Client
{
    public class EventSender : IEventSender
    {
        private readonly SagaClient sagaClient;
        public EventSender(SagaClient sagaClient)
        {
            this.sagaClient = sagaClient;
        }
        public async Task ActivityCompensateAborted(string activityId, string sagaId, string errors)
        {
            var result = await sagaClient.ActivityCompensateAbortedAsync(new Server.Grpc.ActivityCompensateAbortedRequest
            {
                ActivityId = activityId,
                Errors = errors,
                SagaId = sagaId
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task ActivityCompensated(string activityId)
        {
            var result = await sagaClient.ActivityCompensatedAsync(new Server.Grpc.ActivityCompensatedRequest
            {
                ActivityId = activityId,
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task ActivityExecuted(string activityId)
        {
            var result = await sagaClient.ActivityExecutedAsync(new Server.Grpc.ActivityExecutedRequest
            {
                ActivityId = activityId,
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task ActivityExecuteAborted(string activityId)
        {
            var result = await sagaClient.ActivityExecuteAbortedAsync(new Server.Grpc.ActivityExecuteAbortedRequest
            {
                ActivityId = activityId
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task ActivityExecuting(string activityId, string activityName, string sagaId, byte[] parameterData, int order, DateTime addTime, int executeTimes)
        {
            var result = await sagaClient.ActivityExecutingAsync(new Server.Grpc.ActivityExecutingRequest
            {
                ActivityId = activityId,
                ActivityName = activityName,
                AddTime = addTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                Order = order,
                ParameterData = Google.Protobuf.ByteString.CopyFrom(parameterData),
                SagaId = sagaId,
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task SagaEnded(string sagaId, DateTime ExpiresAt)
        {
            var result = await sagaClient.SagaEndedAsync(new Server.Grpc.SagaEndedRequest
            {
                SagaId = sagaId,
                ExpiresAt = ExpiresAt.ToString("yyyy-MM-dd HH:mm:ss.fff"),
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task SagaStarted(string sagaId, string serviceName, DateTime addTime)
        {
            var result = await sagaClient.SagaStartedAsync(new Server.Grpc.SagaStartedRequest
            {
                SagaId = sagaId,
                ServiceName = serviceName,
                AddTime = addTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task ActivityExecuteOvertime(string activityId, string name, byte[] parameterData, DateTime addTime)
        {
            var result = await sagaClient.ActivityExecuteOvertimeAsync(new Server.Grpc.ActivityExecuteOvertimeRequest
            {
                ActivityId = activityId,
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task ActivityRevoked(string activityId)
        {
            var result = await sagaClient.ActivityRevokedAsync(new Server.Grpc.ActivityRevokedRequest
            {
                ActivityId = activityId,
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }

        public async Task ActivityCompensating(string activityId, int compensateTimes)
        {
            var result = await sagaClient.ActivityCompensatingAsync(new Server.Grpc.ActivityCompensatingRequest
            {
                ActivityId = activityId,
            });
            if (!result.IsSuccess)
            {
                throw new SagasServerException(result.Errors);
            }
        }
    }
}