Ejemplo n.º 1
0
        private static async void OnChanged(object sender, FileSystemEventArgs e)
        {
            if ((sender == null) || (e == null))
            {
                Logging.LogNullError(nameof(sender) + " || " + nameof(e));
                return;
            }

            string botName = GetBotNameFromConfigFileName(e.Name);

            if (string.IsNullOrEmpty(botName))
            {
                return;
            }

            Bot bot;

            if (!Bot.Bots.TryGetValue(botName, out bot))
            {
                CreateBot(botName).Forget();
                return;
            }

            DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);

            DateTime savedLastWriteTime;

            if (LastWriteTimes.TryGetValue(bot, out savedLastWriteTime))
            {
                if (savedLastWriteTime >= lastWriteTime)
                {
                    return;
                }
            }

            LastWriteTimes[bot] = lastWriteTime;

            // It's entirely possible that some process is still accessing our file, allow at least a second before trying to read it
            await Task.Delay(1000).ConfigureAwait(false);

            // It's also possible that we got some other event in the meantime
            if (LastWriteTimes.TryGetValue(bot, out savedLastWriteTime))
            {
                if (lastWriteTime != savedLastWriteTime)
                {
                    return;
                }

                if (LastWriteTimes.TryRemove(bot, out savedLastWriteTime))
                {
                    if (lastWriteTime != savedLastWriteTime)
                    {
                        return;
                    }
                }
            }

            bot.OnNewConfigLoaded(new BotConfigEventArgs(BotConfig.Load(e.FullPath))).Forget();
        }
Ejemplo n.º 2
0
        private static async void OnChanged(object sender, FileSystemEventArgs e)
        {
            if ((sender == null) || (e == null))
            {
                ArchiLogger.LogNullError(nameof(sender) + " || " + nameof(e));
                return;
            }

            string botName = Path.GetFileNameWithoutExtension(e.Name);

            if (string.IsNullOrEmpty(botName) || (botName[0] == '.'))
            {
                return;
            }

            DateTime lastWriteTime = DateTime.UtcNow;

            if (LastWriteTimes.TryGetValue(botName, out DateTime savedLastWriteTime))
            {
                if (savedLastWriteTime >= lastWriteTime)
                {
                    return;
                }
            }

            LastWriteTimes[botName] = lastWriteTime;

            // It's entirely possible that some process is still accessing our file, allow at least a second before trying to read it
            await Task.Delay(1000).ConfigureAwait(false);

            // It's also possible that we got some other event in the meantime
            if (LastWriteTimes.TryGetValue(botName, out savedLastWriteTime))
            {
                if (lastWriteTime != savedLastWriteTime)
                {
                    return;
                }

                if (LastWriteTimes.TryRemove(botName, out savedLastWriteTime))
                {
                    if (lastWriteTime != savedLastWriteTime)
                    {
                        return;
                    }
                }
            }

            if (botName.Equals(SharedInfo.ASF))
            {
                ArchiLogger.LogGenericInfo(Strings.GlobalConfigChanged);
                await RestartOrExit().ConfigureAwait(false);

                return;
            }

            if (!Bot.Bots.TryGetValue(botName, out Bot bot))
            {
                if (IsValidBotName(botName))
                {
                    await CreateBot(botName).ConfigureAwait(false);
                }

                return;
            }

            await bot.OnNewConfigLoaded(new BotConfigEventArgs(BotConfig.Load(e.FullPath))).ConfigureAwait(false);
        }
Ejemplo n.º 3
0
        private static async void OnChanged(object sender, FileSystemEventArgs e)
        {
            if ((sender == null) || (e == null))
            {
                ArchiLogger.LogNullError(nameof(sender) + " || " + nameof(e));
                return;
            }

            string botName = Path.GetFileNameWithoutExtension(e.Name);

            if (string.IsNullOrEmpty(botName))
            {
                return;
            }

            if (botName.Equals(SharedInfo.ASF))
            {
                ArchiLogger.LogGenericError("Global config file has been changed, restarting...");
                Program.Restart();
                return;
            }

            Bot bot;

            if (!Bot.Bots.TryGetValue(botName, out bot))
            {
                return;
            }

            DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);

            DateTime savedLastWriteTime;

            if (LastWriteTimes.TryGetValue(bot, out savedLastWriteTime))
            {
                if (savedLastWriteTime >= lastWriteTime)
                {
                    return;
                }
            }

            LastWriteTimes[bot] = lastWriteTime;

            // It's entirely possible that some process is still accessing our file, allow at least a second before trying to read it
            await Task.Delay(1000).ConfigureAwait(false);

            // It's also possible that we got some other event in the meantime
            if (LastWriteTimes.TryGetValue(bot, out savedLastWriteTime))
            {
                if (lastWriteTime != savedLastWriteTime)
                {
                    return;
                }

                if (LastWriteTimes.TryRemove(bot, out savedLastWriteTime))
                {
                    if (lastWriteTime != savedLastWriteTime)
                    {
                        return;
                    }
                }
            }

            bot.OnNewConfigLoaded(new BotConfigEventArgs(BotConfig.Load(e.FullPath))).Forget();
        }