Ejemplo n.º 1
0
    public Task HandleAsync(RuntimeUnattendedUpgradeNotification notification, CancellationToken cancellationToken)
    {
        if (_runtimeState.RunUnattendedBootLogic())
        {
            switch (_runtimeState.Reason)
            {
            case RuntimeLevelReason.UpgradeMigrations:
            {
                var plan = new UmbracoPlan(_umbracoVersion);
                using (_profilingLogger.TraceDuration <UnattendedUpgrader>(
                           "Starting unattended upgrade.",
                           "Unattended upgrade completed."))
                {
                    DatabaseBuilder.Result?result = _databaseBuilder.UpgradeSchemaAndData(plan);
                    if (result?.Success == false)
                    {
                        var innerException = new UnattendedInstallException(
                            "An error occurred while running the unattended upgrade.\n" + result.Message);
                        _runtimeState.Configure(RuntimeLevel.BootFailed, RuntimeLevelReason.BootFailedOnException, innerException);
                    }

                    notification.UnattendedUpgradeResult =
                        RuntimeUnattendedUpgradeNotification.UpgradeResult.CoreUpgradeComplete;
                }
            }

            break;

            case RuntimeLevelReason.UpgradePackageMigrations:
            {
                if (!_runtimeState.StartupState.TryGetValue(
                        RuntimeState.PendingPackageMigrationsStateKey,
                        out var pm) ||
                    pm is not IReadOnlyList <string> pendingMigrations)
                {
                    throw new InvalidOperationException(
                              $"The required key {RuntimeState.PendingPackageMigrationsStateKey} does not exist in startup state");
                }

                if (pendingMigrations.Count == 0)
                {
                    throw new InvalidOperationException(
                              "No pending migrations found but the runtime level reason is " +
                              RuntimeLevelReason.UpgradePackageMigrations);
                }

                try
                {
                    _packageMigrationRunner.RunPackagePlans(pendingMigrations);
                    notification.UnattendedUpgradeResult = RuntimeUnattendedUpgradeNotification.UpgradeResult
                                                           .PackageMigrationComplete;
                }
                catch (Exception ex)
                {
                    SetRuntimeError(ex);
                    notification.UnattendedUpgradeResult =
                        RuntimeUnattendedUpgradeNotification.UpgradeResult.HasErrors;
                }
            }

            break;

            default:
                throw new InvalidOperationException("Invalid reason " + _runtimeState.Reason);
            }
        }

        return(Task.CompletedTask);
    }