Ejemplo n.º 1
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();
            }
        }
Ejemplo n.º 2
0
        public async Task MigrateAsync(CancellationToken ct = default)
        {
            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);
                }

                var 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.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);
                }
            }
            finally
            {
                await migrationStatus.UnlockAsync();
            }
        }