Beispiel #1
0
        static void Main(string[] args)
        {
            Console.Title = "CDC to Redshift";

            // support graceful shutdown in Docker
            var ended    = new ManualResetEventSlim();
            var starting = new ManualResetEventSlim();

            AssemblyLoadContext.Default.Unloading += ctx =>
            {
                System.Console.WriteLine("Unloading fired");
                starting.Set();
                System.Console.WriteLine("Waiting for completion");
                ended.Wait();
            };

            // set up configuration
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddCommandLine(args)
                          .AddEnvironmentVariables("CDCTOOLS_"); // all environment variables with this prefix;

            IConfigurationRoot configuration = builder.Build();

            var executionId    = GetExecutionId(configuration);
            var runMode        = GetRunMode(configuration);
            var tables         = GetTables(configuration);
            var batchSize      = GetNonTransactionalTableBatchSize(configuration);
            var redshiftClient = GetRedshiftClient(configuration);

            var cts = new CancellationTokenSource();

            if (runMode == RunMode.FullLoad)
            {
                var cdcReaderClient  = new CdcReaderClient(configuration["DatabaseConnection"], configuration["StateManagmentConnection"]);
                var printMod         = GetPrintMod(configuration);
                var fullLoadExporter = new FullLoadExporter(cdcReaderClient, redshiftClient);
                fullLoadExporter.ExportTablesAsync(cts.Token, executionId, tables, batchSize, printMod).Wait();
                Console.WriteLine("Export to Redshift started.");

                Thread.Sleep(2000);
                bool shutdown = false;
                // wait for shutdown signal
#if DEBUG
                Console.WriteLine("Press any key to shutdown");

                while (!shutdown)
                {
                    if (Console.KeyAvailable)
                    {
                        shutdown = true;
                    }
                    else if (fullLoadExporter.HasFinished())
                    {
                        shutdown = true;
                    }

                    Thread.Sleep(500);
                }
#else
                while (!shutdown)
                {
                    if (starting.IsSet)
                    {
                        shutdown = true;
                    }
                    else if (fullLoadExporter.HasFinished())
                    {
                        shutdown = true;
                    }

                    Thread.Sleep(500);
                }
#endif

                Console.WriteLine("Received signal gracefully shutting down");
                cts.Cancel();
                fullLoadExporter.WaitForCompletion();
                ended.Set();
            }
            else if (runMode == RunMode.NonTransactionalCdc)
            {
                var interval        = GetInterval(configuration);
                var cdcReaderClient = new CdcReaderClient(configuration["DatabaseConnection"], configuration["StateManagmentConnection"]);
                var cdcExporter     = new ChangeExporter(cdcReaderClient, redshiftClient);
                cdcExporter.StartExportingChangesAsync(cts.Token, executionId, tables, interval, batchSize).Wait();
                Console.WriteLine("Export to Redshift in progress.");

                // wait for shutdown signal
#if DEBUG
                Console.WriteLine("Press any key to shutdown");
                Console.ReadKey();
#else
                starting.Wait();
#endif

                Console.WriteLine("Received signal gracefully shutting down");
                cts.Cancel();
                cdcExporter.WaitForCompletion();
                ended.Set();
            }
            else
            {
                var interval            = GetInterval(configuration);
                var cdcReaderClient     = new CdcTransactionClient(configuration["DatabaseConnection"], configuration["StateManagmentConnection"]);
                var cdcExporter         = new TransactionExporter(cdcReaderClient, redshiftClient);
                var perTableBufferLimit = GetPerTableBufferLimit(configuration);
                var tranBufferLimit     = GetTransactionBufferLimit(configuration);
                var tranBatchSizeLimit  = GetTransactionBatchSizeLimit(configuration);

                cdcExporter.StartExportingChangesAsync(cts.Token, executionId, tables, interval, perTableBufferLimit, tranBufferLimit, tranBatchSizeLimit).Wait();
                Console.WriteLine("Export to Redshift in progress.");

                // wait for shutdown signal
#if DEBUG
                Console.WriteLine("Press any key to shutdown");
                Console.ReadKey();
#else
                starting.Wait();
#endif

                Console.WriteLine("Received signal gracefully shutting down");
                cts.Cancel();
                cdcExporter.WaitForCompletion();
                ended.Set();
            }
        }
 public TransactionExporter(CdcTransactionClient cdcTransactionClient,
                            RedshiftClient redshiftClient)
 {
     _cdcTransactionClient = cdcTransactionClient;
     _redshiftClient       = redshiftClient;
 }