Ejemplo n.º 1
0
        public static void EnsureSqlStreamStoreSchema <T>(MsSqlStreamStore streamStore, ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger <T>();

            // TODO: Need to revisit this with a Consul lock
            Policy
            .Handle <SqlException>()
            .WaitAndRetry(
                5,
                retryAttempt =>
            {
                var value       = Math.Pow(2, retryAttempt) / 4;
                var randomValue = new Random().Next((int)value * 3, (int)value * 5);
                logger.LogInformation("Retrying after {Seconds} seconds...", randomValue);
                return(TimeSpan.FromSeconds(randomValue));
            })
            .Execute(() =>
            {
                logger.LogInformation("Ensuring the sql stream store schema.");

                var checkSchemaResult = streamStore.CheckSchema().GetAwaiter().GetResult();
                if (!checkSchemaResult.IsMatch())
                {
                    streamStore.CreateSchema().GetAwaiter().GetResult();
                }
            });
        }
Ejemplo n.º 2
0
        private void SetupStreamStore(IServiceCollection services)
        {
            var streamStore = new MsSqlStreamStore(_msSqlStreamStoreSettings);

            services.AddSingleton <IStreamStore>(x => streamStore);
            var schema = streamStore.CheckSchema().GetAwaiter().GetResult();

            if (!schema.IsMatch())
            {
                streamStore.CreateSchema();
            }
        }
        private static async Task CreateDatabase(string connectionString, CancellationToken ct)
        {
            // There is a while true loop here, because sometimes after creating the db, the db is not available always.
            // There are much better ways of handling this, but I couldn't be bothered right now;)
            while (true)
            {
                try
                {
                    await Task.Delay(1000);

                    Console.WriteLine("Creating database schema");
                    await Task.Delay(3000);

                    var mgr = new DatabaseManager(connectionString);
                    mgr.EnsureDatabaseExists(10, 10);

                    await Task.Delay(2000, ct);

                    var msSqlStreamStoreSettings = new MsSqlStreamStoreSettings(connectionString);

                    var store = new MsSqlStreamStore(msSqlStreamStoreSettings);
                    if (!(await store.CheckSchema(ct)).IsMatch())
                    {
                        await store.CreateSchema(ct);
                    }

                    var repo = new StreamStoreConfigRepository(store);

                    Console.WriteLine("Now generating 20 changes:");

                    for (int i = 0; i < 20; i++)
                    {
                        await Task.Delay(1000, ct);

                        await repo.Modify(ct, ("setting1", "new value, written at: " + DateTime.Now.ToLongTimeString()));
                    }

                    // Delay for a while, so the latest version can be printed.
                    await Task.Delay(1000, ct);

                    var history = await repo.GetSettingsHistory(ct);

                    Console.WriteLine($"There have historically been:{history.Count} versions: ");
                    foreach (var setting in history)
                    {
                        Console.WriteLine($"\t- Version: {setting.Version}, setting1 = '{setting["setting1"]}', ModifiedKeys: [{string.Join(',', setting.ModifiedKeys)}]");
                    }

                    Console.WriteLine($"Now reverting back to the first version's data: {history.First().Version}");

                    // Now revert back to the first version (this actually creates a new version, with the old data in it)
                    await repo.RevertToVersion(history.First(), ct);


                    // Completed succesfully. End the function
                    return;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("error while creating database: " + ex.Message + " (will retry)");
                }
            }
        }
Ejemplo n.º 4
0
 public async Task Ping(CancellationToken cancellationToken = default)
 {
     var eventStore = new MsSqlStreamStore(settings);
     await eventStore.CheckSchema(cancellationToken);
 }