From 4309ab9aadaf106b8f1c83d6b0537641ca1e35c6 Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Fri, 17 Nov 2023 20:40:46 -0500 Subject: [PATCH] woohoo --- .../Properties/launchSettings.json | 12 +-- .../BlazorCanvas.Server.csproj | 1 + .../Components/Data/RedisService.cs | 80 +++++++++++++++++-- .../BlazorCanvas.Server/appsettings.json | 5 +- 4 files changed, 83 insertions(+), 15 deletions(-) diff --git a/BlazorCanvas/BlazorCanvas.AppHost/Properties/launchSettings.json b/BlazorCanvas/BlazorCanvas.AppHost/Properties/launchSettings.json index 32cc58f..4b3378d 100644 --- a/BlazorCanvas/BlazorCanvas.AppHost/Properties/launchSettings.json +++ b/BlazorCanvas/BlazorCanvas.AppHost/Properties/launchSettings.json @@ -1,16 +1,16 @@ { - "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "http": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:15071", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "DOTNET_ENVIRONMENT": "Development", "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16047" - } + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:15071" } - } -} + }, + "$schema": "http://json.schemastore.org/launchsettings.json" +} \ No newline at end of file diff --git a/BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj b/BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj index b6f9e91..3b84305 100644 --- a/BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj +++ b/BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj @@ -7,6 +7,7 @@ ffc728b4-a681-4404-8156-a09f59c957d3 Linux ..\.. + ..\..\docker-compose.dcproj diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs index a20a370..2af5e6f 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs @@ -1,17 +1,79 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using StackExchange.Redis; +using System; +using System.Reflection.Metadata.Ecma335; namespace BlazorCanvas.Server.Components.Data; // https://developer.redis.com/develop/dotnet/streams/stream-basics/ -public class RedisService: IRedisService { +public class RedisService : IRedisService { + const string STREAM_NAME = "steamie", + GROUP_NAME = "groupie", + SUB_NAME = "servant"; + + private NameValueEntry[] arnve = new NameValueEntry[1]; + private IConnectionMultiplexer _cache; + private IDatabase _database; private ChannelMessageQueue _channel; - public RedisService(IConnectionMultiplexer cache) { - _cache = cache; - _channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal("lol")); + public RedisService(IConnectionMultiplexer cache) { _cache = cache; } + + /// + /// Init Pub/Sub - Redis en mode Pub/Sub ne garde pas ses messages en mémoire, + /// donc les commandes faites avant la souscription ne sont pas copiées. + /// + /// Si la connexion a réussi. + public bool InitSubscriber() { + try { + _channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal(SUB_NAME)); + } catch (Exception ex) { + Console.WriteLine(ex); + return false; + } + return true; + } + + /// + /// Init Streamer - Devrait être plus près de Kafka comme comportement. + /// + /// Si la connexion a réussi. + public async Task InitStreamer() { + try { + _database = _cache.GetDatabase(); + if (!(await _database.KeyExistsAsync(STREAM_NAME)) || + (await _database.StreamGroupInfoAsync(STREAM_NAME)).All(x => x.Name != GROUP_NAME)) { + await _database.StreamCreateConsumerGroupAsync(STREAM_NAME, GROUP_NAME, "0-0", true); + } + } catch (Exception ex) { + Console.WriteLine(ex); + return false; + } + return true; + } + + public async void Produce(CanvasCommand command) { + arnve[0] = new NameValueEntry("command", JsonConvert.SerializeObject(command)); + await _database.StreamAddAsync(STREAM_NAME, arnve); + } + + public async Task> Consume() { + List lsComm = new(); + CanvasCommand? comm; + var result = await _database.StreamReadGroupAsync(STREAM_NAME, GROUP_NAME, "commCon", ">"); + + foreach (var c in result) { + await _database.StreamAcknowledgeAsync(STREAM_NAME, GROUP_NAME, c.Id); + try { + string json = c.Values.FirstOrDefault(x => x.Name == "command").Value.ToString(); + comm = JsonConvert.DeserializeObject(json); + } catch { continue; } + if (comm is not null) + lsComm.Add(comm); + } + return lsComm; } /// @@ -20,10 +82,13 @@ public class RedisService: IRedisService { /// public async Task Subscribe(CancellationToken cToken) { var mess = await _channel.ReadAsync(cToken); - var comm = JsonConvert.DeserializeObject(mess.Message); + CanvasCommand? comm; + try { + comm = JsonConvert.DeserializeObject(mess.Message); + } catch { return null; } if (comm is not null) return comm; - else return null; + return null; } /// @@ -32,7 +97,6 @@ public class RedisService: IRedisService { /// /// La commande à publier public async void Publish(CanvasCommand command) { - CanvasCommand cm = new(command); - await _cache.GetSubscriber().PublishAsync(_channel.Channel, JsonConvert.SerializeObject(cm)); + await _cache.GetSubscriber().PublishAsync(_channel.Channel, JsonConvert.SerializeObject(command)); } } diff --git a/BlazorCanvas/BlazorCanvas.Server/appsettings.json b/BlazorCanvas/BlazorCanvas.Server/appsettings.json index 10f68b8..06b5635 100644 --- a/BlazorCanvas/BlazorCanvas.Server/appsettings.json +++ b/BlazorCanvas/BlazorCanvas.Server/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "cache": "host.docker.internal" + } }