1
0
This commit is contained in:
MarcEricMartel 2023-11-19 10:54:37 -05:00
parent 41f5bc5d67
commit 93450a9f49
3 changed files with 20 additions and 17 deletions

View File

@ -18,7 +18,6 @@ public class CanvasService {
private IRedisService _redisService;
private CanvasCommand _lastCommand = new();
private List<CanvasCommand> _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)

View File

@ -8,5 +8,5 @@ public interface IRedisService {
void Publish(CanvasCommand command);
void Produce(CanvasCommand command);
Task<IEnumerable<CanvasCommand>> Consume(string id);
Task<IEnumerable<CanvasCommand>> Consume();
}

View File

@ -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<bool> 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<IEnumerable<CanvasCommand>> Consume(string id) {
public async Task<IEnumerable<CanvasCommand>> Consume() {
List<CanvasCommand> 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<CanvasCommand>(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;
}