Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            InitUtils.Init();

            var config = Configuration.GetSection("PluralKit").Get <CoreConfig>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services
            .AddScoped <IDbConnection, NpgsqlConnection>(_ => new NpgsqlConnection(config.Database))
            .AddTransient <SystemStore>()
            .AddTransient <MemberStore>()
            .AddSingleton(config);
        }
Exemplo n.º 2
0
        private async Task MainAsync()
        {
            ThreadPool.SetMinThreads(32, 32);
            ThreadPool.SetMaxThreads(128, 128);

            Console.WriteLine("Starting PluralKit...");

            InitUtils.Init();

            // Set up a CancellationToken and a SIGINT hook to properly dispose of things when the app is closed
            // The Task.Delay line will throw/exit (forgot which) and the stack and using statements will properly unwind
            var token = new CancellationTokenSource();

            Console.CancelKeyPress += delegate(object e, ConsoleCancelEventArgs args)
            {
                args.Cancel = true;
                token.Cancel();
            };

            using (var services = BuildServiceProvider())
            {
                var logger     = services.GetRequiredService <ILogger>().ForContext <Initialize>();
                var coreConfig = services.GetRequiredService <CoreConfig>();
                var botConfig  = services.GetRequiredService <BotConfig>();
                var schema     = services.GetRequiredService <SchemaService>();

                using (Sentry.SentrySdk.Init(coreConfig.SentryUrl))
                {
                    logger.Information("Connecting to database");
                    await schema.ApplyMigrations();

                    logger.Information("Connecting to Discord");
                    var client = services.GetRequiredService <IDiscordClient>() as DiscordShardedClient;
                    await client.LoginAsync(TokenType.Bot, botConfig.Token);

                    logger.Information("Initializing bot");
                    await services.GetRequiredService <Bot>().Init();

                    await client.StartAsync();

                    try
                    {
                        await Task.Delay(-1, token.Token);
                    }
                    catch (TaskCanceledException) { } // We'll just exit normally
                    logger.Information("Shutting down");
                }
            }
        }
Exemplo n.º 3
0
        private async Task MainAsync()
        {
            ThreadPool.SetMinThreads(32, 32);
            ThreadPool.SetMaxThreads(128, 128);

            Console.WriteLine("Starting PluralKit...");

            InitUtils.Init();

            // Set up a CancellationToken and a SIGINT hook to properly dispose of things when the app is closed
            // The Task.Delay line will throw/exit (forgot which) and the stack and using statements will properly unwind
            var token = new CancellationTokenSource();

            Console.CancelKeyPress += delegate(object e, ConsoleCancelEventArgs args)
            {
                args.Cancel = true;
                token.Cancel();
            };

            var builder = new ContainerBuilder();

            builder.RegisterInstance(_config);
            builder.RegisterModule(new ConfigModule <BotConfig>("Bot"));
            builder.RegisterModule(new LoggingModule("bot"));
            builder.RegisterModule(new MetricsModule());
            builder.RegisterModule <DataStoreModule>();
            builder.RegisterModule <BotModule>();

            using var services = builder.Build();

            var logger = services.Resolve <ILogger>().ForContext <Initialize>();

            try
            {
                SchemaService.Initialize();

                var coreConfig = services.Resolve <CoreConfig>();
                var botConfig  = services.Resolve <BotConfig>();
                var schema     = services.Resolve <SchemaService>();

                using var _ = Sentry.SentrySdk.Init(coreConfig.SentryUrl);

                logger.Information("Connecting to database");
                await schema.ApplyMigrations();

                logger.Information("Connecting to Discord");
                var client = services.Resolve <DiscordShardedClient>();
                await client.LoginAsync(TokenType.Bot, botConfig.Token);

                logger.Information("Initializing bot");
                await client.StartAsync();

                await services.Resolve <Bot>().Init();

                try
                {
                    await Task.Delay(-1, token.Token);
                }
                catch (TaskCanceledException) { } // We'll just exit normally
            }
            catch (Exception e)
            {
                logger.Fatal(e, "Unrecoverable error while initializing bot");
            }

            logger.Information("Shutting down");
        }
Exemplo n.º 4
0
 public static void Main(string[] args)
 {
     InitUtils.Init();
     CreateHostBuilder(args).Build().Run();
 }