diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs index 20c6544..4f7107a 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs @@ -18,7 +18,6 @@ public class CanvasService { private IRedisService _redisService; private CanvasCommand _lastCommand = new(); private List _lsComms = new(); - private Guid _id = Guid.NewGuid(); private bool _is_init = false, _is_started = false, _has_ended = false; @@ -55,7 +54,7 @@ public class CanvasService { while (!_has_ended) { if (_lsComms.Count > 0) _lsComms.Clear(); - _lsComms.AddRange(await _redisService.Consume(_id.ToString())); + _lsComms.AddRange(await _redisService.Consume()); if (_lsComms.Count == 1) Draw(_lsComms[0]); else if (_lsComms.Count > 0) diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/IRedisService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/IRedisService.cs index 38554ab..36aa4a0 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/IRedisService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/IRedisService.cs @@ -8,5 +8,5 @@ public interface IRedisService { void Publish(CanvasCommand command); void Produce(CanvasCommand command); - Task> Consume(string id); + Task> Consume(); } diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs index 52a1c94..653350e 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs @@ -9,9 +9,11 @@ namespace BlazorCanvas.Server.Components.Data; // https://developer.redis.com/develop/dotnet/streams/stream-basics/ public class RedisService : IRedisService { - const string STREAM_NAME = "steamie", - GROUP_NAME = "groupie", - SUB_NAME = "servant"; + private const string STREAM_NAME = "steamie", + GROUP_NAME = "groupie", + SUB_NAME = "servant"; + + private string _lastId = "0-0"; private NameValueEntry[] arNve = new NameValueEntry[1]; @@ -29,10 +31,11 @@ public class RedisService : IRedisService { public bool InitSubscriber() { try { _channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal(SUB_NAME)); - } catch (Exception ex) { - Console.WriteLine(ex); + } catch { return false; } + if (_channel is null) + return false; return true; } @@ -43,14 +46,11 @@ public class RedisService : IRedisService { 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); + } catch { return false; } + if (_database is null) + return false; return true; } @@ -59,13 +59,13 @@ public class RedisService : IRedisService { _database.StreamAddAsync(STREAM_NAME, arNve); } - public async Task> Consume(string id) { + public async Task> Consume() { List lsComm = new(); CanvasCommand? comm; - var result = await _database.StreamReadGroupAsync(STREAM_NAME, GROUP_NAME, id, ">", 100); + var result = await _database.StreamReadAsync(STREAM_NAME, _lastId, 100); string json = ""; + bool ok = false; foreach (var c in result) { - _database.StreamAcknowledgeAsync(STREAM_NAME, GROUP_NAME, c.Id); try { json = c.Values.FirstOrDefault(x => x.Name == "command").Value.ToString(); comm = JsonConvert.DeserializeObject(json); @@ -74,7 +74,11 @@ public class RedisService : IRedisService { continue; } if (comm is not null) lsComm.Add(comm); + if (!ok) + ok = true; } + if (ok) + _lastId = result.LastOrDefault().Id.ToString()?? "0-0"; return lsComm; }