public MonitoringService(IQueuedHandler monitoringQueue,
                                 IPublisher statsCollectionBus,
                                 IPublisher mainBus,
                                 ICheckpoint writerCheckpoint,
                                 string dbPath,
                                 TimeSpan statsCollectionPeriod,
                                 IPEndPoint nodeEndpoint,
                                 StatsStorage statsStorage,
                                 IPEndPoint tcpEndpoint,
                                 IPEndPoint tcpSecureEndpoint)
        {
            Ensure.NotNull(monitoringQueue, "monitoringQueue");
            Ensure.NotNull(statsCollectionBus, "statsCollectionBus");
            Ensure.NotNull(mainBus, "mainBus");
            Ensure.NotNull(writerCheckpoint, "writerCheckpoint");
            Ensure.NotNullOrEmpty(dbPath, "dbPath");
            Ensure.NotNull(nodeEndpoint, "nodeEndpoint");
            Ensure.NotNull(tcpEndpoint, "tcpEndpoint");

            _monitoringQueue         = monitoringQueue;
            _statsCollectionBus      = statsCollectionBus;
            _mainBus                 = mainBus;
            _writerCheckpoint        = writerCheckpoint;
            _dbPath                  = dbPath;
            _statsStorage            = statsStorage;
            _statsCollectionPeriodMs = statsCollectionPeriod > TimeSpan.Zero
                                ? (long)statsCollectionPeriod.TotalMilliseconds
                                : Timeout.Infinite;
            _nodeStatsStream   = string.Format("{0}-{1}", SystemStreams.StatsStreamPrefix, nodeEndpoint);
            _tcpEndpoint       = tcpEndpoint;
            _tcpSecureEndpoint = tcpSecureEndpoint;
            _timer             = new Timer(OnTimerTick, null, Timeout.Infinite, Timeout.Infinite);
        }
Esempio n. 2
0
            public async Task Restart(CommandContext ctx)
            {
                if (await LockObj.WaitAsync(0).ConfigureAwait(false))
                {
                    DiscordMessage?msg = null;
                    try
                    {
                        msg = await ctx.RespondAsync("Saving state...").ConfigureAwait(false);

                        await StatsStorage.SaveAsync(true).ConfigureAwait(false);

                        msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Restarting...").ConfigureAwait(false);

                        Restart(ctx.Channel.Id, "Restarted due to command request");
                    }
                    catch (Exception e)
                    {
                        await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Restarting failed: " + e.Message).ConfigureAwait(false);
                    }
                    finally
                    {
                        LockObj.Release();
                    }
                }
                else
                {
                    await ctx.RespondAsync("Update is in progress").ConfigureAwait(false);
                }
            }
        public SingleVNodeSettings(IPEndPoint externalTcpEndPoint, 
                                   IPEndPoint externalHttpEndPoint, 
                                   string[] httpPrefixes,
                                   int httpSendingThreads, 
                                   int httpReceivingThreads, 
                                   int tcpSendingThreads,
                                   TimeSpan statsPeriod, 
                                   StatsStorage statsStorage = StatsStorage.StreamAndCsv)
        {
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            Ensure.NotNull(httpPrefixes, "httpPrefixes");
            Ensure.Positive(httpReceivingThreads, "httpReceivingThreads");
            Ensure.Positive(httpSendingThreads, "httpSendingThreads");
            Ensure.Positive(tcpSendingThreads, "tcpSendingThreads");
            
            ExternalTcpEndPoint = externalTcpEndPoint;
            ExternalHttpEndPoint = externalHttpEndPoint;
            HttpPrefixes = httpPrefixes;
            HttpSendingThreads = httpSendingThreads;
            HttpReceivingThreads = httpReceivingThreads;
            TcpSendingThreads = tcpSendingThreads;

            StatsPeriod = statsPeriod;
            StatsStorage = statsStorage;
        }
        // ReSharper restore FieldCanBeMadeReadOnly.Local

        private EmbeddedVNodeBuilder()
        {
            _chunkSize       = TFConsts.ChunkSize;
            _dbPath          = Path.Combine(Path.GetTempPath(), "EmbeddedEventStore", string.Format("{0:yyyy-MM-dd_HH.mm.ss.ffffff}-EmbeddedNode", DateTime.UtcNow));
            _chunksCacheSize = TFConsts.ChunksCacheSize;
            _inMemoryDb      = true;

            _externalTcp       = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
            _externalSecureTcp = null;
            _externalTcp       = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
            _externalSecureTcp = null;
            _externalHttp      = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
            _externalHttp      = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);

            _intHttpPrefixes   = new List <string>();
            _extHttpPrefixes   = new List <string>();
            _enableTrustedAuth = Opts.EnableTrustedAuthDefault;
            _certificate       = null;
            _workerThreads     = Opts.WorkerThreadsDefault;

            _discoverViaDns = false;
            _clusterDns     = Opts.ClusterDnsDefault;
            _gossipSeeds    = new List <IPEndPoint>();

            _minFlushDelay = TimeSpan.FromMilliseconds(Opts.MinFlushDelayMsDefault);

            _clusterNodeCount = 1;
            _prepareAckCount  = 1;
            _commitAckCount   = 1;
            _prepareTimeout   = TimeSpan.FromMilliseconds(Opts.PrepareTimeoutMsDefault);
            _commitTimeout    = TimeSpan.FromMilliseconds(Opts.CommitTimeoutMsDefault);

            _nodePriority = Opts.NodePriorityDefault;

            _useSsl            = Opts.UseInternalSslDefault;
            _sslTargetHost     = Opts.SslTargetHostDefault;
            _sslValidateServer = Opts.SslValidateServerDefault;

            _statsPeriod  = TimeSpan.FromSeconds(Opts.StatsPeriodDefault);
            _statsStorage = StatsStorage.Stream;

            _authenticationProviderFactory = new InternalAuthenticationProviderFactory();
            _disableScavengeMerging        = Opts.DisableScavengeMergeDefault;
            _adminOnPublic  = Opts.AdminOnExtDefault;
            _statsOnPublic  = Opts.StatsOnExtDefault;
            _gossipOnPublic = Opts.GossipOnExtDefault;
            _gossipInterval = TimeSpan.FromMilliseconds(Opts.GossipIntervalMsDefault);
            _gossipAllowedTimeDifference = TimeSpan.FromMilliseconds(Opts.GossipAllowedDifferenceMsDefault);
            _gossipTimeout = TimeSpan.FromMilliseconds(Opts.GossipTimeoutMsDefault);

            _intTcpHeartbeatInterval = TimeSpan.FromMilliseconds(Opts.IntTcpHeartbeatInvervalDefault);
            _intTcpHeartbeatTimeout  = TimeSpan.FromMilliseconds(Opts.IntTcpHeartbeatTimeoutDefault);
            _extTcpHeartbeatInterval = TimeSpan.FromMilliseconds(Opts.ExtTcpHeartbeatIntervalDefault);
            _extTcpHeartbeatTimeout  = TimeSpan.FromMilliseconds(Opts.ExtTcpHeartbeatTimeoutDefault);

            _skipVerifyDbHashes = Opts.SkipDbVerifyDefault;
            _maxMemtableSize    = Opts.MaxMemtableSizeDefault;
            _subsystems         = new List <ISubsystem>();
            _clusterGossipPort  = Opts.ClusterGossipPortDefault;
        }
