|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Amazon; |
| 7 | +using Amazon.Kinesis; |
| 8 | +using Amazon.Kinesis.Model; |
| 9 | +using Amazon.Runtime; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Newtonsoft.Json; |
| 12 | +using WorkflowCore.Interface; |
| 13 | +using WorkflowCore.Models.LifeCycleEvents; |
| 14 | +using WorkflowCore.Providers.AWS.Interface; |
| 15 | + |
| 16 | +namespace WorkflowCore.Providers.AWS.Services |
| 17 | +{ |
| 18 | + public class KinesisProvider : ILifeCycleEventHub |
| 19 | + { |
| 20 | + private readonly ILogger _logger; |
| 21 | + private Queue<Action<LifeCycleEvent>> _deferredSubscribers = new Queue<Action<LifeCycleEvent>>(); |
| 22 | + private readonly string _streamName; |
| 23 | + private readonly string _appName; |
| 24 | + private readonly JsonSerializer _serializer; |
| 25 | + private readonly IKinesisStreamConsumer _consumer; |
| 26 | + private readonly AmazonKinesisClient _client; |
| 27 | + private readonly int _defaultShardCount = 1; |
| 28 | + private bool _started = false; |
| 29 | + |
| 30 | + public KinesisProvider(AWSCredentials credentials, RegionEndpoint region, string appName, string streamName, IKinesisStreamConsumer consumer, ILoggerFactory logFactory) |
| 31 | + { |
| 32 | + _logger = logFactory.CreateLogger(GetType()); |
| 33 | + _appName = appName; |
| 34 | + _streamName = streamName; |
| 35 | + _consumer = consumer; |
| 36 | + _serializer = new JsonSerializer(); |
| 37 | + _serializer.TypeNameHandling = TypeNameHandling.All; |
| 38 | + _client = new AmazonKinesisClient(credentials, region); |
| 39 | + } |
| 40 | + |
| 41 | + public async Task PublishNotification(LifeCycleEvent evt) |
| 42 | + { |
| 43 | + using (var stream = new MemoryStream()) |
| 44 | + { |
| 45 | + var writer = new StreamWriter(stream); |
| 46 | + _serializer.Serialize(writer, evt); |
| 47 | + writer.Flush(); |
| 48 | + |
| 49 | + var response = await _client.PutRecordAsync(new PutRecordRequest() |
| 50 | + { |
| 51 | + StreamName = _streamName, |
| 52 | + PartitionKey = evt.WorkflowInstanceId, |
| 53 | + Data = stream |
| 54 | + }); |
| 55 | + |
| 56 | + //if (response.HttpStatusCode != System.Net.HttpStatusCode.OK) |
| 57 | + //{ |
| 58 | + // _logger.LogWarning($"Failed to send event to Kinesis {response.HttpStatusCode}"); |
| 59 | + //} |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public void Subscribe(Action<LifeCycleEvent> action) |
| 64 | + { |
| 65 | + if (_started) |
| 66 | + { |
| 67 | + _consumer.Subscribe(_appName, _streamName, record => Consume(record, action)); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + _deferredSubscribers.Enqueue(action); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public async Task Start() |
| 76 | + { |
| 77 | + await EnsureStream(); |
| 78 | + _started = true; |
| 79 | + while (_deferredSubscribers.Count > 0) |
| 80 | + { |
| 81 | + var action = _deferredSubscribers.Dequeue(); |
| 82 | + await _consumer.Subscribe(_appName, _streamName, record => Consume(record, action)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public Task Stop() |
| 87 | + { |
| 88 | + _started = false; |
| 89 | + return Task.CompletedTask; |
| 90 | + } |
| 91 | + |
| 92 | + private async Task EnsureStream() |
| 93 | + { |
| 94 | + try |
| 95 | + { |
| 96 | + await _client.DescribeStreamSummaryAsync(new DescribeStreamSummaryRequest() |
| 97 | + { |
| 98 | + StreamName = _streamName |
| 99 | + }); |
| 100 | + } |
| 101 | + catch (ResourceNotFoundException) |
| 102 | + { |
| 103 | + await CreateStream(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private async Task<string> CreateStream() |
| 108 | + { |
| 109 | + await _client.CreateStreamAsync(new CreateStreamRequest() |
| 110 | + { |
| 111 | + StreamName = _streamName, |
| 112 | + ShardCount = _defaultShardCount |
| 113 | + }); |
| 114 | + |
| 115 | + var i = 0; |
| 116 | + while (i < 20) |
| 117 | + { |
| 118 | + i++; |
| 119 | + await Task.Delay(3000); |
| 120 | + var poll = await _client.DescribeStreamSummaryAsync(new DescribeStreamSummaryRequest() |
| 121 | + { |
| 122 | + StreamName = _streamName |
| 123 | + }); |
| 124 | + |
| 125 | + if (poll.StreamDescriptionSummary.StreamStatus == StreamStatus.ACTIVE) |
| 126 | + return poll.StreamDescriptionSummary.StreamARN; |
| 127 | + } |
| 128 | + |
| 129 | + throw new TimeoutException(); |
| 130 | + } |
| 131 | + |
| 132 | + private void Consume(Record record, Action<LifeCycleEvent> action) |
| 133 | + { |
| 134 | + using (var strm = new StreamReader(record.Data)) |
| 135 | + { |
| 136 | + var evt = _serializer.Deserialize(new JsonTextReader(strm)); |
| 137 | + action(evt as LifeCycleEvent); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments