示例#1
0
        public async Task MigrateAsync()
        {
            var version = 0;

            try
            {
                while (!await migrationStatus.TryLockAsync())
                {
                    log.LogInformation(w => w
                                       .WriteProperty("action", "Migrate")
                                       .WriteProperty("mesage", $"Waiting {LockWaitMs}ms to acquire lock."));

                    await Task.Delay(LockWaitMs);
                }

                version = await migrationStatus.GetVersionAsync();

                while (true)
                {
                    var migrationStep = migrationPath.GetNext(version);

                    if (migrationStep.Migrations == null || !migrationStep.Migrations.Any())
                    {
                        break;
                    }

                    foreach (var migration in migrationStep.Migrations)
                    {
                        var name = migration.GetType().ToString();

                        log.LogInformation(w => w
                                           .WriteProperty("action", "Migration")
                                           .WriteProperty("status", "Started")
                                           .WriteProperty("migrator", name));

                        using (log.MeasureInformation(w => w
                                                      .WriteProperty("action", "Migration")
                                                      .WriteProperty("status", "Completed")
                                                      .WriteProperty("migrator", name)))
                        {
                            await migration.UpdateAsync();
                        }
                    }

                    version = migrationStep.Version;
                }
            }
            finally
            {
                await migrationStatus.UnlockAsync(version);
            }
        }
示例#2
0
        public async Task MigrateAsync(
            CancellationToken ct = default)
        {
            if (!await TryLockAsync(ct))
            {
                return;
            }

            try
            {
                var version = await migrationStatus.GetVersionAsync(ct);

                while (!ct.IsCancellationRequested)
                {
                    var(newVersion, migrations) = migrationPath.GetNext(version);

                    if (migrations == null || !migrations.Any())
                    {
                        break;
                    }

                    foreach (var migration in migrations)
                    {
                        var name = migration.ToString() !;

                        log.LogInformation("Migration {migration} started.", name);

                        try
                        {
                            var watch = ValueStopwatch.StartNew();

                            await migration.UpdateAsync(ct);

                            log.LogInformation("Migration {migration} completed after {time}ms.", name, watch.Stop());
                        }
                        catch (Exception ex)
                        {
                            log.LogCritical(ex, "Migration {migration} failed.", name);
                            throw new MigrationFailedException(name, ex);
                        }
                    }

                    version = newVersion;

                    await migrationStatus.CompleteAsync(newVersion, ct);
                }
            }
            finally
            {
                await UnlockAsync();
            }
        }
示例#3
0
        public async Task MigrateAsync(CancellationToken ct = default)
        {
            var version = 0;

            try
            {
                while (!await migrationStatus.TryLockAsync())
                {
                    log.LogInformation(w => w
                                       .WriteProperty("action", "Migrate")
                                       .WriteProperty("mesage", $"Waiting {LockWaitMs}ms to acquire lock."));

                    await Task.Delay(LockWaitMs, ct);
                }

                version = await migrationStatus.GetVersionAsync();

                while (!ct.IsCancellationRequested)
                {
                    var(newVersion, migrations) = migrationPath.GetNext(version);

                    if (migrations == null || !migrations.Any())
                    {
                        break;
                    }

                    foreach (var migration in migrations)
                    {
                        var name = migration.GetType().ToString();

                        log.LogInformation(w => w
                                           .WriteProperty("action", "Migration")
                                           .WriteProperty("status", "Started")
                                           .WriteProperty("migrator", name));

                        try
                        {
                            using (log.MeasureInformation(w => w
                                                          .WriteProperty("action", "Migration")
                                                          .WriteProperty("status", "Completed")
                                                          .WriteProperty("migrator", name)))
                            {
                                await migration.UpdateAsync();
                            }
                        }
                        catch (Exception ex)
                        {
                            log.LogFatal(ex, w => w
                                         .WriteProperty("action", "Migration")
                                         .WriteProperty("status", "Failed")
                                         .WriteProperty("migrator", name));

                            throw new MigrationFailedException(name, ex);
                        }
                    }

                    version = newVersion;
                }
            }
            finally
            {
                await migrationStatus.UnlockAsync(version);
            }
        }
示例#4
0
        public async Task MigrateAsync(
            CancellationToken ct = default)
        {
            if (!await TryLockAsync(ct))
            {
                return;
            }

            try
            {
                var version = await migrationStatus.GetVersionAsync(ct);

                while (!ct.IsCancellationRequested)
                {
                    var(newVersion, migrations) = migrationPath.GetNext(version);

                    if (migrations == null || !migrations.Any())
                    {
                        break;
                    }

                    foreach (var migration in migrations)
                    {
                        var name = migration.ToString() !;

                        log.LogInformation(w => w
                                           .WriteProperty("action", "Migration")
                                           .WriteProperty("status", "Started")
                                           .WriteProperty("migrator", name));

                        try
                        {
                            using (log.MeasureInformation(w => w
                                                          .WriteProperty("action", "Migration")
                                                          .WriteProperty("status", "Completed")
                                                          .WriteProperty("migrator", name)))
                            {
                                await migration.UpdateAsync(ct);
                            }
                        }
                        catch (Exception ex)
                        {
                            log.LogFatal(ex, w => w
                                         .WriteProperty("action", "Migration")
                                         .WriteProperty("status", "Failed")
                                         .WriteProperty("migrator", name));

                            throw new MigrationFailedException(name, ex);
                        }
                    }

                    version = newVersion;

                    await migrationStatus.CompleteAsync(newVersion, ct);
                }
            }
            finally
            {
                await UnlockAsync();
            }
        }