Esempio n. 5
0
            public async Task Update(CommandContext ctx)
            {
                if (await lockObj.WaitAsync(0).ConfigureAwait(false))
                {
                    DiscordMessage msg = null;
                    try
                    {
                        Config.Log.Info("Checking for available updates...");
                        msg = await ctx.RespondAsync("Saving state...").ConfigureAwait(false);

                        await StatsStorage.SaveAsync(true).ConfigureAwait(false);

                        msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Checking for updates...").ConfigureAwait(false);

                        using (var git = new Process
                        {
                            StartInfo = new ProcessStartInfo("git", "pull")
                            {
                                CreateNoWindow = true,
                                UseShellExecute = false,
                                RedirectStandardOutput = true,
                                StandardOutputEncoding = Encoding.UTF8,
                            },
                        })
                        {
                            git.Start();
                            var stdout = await git.StandardOutput.ReadToEndAsync().ConfigureAwait(false);

                            git.WaitForExit();
                            if (!string.IsNullOrEmpty(stdout))
                            {
                                await ctx.SendAutosplitMessageAsync("```" + stdout + "```").ConfigureAwait(false);

                                if (stdout.Contains("Already up to date", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    return;
                                }
                            }
                        }
                        msg = await ctx.RespondAsync("Restarting...").ConfigureAwait(false);

                        Restart(ctx.Channel.Id);
                    }
                    catch (Exception e)
                    {
                        msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Updating failed: " + e.Message).ConfigureAwait(false);
                    }
                    finally
                    {
                        lockObj.Release();
                    }
                }
                else
                {
                    await ctx.RespondAsync("Update is already in progress").ConfigureAwait(false);
                }
            }
Esempio n. 6
0
 private void UpdateStats()
 {
     if (SettingsHolder.Drive.WatchForSharedMemory)
     {
         var last = PlayerStatsManager.Instance.Last;
         if (last != null && !last.IsSpoiled)
         {
             Stats.Extend(last);
             StatsStorage.SetObject(Id, Stats);
             OnPropertyChanged(nameof(SessionsCount));
         }
     }
 }
Esempio n. 7
0
        public SingleVNodeSettings(IPEndPoint externalTcpEndPoint,
                                   IPEndPoint externalSecureTcpEndPoint,
                                   IPEndPoint externalHttpEndPoint,
                                   string[] httpPrefixes,
                                   bool enableTrustedAuth,
                                   X509Certificate2 certificate,
                                   int workerThreads,
                                   TimeSpan minFlushDelay,
                                   TimeSpan prepareTimeout,
                                   TimeSpan commitTimeout,
                                   TimeSpan statsPeriod,
                                   TimeSpan tcpTimeout,
                                   StatsStorage statsStorage             = StatsStorage.StreamAndFile,
                                   bool skipInitializeStandardUsersCheck = false,
                                   bool disableScavengeMerging           = false)
        {
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            Ensure.NotNull(httpPrefixes, "httpPrefixes");
            if (externalSecureTcpEndPoint != null)
            {
                Ensure.NotNull(certificate, "certificate");
            }
            Ensure.Positive(workerThreads, "workerThreads");

            ExternalTcpEndPoint       = externalTcpEndPoint;
            ExternalSecureTcpEndPoint = externalSecureTcpEndPoint;
            ExternalHttpEndPoint      = externalHttpEndPoint;
            HttpPrefixes      = httpPrefixes;
            EnableTrustedAuth = enableTrustedAuth;
            Certificate       = certificate;
            WorkerThreads     = workerThreads;

            MinFlushDelay  = minFlushDelay;
            PrepareTimeout = prepareTimeout;
            CommitTimeout  = commitTimeout;

            StatsPeriod  = statsPeriod;
            StatsStorage = statsStorage;

            SkipInitializeStandardUsersCheck = skipInitializeStandardUsersCheck;
            DisableScavengeMerging           = disableScavengeMerging;
            TcpTimeout = tcpTimeout;
        }
Esempio n. 8
0
            public async Task Update(CommandContext ctx)
            {
                if (await LockObj.WaitAsync(0).ConfigureAwait(false))
                {
                    DiscordMessage?msg = null;
                    try
                    {
                        Config.Log.Info("Checking for available bot updates...");
                        msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Checking for bot updates...").ConfigureAwait(false);

                        var(updated, stdout) = await UpdateAsync().ConfigureAwait(false);

                        if (!string.IsNullOrEmpty(stdout))
                        {
                            await ctx.SendAutosplitMessageAsync("```" + stdout + "```").ConfigureAwait(false);
                        }
                        if (!updated)
                        {
                            return;
                        }

                        msg = await ctx.RespondAsync("Saving state...").ConfigureAwait(false);

                        await StatsStorage.SaveAsync(true).ConfigureAwait(false);

                        msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Restarting...").ConfigureAwait(false);

                        Restart(ctx.Channel.Id, "Restarted after successful bot update");
                    }
                    catch (Exception e)
                    {
                        await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Updating failed: " + e.Message).ConfigureAwait(false);
                    }
                    finally
                    {
                        LockObj.Release();
                    }
                }
                else
                {
                    await ctx.RespondAsync("Update is already in progress").ConfigureAwait(false);
                }
            }
Esempio n. 9
0
        public SingleVNodeSettings(IPEndPoint externalTcpEndPoint, 
                                   IPEndPoint externalSecureTcpEndPoint,
                                   IPEndPoint externalHttpEndPoint, 
                                   string[] httpPrefixes,
                                   bool enableTrustedAuth,
                                   X509Certificate2 certificate,
                                   int workerThreads, 
                                   TimeSpan minFlushDelay,
                                   TimeSpan prepareTimeout,
                                   TimeSpan commitTimeout,
                                   TimeSpan statsPeriod, 
                                   TimeSpan tcpTimeout,
                                   StatsStorage statsStorage = StatsStorage.StreamAndCsv,
                                   bool skipInitializeStandardUsersCheck = false,
                                   bool disableScavengeMerging = false)
        {
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            Ensure.NotNull(httpPrefixes, "httpPrefixes");
            if (externalSecureTcpEndPoint != null)
                Ensure.NotNull(certificate, "certificate");
            Ensure.Positive(workerThreads, "workerThreads");

            ExternalTcpEndPoint = externalTcpEndPoint;
            ExternalSecureTcpEndPoint = externalSecureTcpEndPoint;
            ExternalHttpEndPoint = externalHttpEndPoint;
            HttpPrefixes = httpPrefixes;
            EnableTrustedAuth = enableTrustedAuth;
            Certificate = certificate;
            WorkerThreads = workerThreads;

            MinFlushDelay = minFlushDelay;
            PrepareTimeout = prepareTimeout;
            CommitTimeout = commitTimeout;

            StatsPeriod = statsPeriod;
            StatsStorage = statsStorage;

            SkipInitializeStandardUsersCheck = skipInitializeStandardUsersCheck;
            DisableScavengeMerging = disableScavengeMerging;
            TcpTimeout = tcpTimeout;
        }
        public ClusterVNodeSettings(Guid instanceId, int debugIndex,
                                    IPEndPoint internalTcpEndPoint,
                                    IPEndPoint internalSecureTcpEndPoint,
                                    IPEndPoint externalTcpEndPoint,
                                    IPEndPoint externalSecureTcpEndPoint,
                                    IPEndPoint internalHttpEndPoint,
                                    IPEndPoint externalHttpEndPoint,
                                    string[] httpPrefixes,
                                    bool enableTrustedAuth,
                                    X509Certificate2 certificate,
                                    int workerThreads,
                                    bool discoverViaDns,
                                    string clusterDns,
                                    IPEndPoint[] gossipSeeds,
                                    TimeSpan minFlushDelay,
                                    int clusterNodeCount,
                                    int prepareAckCount,
                                    int commitAckCount,
                                    TimeSpan prepareTimeout,
                                    TimeSpan commitTimeout,
                                    bool useSsl,
                                    string sslTargetHost,
                                    bool sslValidateServer,
                                    TimeSpan statsPeriod,
                                    StatsStorage statsStorage,
                                    int nodePriority,
                                    IAuthenticationProviderFactory authenticationProviderFactory,
                                    bool disableScavengeMerging,
                                    bool adminOnPublic,
                                    bool statsOnPublic,
                                    bool gossipOnPublic,
                                    TimeSpan gossipInterval,
                                    TimeSpan gossipAllowedTimeDifference,
                                    TimeSpan gossipTimeout,
                                    TimeSpan tcpTimeout)
        {
            Ensure.NotEmptyGuid(instanceId, "instanceId");
            Ensure.NotNull(internalTcpEndPoint, "internalTcpEndPoint");
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(internalHttpEndPoint, "internalHttpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            Ensure.NotNull(httpPrefixes, "httpPrefixes");
            if (internalSecureTcpEndPoint != null || externalSecureTcpEndPoint != null)
                Ensure.NotNull(certificate, "certificate");
            Ensure.Positive(workerThreads, "workerThreads");
            Ensure.NotNull(clusterDns, "clusterDns");
            Ensure.NotNull(gossipSeeds, "gossipSeeds");
            Ensure.Positive(clusterNodeCount, "clusterNodeCount");
            Ensure.Positive(prepareAckCount, "prepareAckCount");
            Ensure.Positive(commitAckCount, "commitAckCount");

            if (discoverViaDns && string.IsNullOrWhiteSpace(clusterDns))
                throw new ArgumentException("Either DNS Discovery must be disabled (and seeds specified), or a cluster DNS name must be provided.");

            if (useSsl)
                Ensure.NotNull(sslTargetHost, "sslTargetHost");

            NodeInfo = new VNodeInfo(instanceId, debugIndex,
                                     internalTcpEndPoint, internalSecureTcpEndPoint,
                                     externalTcpEndPoint, externalSecureTcpEndPoint,
                                     internalHttpEndPoint, externalHttpEndPoint);
            HttpPrefixes = httpPrefixes;
            EnableTrustedAuth = enableTrustedAuth;
            Certificate = certificate;
            WorkerThreads = workerThreads;

            DiscoverViaDns = discoverViaDns;
            ClusterDns = clusterDns;
            GossipSeeds = gossipSeeds;

            ClusterNodeCount = clusterNodeCount;
            MinFlushDelay = minFlushDelay;
            PrepareAckCount = prepareAckCount;
            CommitAckCount = commitAckCount;
            PrepareTimeout = prepareTimeout;
            CommitTimeout = commitTimeout;

            UseSsl = useSsl;
            SslTargetHost = sslTargetHost;
            SslValidateServer = sslValidateServer;

            StatsPeriod = statsPeriod;
            StatsStorage = statsStorage;

            AuthenticationProviderFactory = authenticationProviderFactory;

            NodePriority = nodePriority;
            DisableScavengeMerging = disableScavengeMerging;
            AdminOnPublic = adminOnPublic;
            StatsOnPublic = statsOnPublic;
            GossipOnPublic = gossipOnPublic;
            GossipInterval = gossipInterval;
            GossipAllowedTimeDifference = gossipAllowedTimeDifference;
            GossipTimeout = gossipTimeout;
            TcpTimeout = tcpTimeout;
        }
Esempio n. 11
0
        public ClusterVNodeSettings(Guid instanceId, int debugIndex,
                                    IPEndPoint internalTcpEndPoint,
                                    IPEndPoint internalSecureTcpEndPoint,
                                    IPEndPoint externalTcpEndPoint,
                                    IPEndPoint externalSecureTcpEndPoint,
                                    IPEndPoint internalHttpEndPoint,
                                    IPEndPoint externalHttpEndPoint,
                                    GossipAdvertiseInfo gossipAdvertiseInfo,
                                    string[] intHttpPrefixes,
                                    string[] extHttpPrefixes,
                                    bool enableTrustedAuth,
                                    X509Certificate2 certificate,
                                    int workerThreads,
                                    bool discoverViaDns,
                                    string clusterDns,
                                    IPEndPoint[] gossipSeeds,
                                    TimeSpan minFlushDelay,
                                    int clusterNodeCount,
                                    int prepareAckCount,
                                    int commitAckCount,
                                    TimeSpan prepareTimeout,
                                    TimeSpan commitTimeout,
                                    bool useSsl,
                                    string sslTargetHost,
                                    bool sslValidateServer,
                                    TimeSpan statsPeriod,
                                    StatsStorage statsStorage,
                                    int nodePriority,
                                    IAuthenticationProviderFactory authenticationProviderFactory,
                                    bool disableScavengeMerging,
                                    bool adminOnPublic,
                                    bool statsOnPublic,
                                    bool gossipOnPublic,
                                    TimeSpan gossipInterval,
                                    TimeSpan gossipAllowedTimeDifference,
                                    TimeSpan gossipTimeout,
                                    TimeSpan intTcpHeartbeatTimeout,
                                    TimeSpan intTcpHeartbeatInterval,
                                    TimeSpan extTcpHeartbeatTimeout,
                                    TimeSpan extTcpHeartbeatInterval,
				                    bool verifyDbHash,
				                    int maxMemtableEntryCount,
                                    bool startStandardProjections,
                                    bool disableHTTPCaching, string index = null, bool enableHistograms = false,
                                    int indexCacheDepth = 16,
                                    IPersistentSubscriptionConsumerStrategyFactory[] additionalConsumerStrategies = null)
        {
            Ensure.NotEmptyGuid(instanceId, "instanceId");
            Ensure.NotNull(internalTcpEndPoint, "internalTcpEndPoint");
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(internalHttpEndPoint, "internalHttpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            Ensure.NotNull(intHttpPrefixes, "intHttpPrefixes");
            Ensure.NotNull(extHttpPrefixes, "extHttpPrefixes");
            if (internalSecureTcpEndPoint != null || externalSecureTcpEndPoint != null)
                Ensure.NotNull(certificate, "certificate");
            Ensure.Positive(workerThreads, "workerThreads");
            Ensure.NotNull(clusterDns, "clusterDns");
            Ensure.NotNull(gossipSeeds, "gossipSeeds");
            Ensure.Positive(clusterNodeCount, "clusterNodeCount");
            Ensure.Positive(prepareAckCount, "prepareAckCount");
            Ensure.Positive(commitAckCount, "commitAckCount");
            Ensure.NotNull(gossipAdvertiseInfo, "gossipAdvertiseInfo");

            if (discoverViaDns && string.IsNullOrWhiteSpace(clusterDns))
                throw new ArgumentException("Either DNS Discovery must be disabled (and seeds specified), or a cluster DNS name must be provided.");

            if (useSsl)
                Ensure.NotNull(sslTargetHost, "sslTargetHost");

            NodeInfo = new VNodeInfo(instanceId, debugIndex,
                                     internalTcpEndPoint, internalSecureTcpEndPoint,
                                     externalTcpEndPoint, externalSecureTcpEndPoint,
                                     internalHttpEndPoint, externalHttpEndPoint);
            GossipAdvertiseInfo = gossipAdvertiseInfo;
            IntHttpPrefixes = intHttpPrefixes;
            ExtHttpPrefixes = extHttpPrefixes;
            EnableTrustedAuth = enableTrustedAuth;
            Certificate = certificate;
            WorkerThreads = workerThreads;
            StartStandardProjections = startStandardProjections;
            DisableHTTPCaching = disableHTTPCaching;
            AdditionalConsumerStrategies = additionalConsumerStrategies ?? new IPersistentSubscriptionConsumerStrategyFactory[0];

            DiscoverViaDns = discoverViaDns;
            ClusterDns = clusterDns;
            GossipSeeds = gossipSeeds;

            ClusterNodeCount = clusterNodeCount;
            MinFlushDelay = minFlushDelay;
            PrepareAckCount = prepareAckCount;
            CommitAckCount = commitAckCount;
            PrepareTimeout = prepareTimeout;
            CommitTimeout = commitTimeout;

            UseSsl = useSsl;
            SslTargetHost = sslTargetHost;
            SslValidateServer = sslValidateServer;

            StatsPeriod = statsPeriod;
            StatsStorage = statsStorage;

            AuthenticationProviderFactory = authenticationProviderFactory;

            NodePriority = nodePriority;
            DisableScavengeMerging = disableScavengeMerging;
            AdminOnPublic = adminOnPublic;
            StatsOnPublic = statsOnPublic;
            GossipOnPublic = gossipOnPublic;
            GossipInterval = gossipInterval;
            GossipAllowedTimeDifference = gossipAllowedTimeDifference;
            GossipTimeout = gossipTimeout;
            IntTcpHeartbeatTimeout = intTcpHeartbeatTimeout;
            IntTcpHeartbeatInterval = intTcpHeartbeatInterval;
            ExtTcpHeartbeatTimeout = extTcpHeartbeatTimeout;
            ExtTcpHeartbeatInterval = extTcpHeartbeatInterval;

            VerifyDbHash = verifyDbHash;
            MaxMemtableEntryCount = maxMemtableEntryCount;

            EnableHistograms = enableHistograms;
            IndexCacheDepth = indexCacheDepth;
            Index = index;
        }
Esempio n. 12
0
        public ClusterVNodeSettings(Guid instanceId,
                                    IPEndPoint internalTcpEndPoint,
                                    IPEndPoint internalSecureTcpEndPoint,
                                    IPEndPoint externalTcpEndPoint,
                                    IPEndPoint externalSecureTcpEndPoint,
                                    IPEndPoint internalHttpEndPoint,
                                    IPEndPoint externalHttpEndPoint,
                                    string[] httpPrefixes,
                                    bool enableTrustedAuth,
                                    X509Certificate2 certificate,
                                    int workerThreads,
                                    bool discoverViaDns,
                                    string clusterDns,
                                    IPEndPoint[] gossipSeeds,
                                    TimeSpan minFlushDelay,
                                    int clusterNodeCount,
                                    int prepareAckCount,
                                    int commitAckCount,
                                    TimeSpan prepareTimeout,
                                    TimeSpan commitTimeout,
                                    bool useSsl,
                                    string sslTargetHost,
                                    bool sslValidateServer,
                                    TimeSpan statsPeriod,
                                    StatsStorage statsStorage,
                                    int nodePriority,
                                    IAuthenticationProviderFactory authenticationProviderFactory)
        {
            Ensure.NotEmptyGuid(instanceId, "instanceId");
            Ensure.NotNull(internalTcpEndPoint, "internalTcpEndPoint");
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(internalHttpEndPoint, "internalHttpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            Ensure.NotNull(httpPrefixes, "httpPrefixes");
            if (internalSecureTcpEndPoint != null || externalSecureTcpEndPoint != null)
            {
                Ensure.NotNull(certificate, "certificate");
            }
            Ensure.Positive(workerThreads, "workerThreads");
            Ensure.NotNull(clusterDns, "clusterDns");
            Ensure.NotNull(gossipSeeds, "gossipSeeds");
            Ensure.Positive(clusterNodeCount, "clusterNodeCount");
            Ensure.Positive(prepareAckCount, "prepareAckCount");
            Ensure.Positive(commitAckCount, "commitAckCount");

            if (discoverViaDns && string.IsNullOrWhiteSpace(clusterDns))
            {
                throw new ArgumentException("Either DNS Discovery must be disabled (and seeds specified), or a cluster DNS name must be provided.");
            }

            if (useSsl)
            {
                Ensure.NotNull(sslTargetHost, "sslTargetHost");
            }

            NodeInfo = new VNodeInfo(instanceId,
                                     internalTcpEndPoint, internalSecureTcpEndPoint,
                                     externalTcpEndPoint, externalSecureTcpEndPoint,
                                     internalHttpEndPoint, externalHttpEndPoint);
            HttpPrefixes      = httpPrefixes;
            EnableTrustedAuth = enableTrustedAuth;
            Certificate       = certificate;
            WorkerThreads     = workerThreads;

            DiscoverViaDns = discoverViaDns;
            ClusterDns     = clusterDns;
            GossipSeeds    = gossipSeeds;

            ClusterNodeCount = clusterNodeCount;
            MinFlushDelay    = minFlushDelay;
            PrepareAckCount  = prepareAckCount;
            CommitAckCount   = commitAckCount;
            PrepareTimeout   = prepareTimeout;
            CommitTimeout    = commitTimeout;

            UseSsl            = useSsl;
            SslTargetHost     = sslTargetHost;
            SslValidateServer = sslValidateServer;

            StatsPeriod  = statsPeriod;
            StatsStorage = statsStorage;

            AuthenticationProviderFactory = authenticationProviderFactory;

            NodePriority = nodePriority;
        }
Esempio n. 13
0
        public ClusterVNodeSettings(Guid instanceId, int debugIndex,
                                    IPEndPoint internalTcpEndPoint,
                                    IPEndPoint internalSecureTcpEndPoint,
                                    IPEndPoint externalTcpEndPoint,
                                    IPEndPoint externalSecureTcpEndPoint,
                                    IPEndPoint internalHttpEndPoint,
                                    IPEndPoint externalHttpEndPoint,
                                    GossipAdvertiseInfo gossipAdvertiseInfo,
                                    string[] intHttpPrefixes,
                                    string[] extHttpPrefixes,
                                    bool enableTrustedAuth,
                                    X509Certificate2 certificate,
                                    int workerThreads,
                                    bool discoverViaDns,
                                    string clusterDns,
                                    IPEndPoint[] gossipSeeds,
                                    TimeSpan minFlushDelay,
                                    int clusterNodeCount,
                                    int prepareAckCount,
                                    int commitAckCount,
                                    TimeSpan prepareTimeout,
                                    TimeSpan commitTimeout,
                                    bool useSsl,
                                    bool disableInsecureTCP,
                                    string sslTargetHost,
                                    bool sslValidateServer,
                                    TimeSpan statsPeriod,
                                    StatsStorage statsStorage,
                                    int nodePriority,
                                    IAuthenticationProviderFactory authenticationProviderFactory,
                                    bool disableScavengeMerging,
                                    int scavengeHistoryMaxAge,
                                    bool adminOnPublic,
                                    bool statsOnPublic,
                                    bool gossipOnPublic,
                                    TimeSpan gossipInterval,
                                    TimeSpan gossipAllowedTimeDifference,
                                    TimeSpan gossipTimeout,
                                    TimeSpan intTcpHeartbeatTimeout,
                                    TimeSpan intTcpHeartbeatInterval,
                                    TimeSpan extTcpHeartbeatTimeout,
                                    TimeSpan extTcpHeartbeatInterval,
                                    bool verifyDbHash,
                                    int maxMemtableEntryCount,
                                    int hashCollisionReadLimit,
                                    bool startStandardProjections,
                                    bool disableHTTPCaching,
                                    bool logHttpRequests,
                                    int connectionPendingSendBytesThreshold,
                                    int chunkInitialReaderCount,
                                    string index             = null, bool enableHistograms = false,
                                    bool skipIndexVerify     = false,
                                    int indexCacheDepth      = 16,
                                    byte indexBitnessVersion = 4,
                                    bool optimizeIndexMerge  = false,
                                    IPersistentSubscriptionConsumerStrategyFactory[] additionalConsumerStrategies = null,
                                    bool unsafeIgnoreHardDeletes = false,
                                    bool betterOrdering          = false,
                                    int readerThreadsCount       = 4,
                                    bool alwaysKeepScavenged     = false,
                                    bool gossipOnSingleNode      = false,
                                    bool skipIndexScanOnReads    = false,
                                    bool reduceFileCachePressure = false,
                                    int initializationThreads    = 1)
        {
            Ensure.NotEmptyGuid(instanceId, "instanceId");
            Ensure.NotNull(internalTcpEndPoint, "internalTcpEndPoint");
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(internalHttpEndPoint, "internalHttpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            Ensure.NotNull(intHttpPrefixes, "intHttpPrefixes");
            Ensure.NotNull(extHttpPrefixes, "extHttpPrefixes");
            if (internalSecureTcpEndPoint != null || externalSecureTcpEndPoint != null)
            {
                Ensure.NotNull(certificate, "certificate");
            }
            Ensure.Positive(workerThreads, "workerThreads");
            Ensure.NotNull(clusterDns, "clusterDns");
            Ensure.NotNull(gossipSeeds, "gossipSeeds");
            Ensure.Positive(clusterNodeCount, "clusterNodeCount");
            Ensure.Positive(prepareAckCount, "prepareAckCount");
            Ensure.Positive(commitAckCount, "commitAckCount");
            Ensure.Positive(initializationThreads, "initializationThreads");
            Ensure.NotNull(gossipAdvertiseInfo, "gossipAdvertiseInfo");

            if (discoverViaDns && string.IsNullOrWhiteSpace(clusterDns))
            {
                throw new ArgumentException("Either DNS Discovery must be disabled (and seeds specified), or a cluster DNS name must be provided.");
            }

            if (useSsl)
            {
                Ensure.NotNull(sslTargetHost, "sslTargetHost");
            }

            NodeInfo = new VNodeInfo(instanceId, debugIndex,
                                     internalTcpEndPoint, internalSecureTcpEndPoint,
                                     externalTcpEndPoint, externalSecureTcpEndPoint,
                                     internalHttpEndPoint, externalHttpEndPoint);
            GossipAdvertiseInfo          = gossipAdvertiseInfo;
            IntHttpPrefixes              = intHttpPrefixes;
            ExtHttpPrefixes              = extHttpPrefixes;
            EnableTrustedAuth            = enableTrustedAuth;
            Certificate                  = certificate;
            WorkerThreads                = workerThreads;
            StartStandardProjections     = startStandardProjections;
            DisableHTTPCaching           = disableHTTPCaching;
            LogHttpRequests              = logHttpRequests;
            AdditionalConsumerStrategies = additionalConsumerStrategies ?? new IPersistentSubscriptionConsumerStrategyFactory[0];

            DiscoverViaDns     = discoverViaDns;
            ClusterDns         = clusterDns;
            GossipSeeds        = gossipSeeds;
            GossipOnSingleNode = gossipOnSingleNode;

            ClusterNodeCount = clusterNodeCount;
            MinFlushDelay    = minFlushDelay;
            PrepareAckCount  = prepareAckCount;
            CommitAckCount   = commitAckCount;
            PrepareTimeout   = prepareTimeout;
            CommitTimeout    = commitTimeout;

            UseSsl             = useSsl;
            DisableInsecureTCP = disableInsecureTCP;
            SslTargetHost      = sslTargetHost;
            SslValidateServer  = sslValidateServer;

            StatsPeriod  = statsPeriod;
            StatsStorage = statsStorage;

            AuthenticationProviderFactory = authenticationProviderFactory;

            NodePriority                        = nodePriority;
            DisableScavengeMerging              = disableScavengeMerging;
            ScavengeHistoryMaxAge               = scavengeHistoryMaxAge;
            AdminOnPublic                       = adminOnPublic;
            StatsOnPublic                       = statsOnPublic;
            GossipOnPublic                      = gossipOnPublic;
            GossipInterval                      = gossipInterval;
            GossipAllowedTimeDifference         = gossipAllowedTimeDifference;
            GossipTimeout                       = gossipTimeout;
            IntTcpHeartbeatTimeout              = intTcpHeartbeatTimeout;
            IntTcpHeartbeatInterval             = intTcpHeartbeatInterval;
            ExtTcpHeartbeatTimeout              = extTcpHeartbeatTimeout;
            ExtTcpHeartbeatInterval             = extTcpHeartbeatInterval;
            ConnectionPendingSendBytesThreshold = connectionPendingSendBytesThreshold;
            ChunkInitialReaderCount             = chunkInitialReaderCount;

            VerifyDbHash           = verifyDbHash;
            MaxMemtableEntryCount  = maxMemtableEntryCount;
            HashCollisionReadLimit = hashCollisionReadLimit;

            EnableHistograms    = enableHistograms;
            SkipIndexVerify     = skipIndexVerify;
            IndexCacheDepth     = indexCacheDepth;
            IndexBitnessVersion = indexBitnessVersion;
            OptimizeIndexMerge  = optimizeIndexMerge;
            Index = index;
            UnsafeIgnoreHardDeletes = unsafeIgnoreHardDeletes;
            BetterOrdering          = betterOrdering;
            ReaderThreadsCount      = readerThreadsCount;
            AlwaysKeepScavenged     = alwaysKeepScavenged;
            SkipIndexScanOnReads    = skipIndexScanOnReads;
            ReduceFileCachePressure = reduceFileCachePressure;
            InitializationThreads   = initializationThreads;
        }
Esempio n. 14
0
 /// <summary>
 /// Sets how stats are stored. Default is Stream
 /// </summary>
 /// <param name="statsStorage">The storage method to use</param>
 /// <returns>A <see cref="VNodeBuilder"/> with the options set</returns>
 public VNodeBuilder WithStatsStorage(StatsStorage statsStorage)
 {
     _statsStorage = statsStorage;
     return this;
 }
Esempio n. 15
0
        // ReSharper restore FieldCanBeMadeReadOnly.Local

        protected VNodeBuilder()
        {
            _log = LogManager.GetLoggerFor<VNodeBuilder>();
            _statsStorage = StatsStorage.Stream;

            _chunkSize = TFConsts.ChunkSize;
            _dbPath = Path.Combine(Path.GetTempPath(), "EmbeddedEventStore", string.Format("{0:yyyy-MM-dd_HH.mm.ss.ffffff}-EmbeddedNode", DateTime.UtcNow));
            _chunksCacheSize = TFConsts.ChunksCacheSize;
            _cachedChunks = Opts.CachedChunksDefault;
            _inMemoryDb = true;
            _projectionType = ProjectionType.None;

            _externalTcp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalTcpPortDefault);
            _externalSecureTcp = null;
            _internalTcp = new IPEndPoint(Opts.InternalIpDefault, Opts.InternalTcpPortDefault);
            _internalSecureTcp = null;
            _externalHttp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
            _internalHttp = new IPEndPoint(Opts.InternalIpDefault, Opts.InternalHttpPortDefault);

            _intHttpPrefixes = new List<string>();
            _extHttpPrefixes = new List<string>();
            _addInterfacePrefixes = true;
            _enableTrustedAuth = Opts.EnableTrustedAuthDefault;
            _readerThreadsCount = Opts.ReaderThreadsCountDefault;
            _certificate = null;
            _workerThreads = Opts.WorkerThreadsDefault;

            _discoverViaDns = Opts.DiscoverViaDnsDefault;
            _clusterDns = Opts.ClusterDnsDefault;
            _gossipSeeds = new List<IPEndPoint>();

            _minFlushDelay = TimeSpan.FromMilliseconds(Opts.MinFlushDelayMsDefault);

            _clusterNodeCount = 1;
            _prepareAckCount = 1;
            _commitAckCount = 1;
            _prepareTimeout = TimeSpan.FromMilliseconds(Opts.PrepareTimeoutMsDefault);
            _commitTimeout = TimeSpan.FromMilliseconds(Opts.CommitTimeoutMsDefault);

            _nodePriority = Opts.NodePriorityDefault;

            _useSsl = Opts.UseInternalSslDefault;
            _sslTargetHost = Opts.SslTargetHostDefault;
            _sslValidateServer = Opts.SslValidateServerDefault;

            _statsPeriod = TimeSpan.FromSeconds(Opts.StatsPeriodDefault);

            _authenticationProviderFactory = new InternalAuthenticationProviderFactory();
            _disableScavengeMerging = Opts.DisableScavengeMergeDefault;
            _scavengeHistoryMaxAge = Opts.ScavengeHistoryMaxAgeDefault;
            _adminOnPublic = Opts.AdminOnExtDefault;
            _statsOnPublic = Opts.StatsOnExtDefault;
            _gossipOnPublic = Opts.GossipOnExtDefault;
            _gossipInterval = TimeSpan.FromMilliseconds(Opts.GossipIntervalMsDefault);
            _gossipAllowedTimeDifference = TimeSpan.FromMilliseconds(Opts.GossipAllowedDifferenceMsDefault);
            _gossipTimeout = TimeSpan.FromMilliseconds(Opts.GossipTimeoutMsDefault);

            _intTcpHeartbeatInterval = TimeSpan.FromMilliseconds(Opts.IntTcpHeartbeatIntervalDefault);
            _intTcpHeartbeatTimeout = TimeSpan.FromMilliseconds(Opts.IntTcpHeartbeatTimeoutDefault);
            _extTcpHeartbeatInterval = TimeSpan.FromMilliseconds(Opts.ExtTcpHeartbeatIntervalDefault);
            _extTcpHeartbeatTimeout = TimeSpan.FromMilliseconds(Opts.ExtTcpHeartbeatTimeoutDefault);

            _skipVerifyDbHashes = Opts.SkipDbVerifyDefault;
            _maxMemtableSize = Opts.MaxMemtableSizeDefault;
            _subsystems = new List<ISubsystem>();
            _clusterGossipPort = Opts.ClusterGossipPortDefault;

            _startStandardProjections = Opts.StartStandardProjectionsDefault;
            _disableHTTPCaching = Opts.DisableHttpCachingDefault;
            _logHttpRequests = Opts.LogHttpRequestsDefault;
            _enableHistograms = Opts.LogHttpRequestsDefault;
            _index = null;
            _indexCacheDepth = Opts.IndexCacheDepthDefault;
            _indexBitnessVersion = Opts.IndexBitnessVersionDefault;
            _unsafeIgnoreHardDelete = Opts.UnsafeIgnoreHardDeleteDefault;
            _betterOrdering = Opts.BetterOrderingDefault;
            _unsafeDisableFlushToDisk = Opts.UnsafeDisableFlushToDiskDefault;
            _alwaysKeepScavenged = Opts.AlwaysKeepScavengedDefault;
        }
Esempio n. 16
0
        internal static async Task Main(string[] args)
        {
            Config.TelemetryClient?.TrackEvent("startup");

            Console.WriteLine("Confinement: " + SandboxDetector.Detect());
            if (args.Length > 0 && args[0] == "--dry-run")
            {
                Console.WriteLine("Database path: " + Path.GetDirectoryName(Path.GetFullPath(DbImporter.GetDbPath("fake.db", Environment.SpecialFolder.ApplicationData))));
                if (Assembly.GetEntryAssembly().GetCustomAttribute <UserSecretsIdAttribute>() != null)
                {
                    Console.WriteLine("Bot config path: " + Path.GetDirectoryName(Path.GetFullPath(Config.GoogleApiConfigPath)));
                }
                return;
            }

            if (Process.GetCurrentProcess().Id == 0)
            {
                Config.Log.Info("Well, this was unexpected");
            }
            var singleInstanceCheckThread = new Thread(() =>
            {
                using var instanceLock = new Mutex(false, @"Global\RPCS3 Compatibility Bot");
                if (instanceLock.WaitOne(1000))
                {
                    try
                    {
                        InstanceCheck.Release();
                        ShutdownCheck.Wait();
                    }
                    finally
                    {
                        instanceLock.ReleaseMutex();
                    }
                }
            });

            try
            {
                singleInstanceCheckThread.Start();
                if (!await InstanceCheck.WaitAsync(1000).ConfigureAwait(false))
                {
                    Config.Log.Fatal("Another instance is already running.");
                    return;
                }

                if (string.IsNullOrEmpty(Config.Token) || Config.Token.Length < 16)
                {
                    Config.Log.Fatal("No token was specified.");
                    return;
                }

                if (SandboxDetector.Detect() == SandboxType.Docker)
                {
                    Config.Log.Info("Checking for updates...");
                    try
                    {
                        var(updated, stdout) = await Sudo.Bot.UpdateAsync().ConfigureAwait(false);

                        if (!string.IsNullOrEmpty(stdout) && updated)
                        {
                            Config.Log.Debug(stdout);
                        }
                        if (updated)
                        {
                            Sudo.Bot.Restart(InvalidChannelId, "Restarted due to new bot updates not present in this Docker image");
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        Config.Log.Error(e, "Failed to check for updates");
                    }
                }

                using (var db = new BotDb())
                    if (!await DbImporter.UpgradeAsync(db, Config.Cts.Token))
                    {
                        return;
                    }

                using (var db = new ThumbnailDb())
                    if (!await DbImporter.UpgradeAsync(db, Config.Cts.Token))
                    {
                        return;
                    }

                await SqlConfiguration.RestoreAsync().ConfigureAwait(false);

                Config.Log.Debug("Restored configuration variables from persistent storage");

                await StatsStorage.RestoreAsync().ConfigureAwait(false);

                Config.Log.Debug("Restored stats from persistent storage");

                var backgroundTasks = Task.WhenAll(
                    AmdDriverVersionProvider.RefreshAsync(),
#if !DEBUG
                    new PsnScraper().RunAsync(Config.Cts.Token),
                    GameTdbScraper.RunAsync(Config.Cts.Token),
#endif
                    StatsStorage.BackgroundSaveAsync(),
                    CompatList.ImportCompatListAsync()
                    );

                try
                {
                    if (!Directory.Exists(Config.IrdCachePath))
                    {
                        Directory.CreateDirectory(Config.IrdCachePath);
                    }
                }
                catch (Exception e)
                {
                    Config.Log.Warn(e, $"Failed to create new folder {Config.IrdCachePath}: {e.Message}");
                }

                var config = new DiscordConfiguration
                {
                    Token            = Config.Token,
                    TokenType        = TokenType.Bot,
                    MessageCacheSize = Config.MessageCacheSize,
                    LoggerFactory    = Config.LoggerFactory,
                };
                using var client = new DiscordClient(config);
                var commands = client.UseCommandsNext(new CommandsNextConfiguration
                {
                    StringPrefixes = new[] { Config.CommandPrefix, Config.AutoRemoveCommandPrefix },
                    Services       = new ServiceCollection().BuildServiceProvider(),
                });
                commands.RegisterConverter(new TextOnlyDiscordChannelConverter());
                commands.RegisterCommands <Misc>();
                commands.RegisterCommands <CompatList>();
                commands.RegisterCommands <Sudo>();
                commands.RegisterCommands <CommandsManagement>();
                commands.RegisterCommands <ContentFilters>();
                commands.RegisterCommands <Warnings>();
                commands.RegisterCommands <Explain>();
                commands.RegisterCommands <Psn>();
                commands.RegisterCommands <Invites>();
                commands.RegisterCommands <Moderation>();
                commands.RegisterCommands <Ird>();
                commands.RegisterCommands <BotMath>();
                commands.RegisterCommands <Pr>();
                commands.RegisterCommands <Events>();
                commands.RegisterCommands <E3>();
                commands.RegisterCommands <Cyberpunk2077>();
                commands.RegisterCommands <Rpcs3Ama>();
                commands.RegisterCommands <BotStats>();
                commands.RegisterCommands <Syscall>();
                commands.RegisterCommands <ForcedNicknames>();
                commands.RegisterCommands <Minesweeper>();

                if (!string.IsNullOrEmpty(Config.AzureComputerVisionKey))
                {
                    commands.RegisterCommands <Vision>();
                }

                commands.CommandErrored += UnknownCommandHandler.OnError;

                client.UseInteractivity(new InteractivityConfiguration());

                client.Ready += async(c, _) =>
                {
                    var admin = await c.GetUserAsync(Config.BotAdminId).ConfigureAwait(false);

                    Config.Log.Info("Bot is ready to serve!");
                    Config.Log.Info("");
                    Config.Log.Info($"Bot user id : {c.CurrentUser.Id} ({c.CurrentUser.Username})");
                    Config.Log.Info($"Bot admin id : {Config.BotAdminId} ({admin.Username ?? "???"}#{admin.Discriminator ?? "????"})");
                    Config.Log.Info("");
                };
                client.GuildAvailable += async(c, gaArgs) =>
                {
                    await BotStatusMonitor.RefreshAsync(c).ConfigureAwait(false);

                    Watchdog.DisconnectTimestamps.Clear();
                    Watchdog.TimeSinceLastIncomingMessage.Restart();
                    if (gaArgs.Guild.Id != Config.BotGuildId)
                    {
#if DEBUG
                        Config.Log.Warn($"Unknown discord server {gaArgs.Guild.Id} ({gaArgs.Guild.Name})");
#else
                        Config.Log.Warn($"Unknown discord server {gaArgs.Guild.Id} ({gaArgs.Guild.Name}), leaving...");
                        await gaArgs.Guild.LeaveAsync().ConfigureAwait(false);
#endif
                        return;
                    }

                    Config.Log.Info($"Server {gaArgs.Guild.Name} is available now");
                    Config.Log.Info($"Checking moderation backlogs in {gaArgs.Guild.Name}...");
                    try
                    {
                        await Task.WhenAll(
                            Starbucks.CheckBacklogAsync(c, gaArgs.Guild).ContinueWith(_ => Config.Log.Info($"Starbucks backlog checked in {gaArgs.Guild.Name}."), TaskScheduler.Default),
                            DiscordInviteFilter.CheckBacklogAsync(c, gaArgs.Guild).ContinueWith(_ => Config.Log.Info($"Discord invites backlog checked in {gaArgs.Guild.Name}."), TaskScheduler.Default)
                            ).ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        Config.Log.Warn(e, "Error running backlog tasks");
                    }
                    Config.Log.Info($"All moderation backlogs checked in {gaArgs.Guild.Name}.");
                };
                client.GuildAvailable   += (c, _) => UsernameValidationMonitor.MonitorAsync(c, true);
                client.GuildUnavailable += (_, guArgs) =>
                {
                    Config.Log.Warn($"{guArgs.Guild.Name} is unavailable");
                    return(Task.CompletedTask);
                };
#if !DEBUG
/*
 *              client.GuildDownloadCompleted += async gdcArgs =>
 *                                               {
 *                                                   foreach (var guild in gdcArgs.Guilds)
 *                                                       await ModProvider.SyncRolesAsync(guild.Value).ConfigureAwait(false);
 *                                               };
 */
#endif
                client.MessageReactionAdded += Starbucks.Handler;
                client.MessageReactionAdded += ContentFilterMonitor.OnReaction;

                client.MessageCreated += (_, __) => { Watchdog.TimeSinceLastIncomingMessage.Restart(); return(Task.CompletedTask); };
                client.MessageCreated += ContentFilterMonitor.OnMessageCreated; // should be first
                client.MessageCreated += GlobalMessageCache.OnMessageCreated;
                var mediaScreenshotMonitor = new MediaScreenshotMonitor(client);
                if (!string.IsNullOrEmpty(Config.AzureComputerVisionKey))
                {
                    client.MessageCreated += mediaScreenshotMonitor.OnMessageCreated;
                }
                client.MessageCreated += ProductCodeLookup.OnMessageCreated;
                client.MessageCreated += LogParsingHandler.OnMessageCreated;
                client.MessageCreated += LogAsTextMonitor.OnMessageCreated;
                client.MessageCreated += DiscordInviteFilter.OnMessageCreated;
                client.MessageCreated += PostLogHelpHandler.OnMessageCreated;
                client.MessageCreated += BotReactionsHandler.OnMessageCreated;
                client.MessageCreated += GithubLinksHandler.OnMessageCreated;
                client.MessageCreated += NewBuildsMonitor.OnMessageCreated;
                client.MessageCreated += TableFlipMonitor.OnMessageCreated;
                client.MessageCreated += IsTheGamePlayableHandler.OnMessageCreated;
                client.MessageCreated += EmpathySimulationHandler.OnMessageCreated;

                client.MessageUpdated += GlobalMessageCache.OnMessageUpdated;
                client.MessageUpdated += ContentFilterMonitor.OnMessageUpdated;
                client.MessageUpdated += DiscordInviteFilter.OnMessageUpdated;
                client.MessageUpdated += EmpathySimulationHandler.OnMessageUpdated;

                client.MessageDeleted += GlobalMessageCache.OnMessageDeleted;
                if (Config.DeletedMessagesLogChannelId > 0)
                {
                    client.MessageDeleted += DeletedMessagesMonitor.OnMessageDeleted;
                }
                client.MessageDeleted += ThumbnailCacheMonitor.OnMessageDeleted;
                client.MessageDeleted += EmpathySimulationHandler.OnMessageDeleted;

                client.MessagesBulkDeleted += GlobalMessageCache.OnMessagesBulkDeleted;

                client.UserUpdated += UsernameSpoofMonitor.OnUserUpdated;
                client.UserUpdated += UsernameZalgoMonitor.OnUserUpdated;

                client.GuildMemberAdded += Greeter.OnMemberAdded;
                client.GuildMemberAdded += UsernameSpoofMonitor.OnMemberAdded;
                client.GuildMemberAdded += UsernameZalgoMonitor.OnMemberAdded;
                client.GuildMemberAdded += UsernameValidationMonitor.OnMemberAdded;

                client.GuildMemberUpdated += UsernameSpoofMonitor.OnMemberUpdated;
                client.GuildMemberUpdated += UsernameZalgoMonitor.OnMemberUpdated;
                client.GuildMemberUpdated += UsernameValidationMonitor.OnMemberUpdated;

                Watchdog.DisconnectTimestamps.Enqueue(DateTime.UtcNow);

                try
                {
                    await client.ConnectAsync().ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Config.Log.Error(e, "Failed to connect to Discord: " + e.Message);
                    throw;
                }

                ulong? channelId  = null;
                string restartMsg = null;
                using (var db = new BotDb())
                {
                    var chState = db.BotState.FirstOrDefault(k => k.Key == "bot-restart-channel");
                    if (chState != null)
                    {
                        if (ulong.TryParse(chState.Value, out var ch))
                        {
                            channelId = ch;
                        }
                        db.BotState.Remove(chState);
                    }
                    var msgState = db.BotState.FirstOrDefault(i => i.Key == "bot-restart-msg");
                    if (msgState != null)
                    {
                        restartMsg = msgState.Value;
                        db.BotState.Remove(msgState);
                    }
                    db.SaveChanges();
                }
                if (string.IsNullOrEmpty(restartMsg))
                {
                    restartMsg = null;
                }

                if (channelId.HasValue)
                {
                    Config.Log.Info($"Found channelId {channelId}");
                    DiscordChannel channel;
                    if (channelId == InvalidChannelId)
                    {
                        channel = await client.GetChannelAsync(Config.ThumbnailSpamId).ConfigureAwait(false);

                        await channel.SendMessageAsync(restartMsg ?? "Bot has suffered some catastrophic failure and was restarted").ConfigureAwait(false);
                    }
                    else
                    {
                        channel = await client.GetChannelAsync(channelId.Value).ConfigureAwait(false);

                        await channel.SendMessageAsync("Bot is up and running").ConfigureAwait(false);
                    }
                }
                else
                {
                    Config.Log.Debug($"Args count: {args.Length}");
                    var pArgs = args.Select(a => a == Config.Token ? "<Token>" : $"[{a}]");
                    Config.Log.Debug("Args: " + string.Join(" ", pArgs));
                }

                Config.Log.Debug("Running RPCS3 update check thread");
                backgroundTasks = Task.WhenAll(
                    backgroundTasks,
                    NewBuildsMonitor.MonitorAsync(client),
                    Watchdog.Watch(client),
                    InviteWhitelistProvider.CleanupAsync(client),
                    UsernameValidationMonitor.MonitorAsync(client),
                    Psn.Check.MonitorFwUpdates(client, Config.Cts.Token),
                    Watchdog.SendMetrics(client),
                    Watchdog.CheckGCStats(),
                    mediaScreenshotMonitor.ProcessWorkQueue()
                    );

                while (!Config.Cts.IsCancellationRequested)
                {
                    if (client.Ping > 1000)
                    {
                        Config.Log.Warn($"High ping detected: {client.Ping}");
                    }
                    await Task.Delay(TimeSpan.FromMinutes(1), Config.Cts.Token).ContinueWith(dt => { /* in case it was cancelled */ }, TaskScheduler.Default).ConfigureAwait(false);
                }
                await backgroundTasks.ConfigureAwait(false);
            }
            catch (Exception e)
            {
                if (!Config.inMemorySettings.ContainsKey("shutdown"))
                {
                    Config.Log.Fatal(e, "Experienced catastrophic failure, attempting to restart...");
                }
            }
            finally
            {
                Config.TelemetryClient?.Flush();
                ShutdownCheck.Release();
                if (singleInstanceCheckThread.IsAlive)
                {
                    singleInstanceCheckThread.Join(100);
                }
            }
            if (!Config.inMemorySettings.ContainsKey("shutdown"))
            {
                Sudo.Bot.Restart(InvalidChannelId, null);
            }
        }
Esempio n. 17
0
        public ClusterVNodeSettings(Guid instanceId, int debugIndex,
                                    IPEndPoint internalTcpEndPoint,
                                    IPEndPoint internalSecureTcpEndPoint,
                                    IPEndPoint externalTcpEndPoint,
                                    IPEndPoint externalSecureTcpEndPoint,
                                    IPEndPoint internalHttpEndPoint,
                                    IPEndPoint externalHttpEndPoint,
                                    GossipAdvertiseInfo gossipAdvertiseInfo,
                                    bool enableTrustedAuth,
                                    X509Certificate2 certificate,
                                    X509Certificate2Collection trustedRootCerts,
                                    int workerThreads,
                                    bool discoverViaDns,
                                    string clusterDns,
                                    IPEndPoint[] gossipSeeds,
                                    TimeSpan minFlushDelay,
                                    int clusterNodeCount,
                                    int prepareAckCount,
                                    int commitAckCount,
                                    TimeSpan prepareTimeout,
                                    TimeSpan commitTimeout,
                                    TimeSpan writeTimeout,
                                    bool disableInternalTls,
                                    bool disableExternalTls,
                                    TimeSpan statsPeriod,
                                    StatsStorage statsStorage,
                                    int nodePriority,
                                    AuthenticationProviderFactory authenticationProviderFactory,
                                    AuthorizationProviderFactory authorizationProviderFactory,
                                    bool disableScavengeMerging,
                                    int scavengeHistoryMaxAge,
                                    bool adminOnPublic,
                                    bool statsOnPublic,
                                    bool gossipOnPublic,
                                    TimeSpan gossipInterval,
                                    TimeSpan gossipAllowedTimeDifference,
                                    TimeSpan gossipTimeout,
                                    TimeSpan intTcpHeartbeatTimeout,
                                    TimeSpan intTcpHeartbeatInterval,
                                    TimeSpan extTcpHeartbeatTimeout,
                                    TimeSpan extTcpHeartbeatInterval,
                                    TimeSpan deadMemberRemovalPeriod,
                                    bool verifyDbHash,
                                    int maxMemtableEntryCount,
                                    int hashCollisionReadLimit,
                                    bool startStandardProjections,
                                    bool disableHTTPCaching,
                                    bool logHttpRequests,
                                    int connectionPendingSendBytesThreshold,
                                    int connectionQueueSizeThreshold,
                                    int ptableMaxReaderCount,
                                    string index             = null, bool enableHistograms = false,
                                    bool skipIndexVerify     = false,
                                    int indexCacheDepth      = 16,
                                    byte indexBitnessVersion = 4,
                                    bool optimizeIndexMerge  = false,
                                    IPersistentSubscriptionConsumerStrategyFactory[] additionalConsumerStrategies = null,
                                    bool unsafeIgnoreHardDeletes            = false,
                                    int readerThreadsCount                  = 4,
                                    bool alwaysKeepScavenged                = false,
                                    bool gossipOnSingleNode                 = false,
                                    bool skipIndexScanOnReads               = false,
                                    bool reduceFileCachePressure            = false,
                                    int initializationThreads               = 1,
                                    bool faultOutOfOrderProjections         = false,
                                    int maxAutoMergeIndexLevel              = 1000,
                                    bool disableFirstLevelHttpAuthorization = false,
                                    bool logFailedAuthenticationAttempts    = false,
                                    long maxTruncation           = 256 * 1024 * 1024,
                                    bool readOnlyReplica         = false,
                                    int maxAppendSize            = 1024 * 1024,
                                    bool unsafeAllowSurplusNodes = false,
                                    bool enableExternalTCP       = false,
                                    bool enableAtomPubOverHTTP   = true,
                                    bool gossipOverHttps         = true)
        {
            Ensure.NotEmptyGuid(instanceId, "instanceId");
            Ensure.Equal(false, internalTcpEndPoint == null && internalSecureTcpEndPoint == null, "Both internal TCP endpoints are null");

            Ensure.NotNull(internalHttpEndPoint, "internalHttpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            if (internalSecureTcpEndPoint != null || externalSecureTcpEndPoint != null)
            {
                Ensure.NotNull(certificate, "certificate");
            }
            Ensure.Positive(workerThreads, "workerThreads");
            Ensure.NotNull(clusterDns, "clusterDns");
            Ensure.NotNull(gossipSeeds, "gossipSeeds");
            Ensure.Positive(clusterNodeCount, "clusterNodeCount");
            Ensure.Positive(prepareAckCount, "prepareAckCount");
            Ensure.Positive(commitAckCount, "commitAckCount");
            Ensure.Positive(initializationThreads, "initializationThreads");
            Ensure.NotNull(gossipAdvertiseInfo, "gossipAdvertiseInfo");
            if (maxAppendSize > TFConsts.EffectiveMaxLogRecordSize)
            {
                throw new ArgumentOutOfRangeException(nameof(maxAppendSize), $"{nameof(maxAppendSize)} exceeded {TFConsts.EffectiveMaxLogRecordSize} bytes.");
            }

            if (discoverViaDns && string.IsNullOrWhiteSpace(clusterDns))
            {
                throw new ArgumentException(
                          "Either DNS Discovery must be disabled (and seeds specified), or a cluster DNS name must be provided.");
            }

            NodeInfo = new VNodeInfo(instanceId, debugIndex,
                                     internalTcpEndPoint, internalSecureTcpEndPoint,
                                     externalTcpEndPoint, externalSecureTcpEndPoint,
                                     internalHttpEndPoint, externalHttpEndPoint,
                                     readOnlyReplica);
            GossipAdvertiseInfo = gossipAdvertiseInfo;
            EnableTrustedAuth   = enableTrustedAuth;
            Certificate         = certificate;
            TrustedRootCerts    = trustedRootCerts;

            WorkerThreads                   = workerThreads;
            StartStandardProjections        = startStandardProjections;
            EnableAtomPubOverHTTP           = enableAtomPubOverHTTP;
            DisableHTTPCaching              = disableHTTPCaching;
            LogHttpRequests                 = logHttpRequests;
            LogFailedAuthenticationAttempts = logFailedAuthenticationAttempts;
            AdditionalConsumerStrategies    =
                additionalConsumerStrategies ?? new IPersistentSubscriptionConsumerStrategyFactory[0];

            DiscoverViaDns     = discoverViaDns;
            ClusterDns         = clusterDns;
            GossipSeeds        = gossipSeeds;
            GossipOnSingleNode = gossipOnSingleNode;

            ClusterNodeCount = clusterNodeCount;
            MinFlushDelay    = minFlushDelay;
            PrepareAckCount  = prepareAckCount;
            CommitAckCount   = commitAckCount;
            PrepareTimeout   = prepareTimeout;
            CommitTimeout    = commitTimeout;
            WriteTimeout     = writeTimeout;

            DisableInternalTls = disableInternalTls;
            DisableExternalTls = disableExternalTls;
            EnableExternalTCP  = enableExternalTCP;

            StatsPeriod  = statsPeriod;
            StatsStorage = statsStorage;

            AuthenticationProviderFactory      = authenticationProviderFactory;
            AuthorizationProviderFactory       = authorizationProviderFactory;
            DisableFirstLevelHttpAuthorization = disableFirstLevelHttpAuthorization;

            NodePriority                        = nodePriority;
            DisableScavengeMerging              = disableScavengeMerging;
            ScavengeHistoryMaxAge               = scavengeHistoryMaxAge;
            AdminOnPublic                       = adminOnPublic;
            StatsOnPublic                       = statsOnPublic;
            GossipOnPublic                      = gossipOnPublic;
            GossipInterval                      = gossipInterval;
            GossipAllowedTimeDifference         = gossipAllowedTimeDifference;
            GossipTimeout                       = gossipTimeout;
            GossipOverHttps                     = gossipOverHttps;
            IntTcpHeartbeatTimeout              = intTcpHeartbeatTimeout;
            IntTcpHeartbeatInterval             = intTcpHeartbeatInterval;
            ExtTcpHeartbeatTimeout              = extTcpHeartbeatTimeout;
            ExtTcpHeartbeatInterval             = extTcpHeartbeatInterval;
            ConnectionPendingSendBytesThreshold = connectionPendingSendBytesThreshold;
            ConnectionQueueSizeThreshold        = connectionQueueSizeThreshold;
            DeadMemberRemovalPeriod             = deadMemberRemovalPeriod;

            VerifyDbHash           = verifyDbHash;
            MaxMemtableEntryCount  = maxMemtableEntryCount;
            HashCollisionReadLimit = hashCollisionReadLimit;

            EnableHistograms    = enableHistograms;
            SkipIndexVerify     = skipIndexVerify;
            IndexCacheDepth     = indexCacheDepth;
            IndexBitnessVersion = indexBitnessVersion;
            OptimizeIndexMerge  = optimizeIndexMerge;
            Index = index;
            UnsafeIgnoreHardDeletes    = unsafeIgnoreHardDeletes;
            ReaderThreadsCount         = readerThreadsCount;
            AlwaysKeepScavenged        = alwaysKeepScavenged;
            SkipIndexScanOnReads       = skipIndexScanOnReads;
            ReduceFileCachePressure    = reduceFileCachePressure;
            InitializationThreads      = initializationThreads;
            MaxAutoMergeIndexLevel     = maxAutoMergeIndexLevel;
            FaultOutOfOrderProjections = faultOutOfOrderProjections;
            ReadOnlyReplica            = readOnlyReplica;
            MaxAppendSize           = maxAppendSize;
            PTableMaxReaderCount    = ptableMaxReaderCount;
            UnsafeAllowSurplusNodes = unsafeAllowSurplusNodes;
            MaxTruncation           = maxTruncation;
        }
Esempio n. 18
0
        public ClusterVNodeSettings(Guid instanceId, int debugIndex,
                                    IPEndPoint internalTcpEndPoint,
                                    IPEndPoint internalSecureTcpEndPoint,
                                    IPEndPoint externalTcpEndPoint,
                                    IPEndPoint externalSecureTcpEndPoint,
                                    IPEndPoint internalHttpEndPoint,
                                    IPEndPoint externalHttpEndPoint,
                                    string[] httpPrefixes,
                                    bool enableTrustedAuth,
                                    X509Certificate2 certificate,
                                    int workerThreads,
                                    bool discoverViaDns,
                                    string clusterDns,
                                    IPEndPoint[] gossipSeeds,
                                    TimeSpan minFlushDelay,
                                    int clusterNodeCount,
                                    int prepareAckCount,
                                    int commitAckCount,
                                    TimeSpan prepareTimeout,
                                    TimeSpan commitTimeout,
                                    bool useSsl,
                                    string sslTargetHost,
                                    bool sslValidateServer,
                                    TimeSpan statsPeriod,
                                    StatsStorage statsStorage,
                                    int nodePriority,
                                    IAuthenticationProviderFactory authenticationProviderFactory,
                                    bool disableScavengeMerging,
                                    bool adminOnPublic,
                                    bool statsOnPublic,
                                    bool gossipOnPublic,
                                    TimeSpan gossipInterval,
                                    TimeSpan gossipAllowedTimeDifference,
                                    TimeSpan gossipTimeout,
                                    TimeSpan intTcpHeartbeatTimeout,
                                    TimeSpan intTcpHeartbeatInterval,
                                    TimeSpan extTcpHeartbeatTimeout,
                                    TimeSpan extTcpHeartbeatInterval,
                                    bool verifyDbHash,
                                    int maxMemtableEntryCount)
        {
            Ensure.NotEmptyGuid(instanceId, "instanceId");
            Ensure.NotNull(internalTcpEndPoint, "internalTcpEndPoint");
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(internalHttpEndPoint, "internalHttpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");
            Ensure.NotNull(httpPrefixes, "httpPrefixes");
            if (internalSecureTcpEndPoint != null || externalSecureTcpEndPoint != null)
            {
                Ensure.NotNull(certificate, "certificate");
            }
            Ensure.Positive(workerThreads, "workerThreads");
            Ensure.NotNull(clusterDns, "clusterDns");
            Ensure.NotNull(gossipSeeds, "gossipSeeds");
            Ensure.Positive(clusterNodeCount, "clusterNodeCount");
            Ensure.Positive(prepareAckCount, "prepareAckCount");
            Ensure.Positive(commitAckCount, "commitAckCount");

            if (discoverViaDns && string.IsNullOrWhiteSpace(clusterDns))
            {
                throw new ArgumentException("Either DNS Discovery must be disabled (and seeds specified), or a cluster DNS name must be provided.");
            }

            if (useSsl)
            {
                Ensure.NotNull(sslTargetHost, "sslTargetHost");
            }

            NodeInfo = new VNodeInfo(instanceId, debugIndex,
                                     internalTcpEndPoint, internalSecureTcpEndPoint,
                                     externalTcpEndPoint, externalSecureTcpEndPoint,
                                     internalHttpEndPoint, externalHttpEndPoint);
            HttpPrefixes      = httpPrefixes;
            EnableTrustedAuth = enableTrustedAuth;
            Certificate       = certificate;
            WorkerThreads     = workerThreads;

            DiscoverViaDns = discoverViaDns;
            ClusterDns     = clusterDns;
            GossipSeeds    = gossipSeeds;

            ClusterNodeCount = clusterNodeCount;
            MinFlushDelay    = minFlushDelay;
            PrepareAckCount  = prepareAckCount;
            CommitAckCount   = commitAckCount;
            PrepareTimeout   = prepareTimeout;
            CommitTimeout    = commitTimeout;

            UseSsl            = useSsl;
            SslTargetHost     = sslTargetHost;
            SslValidateServer = sslValidateServer;

            StatsPeriod  = statsPeriod;
            StatsStorage = statsStorage;

            AuthenticationProviderFactory = authenticationProviderFactory;

            NodePriority                = nodePriority;
            DisableScavengeMerging      = disableScavengeMerging;
            AdminOnPublic               = adminOnPublic;
            StatsOnPublic               = statsOnPublic;
            GossipOnPublic              = gossipOnPublic;
            GossipInterval              = gossipInterval;
            GossipAllowedTimeDifference = gossipAllowedTimeDifference;
            GossipTimeout               = gossipTimeout;
            IntTcpHeartbeatTimeout      = intTcpHeartbeatTimeout;
            IntTcpHeartbeatInterval     = intTcpHeartbeatInterval;
            ExtTcpHeartbeatTimeout      = extTcpHeartbeatTimeout;
            ExtTcpHeartbeatInterval     = extTcpHeartbeatInterval;

            VerifyDbHash          = verifyDbHash;
            MaxMemtableEntryCount = maxMemtableEntryCount;
        }
        // ReSharper restore FieldCanBeMadeReadOnly.Local

        private EmbeddedVNodeBuilder()
        {
            _chunkSize = TFConsts.ChunkSize;
            _dbPath = Path.Combine(Path.GetTempPath(), "EmbeddedEventStore", string.Format("{0:yyyy-MM-dd_HH.mm.ss.ffffff}-EmbeddedNode", DateTime.UtcNow));
            _chunksCacheSize = TFConsts.ChunksCacheSize;
            _inMemoryDb = true;

            _externalTcp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
            _externalSecureTcp = null;
            _externalTcp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
            _externalSecureTcp = null;
            _externalHttp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
            _externalHttp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);

            _httpPrefixes = new List<string>();
            _enableTrustedAuth = Opts.EnableTrustedAuthDefault;
            _certificate = null;
            _workerThreads = Opts.WorkerThreadsDefault;

            _discoverViaDns = false;
            _clusterDns = Opts.ClusterDnsDefault;
            _gossipSeeds = new List<IPEndPoint>();

            _minFlushDelay = TimeSpan.FromMilliseconds(Opts.MinFlushDelayMsDefault);

            _clusterNodeCount = 1;
            _prepareAckCount = 1;
            _commitAckCount = 1;
            _prepareTimeout = TimeSpan.FromMilliseconds(Opts.PrepareTimeoutMsDefault);
            _commitTimeout = TimeSpan.FromMilliseconds(Opts.CommitTimeoutMsDefault);

            _nodePriority = Opts.NodePriorityDefault;

            _useSsl = Opts.UseInternalSslDefault;
            _sslTargetHost = Opts.SslTargetHostDefault;
            _sslValidateServer = Opts.SslValidateServerDefault;

            _statsPeriod = TimeSpan.FromSeconds(Opts.StatsPeriodDefault);
            _statsStorage = StatsStorage.Stream;

            _authenticationProviderFactory = new InternalAuthenticationProviderFactory();
            _disableScavengeMerging = Opts.DisableScavengeMergeDefault;
            _adminOnPublic = Opts.AdminOnExtDefault;
            _statsOnPublic = Opts.StatsOnExtDefault;
            _gossipOnPublic = Opts.GossipOnExtDefault;
            _gossipInterval = TimeSpan.FromMilliseconds(Opts.GossipIntervalMsDefault);
            _gossipAllowedTimeDifference = TimeSpan.FromMilliseconds(Opts.GossipAllowedDifferenceMsDefault);
            _gossipTimeout = TimeSpan.FromMilliseconds(Opts.GossipTimeoutMsDefault);

            _intTcpHeartbeatInterval = TimeSpan.FromMilliseconds(Opts.IntTcpHeartbeatInvervalDefault);
            _intTcpHeartbeatTimeout = TimeSpan.FromMilliseconds(Opts.IntTcpHeartbeatTimeoutDefault);
            _extTcpHeartbeatInterval = TimeSpan.FromMilliseconds(Opts.ExtTcpHeartbeatIntervalDefault);
            _extTcpHeartbeatTimeout = TimeSpan.FromMilliseconds(Opts.ExtTcpHeartbeatTimeoutDefault);

            _skipVerifyDbHashes = Opts.SkipDbVerifyDefault;
            _maxMemtableSize = Opts.MaxMemtableSizeDefault;
            _subsystems = new List<ISubsystem>();
            _clusterGossipPort = Opts.ClusterGossipPortDefault;
        }
Esempio n. 20
0
        internal static async Task Main(string[] args)
        {
            var singleInstanceCheckThread = new Thread(() =>
            {
                using (var instanceLock = new Mutex(false, @"Global\RPCS3 Compatibility Bot"))
                {
                    if (instanceLock.WaitOne(1000))
                    {
                        try
                        {
                            InstanceCheck.Release();
                            ShutdownCheck.Wait();
                        }
                        finally
                        {
                            instanceLock.ReleaseMutex();
                        }
                    }
                }
            });

            try
            {
                singleInstanceCheckThread.Start();
                if (!await InstanceCheck.WaitAsync(1000).ConfigureAwait(false))
                {
                    Config.Log.Fatal("Another instance is already running.");
                    return;
                }

                if (string.IsNullOrEmpty(Config.Token))
                {
                    Config.Log.Fatal("No token was specified.");
                    return;
                }

                using (var db = new BotDb())
                    if (!await DbImporter.UpgradeAsync(db, Config.Cts.Token))
                    {
                        return;
                    }

                using (var db = new ThumbnailDb())
                    if (!await DbImporter.UpgradeAsync(db, Config.Cts.Token))
                    {
                        return;
                    }

                await StatsStorage.RestoreAsync().ConfigureAwait(false);

                Config.Log.Debug("Restored stats from persistent storage");

                var backgroundTasks = Task.WhenAll(
                    AmdDriverVersionProvider.RefreshAsync(),
                    new PsnScraper().RunAsync(Config.Cts.Token),
                    GameTdbScraper.RunAsync(Config.Cts.Token),
                    new AppveyorClient.Client().GetBuildAsync(Guid.NewGuid().ToString(), Config.Cts.Token),
                    StatsStorage.BackgroundSaveAsync()
                    );

                try
                {
                    if (!Directory.Exists(Config.IrdCachePath))
                    {
                        Directory.CreateDirectory(Config.IrdCachePath);
                    }
                }
                catch (Exception e)
                {
                    Config.Log.Warn(e, $"Failed to create new folder {Config.IrdCachePath}: {e.Message}");
                }

                var config = new DiscordConfiguration
                {
                    Token     = Config.Token,
                    TokenType = TokenType.Bot,
                };
                using (var client = new DiscordClient(config))
                {
                    var commands = client.UseCommandsNext(new CommandsNextConfiguration
                    {
                        StringPrefixes = new[] { Config.CommandPrefix, Config.AutoRemoveCommandPrefix },
                        Services       = new ServiceCollection().BuildServiceProvider(),
                    });
                    commands.RegisterConverter(new TextOnlyDiscordChannelConverter());
                    commands.RegisterCommands <Misc>();
                    commands.RegisterCommands <CompatList>();
                    commands.RegisterCommands <Sudo>();
                    commands.RegisterCommands <CommandsManagement>();
                    commands.RegisterCommands <ContentFilters>();
                    commands.RegisterCommands <Warnings>();
                    commands.RegisterCommands <Explain>();
                    commands.RegisterCommands <Psn>();
                    commands.RegisterCommands <Invites>();
                    commands.RegisterCommands <Moderation>();
                    commands.RegisterCommands <Ird>();
                    commands.RegisterCommands <BotMath>();
                    commands.RegisterCommands <Pr>();
                    commands.RegisterCommands <Events>();
                    commands.RegisterCommands <E3>();
                    commands.RegisterCommands <Cyberpunk2077>();
                    commands.RegisterCommands <Rpcs3Ama>();
                    commands.RegisterCommands <BotStats>();
                    commands.RegisterCommands <Syscall>();

                    commands.CommandErrored += UnknownCommandHandler.OnError;

                    var interactivityConfig = new InteractivityConfiguration {
                    };
                    client.UseInteractivity(interactivityConfig);

                    client.Ready += async r =>
                    {
                        Config.Log.Info("Bot is ready to serve!");
                        Config.Log.Info("");
                        Config.Log.Info($"Bot user id : {r.Client.CurrentUser.Id} ({r.Client.CurrentUser.Username})");
                        Config.Log.Info($"Bot admin id : {Config.BotAdminId} ({(await r.Client.GetUserAsync(Config.BotAdminId)).Username})");
                        Config.Log.Info("");
                    };
                    client.GuildAvailable += async gaArgs =>
                    {
                        await BotStatusMonitor.RefreshAsync(gaArgs.Client).ConfigureAwait(false);

                        Watchdog.DisconnectTimestamps.Clear();
                        if (gaArgs.Guild.Id != Config.BotGuildId)
                        {
#if DEBUG
                            Config.Log.Warn($"Unknown discord server {gaArgs.Guild.Id} ({gaArgs.Guild.Name})");
#else
                            Config.Log.Warn($"Unknown discord server {gaArgs.Guild.Id} ({gaArgs.Guild.Name}), leaving...");
                            await gaArgs.Guild.LeaveAsync().ConfigureAwait(false);
#endif
                            return;
                        }

                        Config.Log.Info($"Server {gaArgs.Guild.Name} is available now");
                        Config.Log.Info($"Checking moderation backlogs in {gaArgs.Guild.Name}...");
                        try
                        {
                            await Task.WhenAll(
                                Starbucks.CheckBacklogAsync(gaArgs.Client, gaArgs.Guild).ContinueWith(_ => Config.Log.Info($"Starbucks backlog checked in {gaArgs.Guild.Name}."), TaskScheduler.Default),
                                DiscordInviteFilter.CheckBacklogAsync(gaArgs.Client, gaArgs.Guild).ContinueWith(_ => Config.Log.Info($"Discord invites backlog checked in {gaArgs.Guild.Name}."), TaskScheduler.Default)
                                ).ConfigureAwait(false);
                        }
                        catch (Exception e)
                        {
                            Config.Log.Warn(e, "Error running backlog tasks");
                        }
                        Config.Log.Info($"All moderation backlogs checked in {gaArgs.Guild.Name}.");
                    };
                    client.GuildUnavailable += guArgs =>
                    {
                        Config.Log.Warn($"{guArgs.Guild.Name} is unavailable");
                        return(Task.CompletedTask);
                    };

                    client.MessageReactionAdded += Starbucks.Handler;
                    client.MessageReactionAdded += AntipiracyMonitor.OnReaction;

                    client.MessageCreated += AntipiracyMonitor.OnMessageCreated; // should be first
                    client.MessageCreated += ProductCodeLookup.OnMessageCreated;
                    client.MessageCreated += LogParsingHandler.OnMessageCreated;
                    client.MessageCreated += LogAsTextMonitor.OnMessageCreated;
                    client.MessageCreated += DiscordInviteFilter.OnMessageCreated;
                    client.MessageCreated += PostLogHelpHandler.OnMessageCreated;
                    client.MessageCreated += BotReactionsHandler.OnMessageCreated;
                    client.MessageCreated += AppveyorLinksHandler.OnMessageCreated;
                    client.MessageCreated += GithubLinksHandler.OnMessageCreated;
                    client.MessageCreated += NewBuildsMonitor.OnMessageCreated;
                    client.MessageCreated += TableFlipMonitor.OnMessageCreated;
                    client.MessageCreated += IsTheGamePlayableHandler.OnMessageCreated;
                    client.MessageCreated += EmpathySimulationHandler.OnMessageCreated;

                    client.MessageUpdated += AntipiracyMonitor.OnMessageUpdated;
                    client.MessageUpdated += DiscordInviteFilter.OnMessageUpdated;
                    client.MessageUpdated += EmpathySimulationHandler.OnMessageUpdated;

                    client.MessageDeleted += ThumbnailCacheMonitor.OnMessageDeleted;
                    client.MessageDeleted += EmpathySimulationHandler.OnMessageDeleted;

                    client.UserUpdated += UsernameSpoofMonitor.OnUserUpdated;
                    client.UserUpdated += UsernameZalgoMonitor.OnUserUpdated;

                    client.GuildMemberAdded += Greeter.OnMemberAdded;
                    client.GuildMemberAdded += UsernameSpoofMonitor.OnMemberAdded;
                    client.GuildMemberAdded += UsernameZalgoMonitor.OnMemberAdded;

                    client.GuildMemberUpdated += UsernameSpoofMonitor.OnMemberUpdated;
                    client.GuildMemberUpdated += UsernameZalgoMonitor.OnMemberUpdated;

                    client.DebugLogger.LogMessageReceived += (sender, eventArgs) =>
                    {
                        Action <Exception, string> logLevel = Config.Log.Info;
                        if (eventArgs.Level == LogLevel.Debug)
                        {
                            logLevel = Config.Log.Debug;
                        }
                        else if (eventArgs.Level == LogLevel.Info)
                        {
                            //logLevel = Config.Log.Info;
                            if (eventArgs.Message?.Contains("Session resumed") ?? false)
                            {
                                Watchdog.DisconnectTimestamps.Clear();
                            }
                        }
                        else if (eventArgs.Level == LogLevel.Warning)
                        {
                            logLevel = Config.Log.Warn;
                            if (eventArgs.Message?.Contains("Dispatch:PRESENCES_REPLACE") ?? false)
                            {
                                BotStatusMonitor.RefreshAsync(client).ConfigureAwait(false).GetAwaiter().GetResult();
                            }
                        }
                        else if (eventArgs.Level == LogLevel.Error)
                        {
                            logLevel = Config.Log.Error;
                        }
                        else if (eventArgs.Level == LogLevel.Critical)
                        {
                            logLevel = Config.Log.Fatal;
                            if ((eventArgs.Message?.Contains("Socket connection terminated") ?? false) ||
                                (eventArgs.Message?.Contains("heartbeats were skipped. Issuing reconnect.") ?? false))
                            {
                                Watchdog.DisconnectTimestamps.Enqueue(DateTime.UtcNow);
                            }
                        }
                        logLevel(eventArgs.Exception, eventArgs.Message);
                    };
                    Watchdog.DisconnectTimestamps.Enqueue(DateTime.UtcNow);

                    await client.ConnectAsync().ConfigureAwait(false);

                    if (args.Length > 1 && ulong.TryParse(args[1], out var channelId))
                    {
                        Config.Log.Info($"Found channelId: {args[1]}");
                        DiscordChannel channel;
                        if (channelId == InvalidChannelId)
                        {
                            channel = await client.GetChannelAsync(Config.ThumbnailSpamId).ConfigureAwait(false);

                            await channel.SendMessageAsync("Bot has suffered some catastrophic failure and was restarted").ConfigureAwait(false);
                        }
                        else
                        {
                            channel = await client.GetChannelAsync(channelId).ConfigureAwait(false);

                            await channel.SendMessageAsync("Bot is up and running").ConfigureAwait(false);
                        }
                    }

                    Config.Log.Debug("Running RPCS3 update check thread");
                    backgroundTasks = Task.WhenAll(
                        backgroundTasks,
                        NewBuildsMonitor.MonitorAsync(client),
                        Watchdog.Watch(client),
                        InviteWhitelistProvider.CleanupAsync(client)
                        );

                    while (!Config.Cts.IsCancellationRequested)
                    {
                        if (client.Ping > 1000)
                        {
                            Config.Log.Warn($"High ping detected: {client.Ping}");
                        }
                        await Task.Delay(TimeSpan.FromMinutes(1), Config.Cts.Token).ContinueWith(dt => { /* in case it was cancelled */ }, TaskScheduler.Default).ConfigureAwait(false);
                    }
                }
                await backgroundTasks.ConfigureAwait(false);
            }
            catch (Exception e)
            {
                if (!Config.inMemorySettings.ContainsKey("shutdown"))
                {
                    Config.Log.Fatal(e, "Experienced catastrophic failure, attempting to restart...");
                }
            }
            finally
            {
                ShutdownCheck.Release();
                if (singleInstanceCheckThread.IsAlive)
                {
                    singleInstanceCheckThread.Join(100);
                }
            }
            if (!Config.inMemorySettings.ContainsKey("shutdown"))
            {
                Sudo.Bot.Restart(InvalidChannelId);
            }
        }