From 6eee161b6bc9d93a6544ac408d55d0cf7815e99e Mon Sep 17 00:00:00 2001 From: Kwoth Date: Wed, 29 Dec 2021 06:59:41 +0100 Subject: [PATCH] Fixed some NRT warnings in pub/sub mechanism --- src/NadekoBot/Common/PubSub/EventPubSub.cs | 8 ++-- src/NadekoBot/Common/PubSub/IPubSub.cs | 6 ++- src/NadekoBot/Common/PubSub/RedisPubSub.cs | 10 ++++- src/NadekoBot/Program.cs | 38 +++++++++---------- .../Services/Settings/ConfigServiceBase.cs | 16 ++++---- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/NadekoBot/Common/PubSub/EventPubSub.cs b/src/NadekoBot/Common/PubSub/EventPubSub.cs index d9291e17c..1c4eabe21 100644 --- a/src/NadekoBot/Common/PubSub/EventPubSub.cs +++ b/src/NadekoBot/Common/PubSub/EventPubSub.cs @@ -2,12 +2,13 @@ namespace NadekoBot.Common; public class EventPubSub : IPubSub { - private readonly Dictionary>>> _actions = new(); + private readonly Dictionary>>> _actions = new(); private readonly object _locker = new(); - public Task Sub(in TypedKey key, Func action) + public Task Sub(in TypedKey key, Func action) + where TData: notnull { - Func localAction = obj => action((TData?)obj); + Func localAction = obj => action((TData)obj); lock (_locker) { if (!_actions.TryGetValue(key.Key, out var keyActions)) @@ -29,6 +30,7 @@ public class EventPubSub : IPubSub } public Task Pub(in TypedKey key, TData data) + where TData: notnull { lock (_locker) { diff --git a/src/NadekoBot/Common/PubSub/IPubSub.cs b/src/NadekoBot/Common/PubSub/IPubSub.cs index 1b4a9311d..f0ee0940f 100644 --- a/src/NadekoBot/Common/PubSub/IPubSub.cs +++ b/src/NadekoBot/Common/PubSub/IPubSub.cs @@ -2,6 +2,8 @@ namespace NadekoBot.Common; public interface IPubSub { - public Task Pub(in TypedKey key, TData data); - public Task Sub(in TypedKey key, Func action); + public Task Pub(in TypedKey key, TData data) + where TData: notnull; + public Task Sub(in TypedKey key, Func action) + where TData: notnull; } \ No newline at end of file diff --git a/src/NadekoBot/Common/PubSub/RedisPubSub.cs b/src/NadekoBot/Common/PubSub/RedisPubSub.cs index 4ffdd35b9..533d0f443 100644 --- a/src/NadekoBot/Common/PubSub/RedisPubSub.cs +++ b/src/NadekoBot/Common/PubSub/RedisPubSub.cs @@ -16,13 +16,15 @@ public sealed class RedisPubSub : IPubSub } public Task Pub(in TypedKey key, TData data) + where TData : notnull { var serialized = _serializer.Serialize(data); return _multi.GetSubscriber() .PublishAsync($"{_creds.RedisKey()}:{key.Key}", serialized, CommandFlags.FireAndForget); } - public Task Sub(in TypedKey key, Func action) + public Task Sub(in TypedKey key, Func action) + where TData : notnull { var eventName = key.Key; @@ -31,7 +33,11 @@ public sealed class RedisPubSub : IPubSub try { var dataObj = _serializer.Deserialize(data); - await action(dataObj); + if(dataObj is not null) + await action(dataObj); + else + Log.Warning("Publishing event {EventName} with a null value. This is not allowed", + eventName); } catch (Exception ex) { diff --git a/src/NadekoBot/Program.cs b/src/NadekoBot/Program.cs index b9fd6889b..dc89a4a8f 100644 --- a/src/NadekoBot/Program.cs +++ b/src/NadekoBot/Program.cs @@ -2,25 +2,25 @@ var pid = Environment.ProcessId; var shardId = 0; int? totalShards = null; // 0 to read from creds.yml -// if (args.Length > 0) -// { -// if (!int.TryParse(args[0], out shardId)) -// { -// Console.Error.WriteLine("Invalid first argument (shard id): {0}", args[0]); -// return; -// } -// -// if (args.Length > 1) -// { -// if (!int.TryParse(args[1], out var shardCount)) -// { -// Console.Error.WriteLine("Invalid second argument (total shards): {0}", args[1]); -// return; -// } -// -// totalShards = shardCount; -// } -// } +if (args.Length > 0 && args[0] != "run") +{ + if (!int.TryParse(args[0], out shardId)) + { + Console.Error.WriteLine("Invalid first argument (shard id): {0}", args[0]); + return; + } + + if (args.Length > 1) + { + if (!int.TryParse(args[1], out var shardCount)) + { + Console.Error.WriteLine("Invalid second argument (total shards): {0}", args[1]); + return; + } + + totalShards = shardCount; + } +} LogSetup.SetupLogger(shardId); Log.Information("Pid: {ProcessId}", pid); diff --git a/src/NadekoBot/Services/Settings/ConfigServiceBase.cs b/src/NadekoBot/Services/Settings/ConfigServiceBase.cs index ef2dff5dc..f2fcec511 100644 --- a/src/NadekoBot/Services/Settings/ConfigServiceBase.cs +++ b/src/NadekoBot/Services/Settings/ConfigServiceBase.cs @@ -27,7 +27,7 @@ public abstract class ConfigServiceBase : IConfigService private readonly Dictionary> _propSetters = new(); private readonly Dictionary> _propSelectors = new(); private readonly Dictionary> _propPrinters = new(); - private readonly Dictionary _propComments = new(); + private readonly Dictionary _propComments = new(); /// /// Initialized an instance of @@ -106,12 +106,12 @@ public abstract class ConfigServiceBase : IConfigService Expression> selector, SettingParser parser, Func printer, - Func checker = null) + Func? checker = null) { checker ??= _ => true; key = key.ToLowerInvariant(); _propPrinters[key] = obj => printer((TProp)obj); - _propSelectors[key] = () => selector.Compile()(data); + _propSelectors[key] = () => selector.Compile()(data)!; _propSetters[key] = Magic(selector, parser, checker); _propComments[key] = ((MemberExpression)selector.Body).Member.GetCustomAttribute()?.Comment; } @@ -146,26 +146,26 @@ public abstract class ConfigServiceBase : IConfigService foreach (var memberExpression in expressions.AsEnumerable().Reverse()) { var localProp = (PropertyInfo)memberExpression.Member; - targetObject = localProp.GetValue(targetObject); + targetObject = localProp.GetValue(targetObject)!; } - prop!.SetValue(targetObject, value, null); + prop.SetValue(targetObject, value, null); return true; }; public IReadOnlyList GetSettableProps() => _propSetters.Keys.ToList(); - public string GetSetting(string prop) + public string? GetSetting(string prop) { prop = prop.ToLowerInvariant(); if (!_propSelectors.TryGetValue(prop, out var selector) || !_propPrinters.TryGetValue(prop, out var printer)) - return default; + return null; return printer(selector()); } - public string GetComment(string prop) + public string? GetComment(string prop) { if (_propComments.TryGetValue(prop, out var comment)) return comment;