|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Newtonsoft.Json; |
| 7 | +using StackExchange.Redis; |
| 8 | +using WorkflowCore.Interface; |
| 9 | +using WorkflowCore.Models; |
| 10 | + |
| 11 | +namespace WorkflowCore.Providers.Redis.Services |
| 12 | +{ |
| 13 | + public class RedisPersistenceProvider : IPersistenceProvider |
| 14 | + { |
| 15 | + private readonly ILogger _logger; |
| 16 | + private readonly string _connectionString; |
| 17 | + private readonly string _prefix; |
| 18 | + private const string WORKFLOW_SET = "workflows"; |
| 19 | + private const string SUBSCRIPTION_SET = "subscriptions"; |
| 20 | + private const string EVENT_SET = "events"; |
| 21 | + private const string RUNNABLE_INDEX = "runnable"; |
| 22 | + private const string EVENTSLUG_INDEX = "eventslug"; |
| 23 | + private readonly IConnectionMultiplexer _multiplexer; |
| 24 | + private readonly IDatabase _redis; |
| 25 | + |
| 26 | + private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }; |
| 27 | + |
| 28 | + public RedisPersistenceProvider(string connectionString, string prefix, ILoggerFactory logFactory) |
| 29 | + { |
| 30 | + _connectionString = connectionString; |
| 31 | + _prefix = prefix; |
| 32 | + _logger = logFactory.CreateLogger(GetType()); |
| 33 | + _multiplexer = ConnectionMultiplexer.Connect(_connectionString); |
| 34 | + _redis = _multiplexer.GetDatabase(); |
| 35 | + } |
| 36 | + |
| 37 | + public async Task<string> CreateNewWorkflow(WorkflowInstance workflow) |
| 38 | + { |
| 39 | + workflow.Id = Guid.NewGuid().ToString(); |
| 40 | + await PersistWorkflow(workflow); |
| 41 | + return workflow.Id; |
| 42 | + } |
| 43 | + |
| 44 | + public async Task PersistWorkflow(WorkflowInstance workflow) |
| 45 | + { |
| 46 | + var str = JsonConvert.SerializeObject(workflow, _serializerSettings); |
| 47 | + await _redis.HashSetAsync($"{_prefix}.{WORKFLOW_SET}", workflow.Id, str); |
| 48 | + |
| 49 | + if ((workflow.Status == WorkflowStatus.Runnable) && (workflow.NextExecution.HasValue)) |
| 50 | + await _redis.SortedSetAddAsync($"{_prefix}.{WORKFLOW_SET}.{RUNNABLE_INDEX}", workflow.Id, workflow.NextExecution.Value); |
| 51 | + else |
| 52 | + await _redis.SortedSetRemoveAsync($"{_prefix}.{WORKFLOW_SET}.{RUNNABLE_INDEX}", workflow.Id); |
| 53 | + } |
| 54 | + |
| 55 | + public async Task<IEnumerable<string>> GetRunnableInstances(DateTime asAt) |
| 56 | + { |
| 57 | + var result = new List<string>(); |
| 58 | + var data = await _redis.SortedSetRangeByScoreAsync($"{_prefix}.{WORKFLOW_SET}.{RUNNABLE_INDEX}", -1, DateTime.UtcNow.Ticks); |
| 59 | + |
| 60 | + foreach (var item in data) |
| 61 | + result.Add(item); |
| 62 | + |
| 63 | + return result; |
| 64 | + } |
| 65 | + |
| 66 | + public async Task<IEnumerable<WorkflowInstance>> GetWorkflowInstances(WorkflowStatus? status, string type, DateTime? createdFrom, DateTime? createdTo, int skip, |
| 67 | + int take) |
| 68 | + { |
| 69 | + throw new NotImplementedException(); |
| 70 | + } |
| 71 | + |
| 72 | + public async Task<WorkflowInstance> GetWorkflowInstance(string Id) |
| 73 | + { |
| 74 | + var raw = await _redis.HashGetAsync($"{_prefix}.{WORKFLOW_SET}", Id); |
| 75 | + return JsonConvert.DeserializeObject<WorkflowInstance>(raw, _serializerSettings); |
| 76 | + } |
| 77 | + |
| 78 | + public async Task<string> CreateEventSubscription(EventSubscription subscription) |
| 79 | + { |
| 80 | + subscription.Id = Guid.NewGuid().ToString(); |
| 81 | + var str = JsonConvert.SerializeObject(subscription, _serializerSettings); |
| 82 | + await _redis.HashSetAsync($"{_prefix}.{SUBSCRIPTION_SET}", subscription.Id, str); |
| 83 | + await _redis.SortedSetAddAsync($"{_prefix}.{SUBSCRIPTION_SET}.{EVENTSLUG_INDEX}.{subscription.EventName}-{subscription.EventKey}", subscription.Id, subscription.SubscribeAsOf.Ticks); |
| 84 | + |
| 85 | + return subscription.Id; |
| 86 | + } |
| 87 | + |
| 88 | + public async Task<IEnumerable<EventSubscription>> GetSubcriptions(string eventName, string eventKey, DateTime asOf) |
| 89 | + { |
| 90 | + var result = new List<EventSubscription>(); |
| 91 | + var data = await _redis.SortedSetRangeByScoreAsync($"{_prefix}.{SUBSCRIPTION_SET}.{EVENTSLUG_INDEX}.{eventName}-{eventKey}", -1, asOf.Ticks); |
| 92 | + |
| 93 | + foreach (var id in data) |
| 94 | + { |
| 95 | + var raw = await _redis.HashGetAsync($"{_prefix}.{SUBSCRIPTION_SET}", id); |
| 96 | + if (raw.HasValue) |
| 97 | + result.Add(JsonConvert.DeserializeObject<EventSubscription>(raw, _serializerSettings)); |
| 98 | + } |
| 99 | + |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + public async Task TerminateSubscription(string eventSubscriptionId) |
| 104 | + { |
| 105 | + var existingRaw = await _redis.HashGetAsync($"{_prefix}.{SUBSCRIPTION_SET}", eventSubscriptionId); |
| 106 | + var existing = JsonConvert.DeserializeObject<EventSubscription>(existingRaw, _serializerSettings); |
| 107 | + await _redis.HashDeleteAsync($"{_prefix}.{SUBSCRIPTION_SET}", eventSubscriptionId); |
| 108 | + await _redis.SortedSetRemoveAsync($"{_prefix}.{SUBSCRIPTION_SET}.{EVENTSLUG_INDEX}.{existing.EventName}-{existing.EventKey}", eventSubscriptionId); |
| 109 | + } |
| 110 | + |
| 111 | + public async Task<string> CreateEvent(Event newEvent) |
| 112 | + { |
| 113 | + newEvent.Id = Guid.NewGuid().ToString(); |
| 114 | + var str = JsonConvert.SerializeObject(newEvent, _serializerSettings); |
| 115 | + await _redis.HashSetAsync($"{_prefix}.{EVENT_SET}", newEvent.Id, str); |
| 116 | + await _redis.SortedSetAddAsync($"{_prefix}.{EVENT_SET}.{EVENTSLUG_INDEX}.{newEvent.EventName}-{newEvent.EventKey}", newEvent.Id, newEvent.EventTime.Ticks); |
| 117 | + |
| 118 | + if (newEvent.IsProcessed) |
| 119 | + await _redis.SortedSetRemoveAsync($"{_prefix}.{EVENT_SET}.{RUNNABLE_INDEX}", newEvent.Id); |
| 120 | + else |
| 121 | + await _redis.SortedSetAddAsync($"{_prefix}.{EVENT_SET}.{RUNNABLE_INDEX}", newEvent.Id, newEvent.EventTime.Ticks); |
| 122 | + |
| 123 | + return newEvent.Id; |
| 124 | + } |
| 125 | + |
| 126 | + public async Task<Event> GetEvent(string id) |
| 127 | + { |
| 128 | + var raw = await _redis.HashGetAsync($"{_prefix}.{EVENT_SET}", id); |
| 129 | + return JsonConvert.DeserializeObject<Event>(raw, _serializerSettings); |
| 130 | + } |
| 131 | + |
| 132 | + public async Task<IEnumerable<string>> GetRunnableEvents(DateTime asAt) |
| 133 | + { |
| 134 | + var result = new List<string>(); |
| 135 | + var data = await _redis.SortedSetRangeByScoreAsync($"{_prefix}.{EVENT_SET}.{RUNNABLE_INDEX}", -1, asAt.Ticks); |
| 136 | + |
| 137 | + foreach (var item in data) |
| 138 | + result.Add(item); |
| 139 | + |
| 140 | + return result; |
| 141 | + } |
| 142 | + |
| 143 | + public async Task<IEnumerable<string>> GetEvents(string eventName, string eventKey, DateTime asOf) |
| 144 | + { |
| 145 | + var result = new List<string>(); |
| 146 | + var data = await _redis.SortedSetRangeByScoreAsync($"{_prefix}.{EVENT_SET}.{EVENTSLUG_INDEX}.{eventName}-{eventKey}", asOf.Ticks); |
| 147 | + |
| 148 | + foreach (var id in data) |
| 149 | + result.Add(id); |
| 150 | + |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | + public async Task MarkEventProcessed(string id) |
| 155 | + { |
| 156 | + var evt = await GetEvent(id); |
| 157 | + evt.IsProcessed = true; |
| 158 | + var str = JsonConvert.SerializeObject(evt, _serializerSettings); |
| 159 | + await _redis.HashSetAsync($"{_prefix}.{EVENT_SET}", evt.Id, str); |
| 160 | + await _redis.SortedSetRemoveAsync($"{_prefix}.{EVENT_SET}.{RUNNABLE_INDEX}", id); |
| 161 | + } |
| 162 | + |
| 163 | + public async Task MarkEventUnprocessed(string id) |
| 164 | + { |
| 165 | + var evt = await GetEvent(id); |
| 166 | + evt.IsProcessed = false; |
| 167 | + var str = JsonConvert.SerializeObject(evt, _serializerSettings); |
| 168 | + await _redis.HashSetAsync($"{_prefix}.{EVENT_SET}", evt.Id, str); |
| 169 | + await _redis.SortedSetAddAsync($"{_prefix}.{EVENT_SET}.{RUNNABLE_INDEX}", evt.Id, evt.EventTime.Ticks); |
| 170 | + } |
| 171 | + |
| 172 | + public Task PersistErrors(IEnumerable<ExecutionError> errors) |
| 173 | + { |
| 174 | + return Task.CompletedTask; |
| 175 | + } |
| 176 | + |
| 177 | + public void EnsureStoreExists() |
| 178 | + { |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments