Example #1
0
        private static async Task Main(string[] args)
        {
            await ConfigureServices();

            var logger = _serviceProvider.GetRequiredService <ILogger <Program> >();

            if (!_serviceProvider.GetRequiredService <Config>().IsValid())
            {
                await Exit(ExitCodes.CONFIGURATION_IS_INVALID, "Configuration is not valid!");
            }

            var cancellationToken = new CancellationTokenSource();

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                eventArgs.Cancel = true;
                cancellationToken.Cancel();
            };

            logger.LogInformation($"Setting up the DB...");

            if (_serviceProvider.GetRequiredService <Config>().IsContainerized)
            {
                logger.LogInformation("App is containerized. - Giving some ahead time for DB to kick in...");
                await Task.Delay(10_000);
            }

            var connectionString = _serviceProvider.GetRequiredService <ConnectionString>();

            try
            {
                await DatabaseHelpers.SetupTables(connectionString);

                await DatabaseHelpers.SetupProcedures(connectionString);

                logger.LogInformation("DB is set up.");
            }
            catch (Exception)
            {
                await Exit(ExitCodes.COULD_NOT_CONNECT_TO_DB,
                           "Couldn't connect to DB! Check your connection..");
            }

            try
            {
                await Task.WhenAll(new Task[]
                {
                    Task.Run(() => DiscordBot.DiscordMainAsync(_serviceProvider, cancellationToken),
                             cancellationToken.Token),
                    Task.Run(() => DatabaseConnectionController.ControlDatabaseConnection(_serviceProvider, cancellationToken),
                             cancellationToken.Token)
                });
            }
            catch (Exception ex)
            {
                cancellationToken.Cancel();
                logger.LogCritical(ex, "Exception occured in one of the clients!");
            }
        }