public static bool ProcessesContainsOnlySelf(this ILockingProcessManager processManager)
 {
     if (processManager is null)
     {
         throw new ArgumentNullException(nameof(processManager));
     }
     return(processManager.GetProcesses().Count() <= 1 && processManager.ProcessesContainsSelf());
 }
Exemplo n.º 2
0
        protected override async Task <PendingHandledResult> HandleLockedComponentsCoreAsync(
            ICollection <IComponent> pendingComponents, ILockingProcessManager lockingProcessManager,
            bool ignoreSelfLocked, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            if (!pendingComponents.Any())
            {
                return(new PendingHandledResult(HandlePendingComponentsStatus.Handled));
            }

            Logger.Trace("Hanlde restart request due to locked files");

            var processes     = lockingProcessManager.GetProcesses().ToList();
            var isSelfLocking = lockingProcessManager.ProcessesContainsSelf();

            if (!isSelfLocking && processes.Any(x => x.ApplicationType == TaskBasedUpdater.Restart.ApplicationType.Critical))
            {
                return(new PendingHandledResult(HandlePendingComponentsStatus.Declined,
                                                "Files are locked by a system process that cannot be terminated. Please restart the system"));
            }

            using var lockingProcessManagerWithoutSelf = CreateFromProcessesWithoutSelf(processes);

            if (!isSelfLocking || ignoreSelfLocked)
            {
                var restartRequestResult =
                    LauncherRestartManager.ShowProcessKillDialog(lockingProcessManagerWithoutSelf, token);
                Logger.Trace($"Kill locking processes: {restartRequestResult}, Launcher needs restart: {false}");
                if (!restartRequestResult)
                {
                    return(new PendingHandledResult(HandlePendingComponentsStatus.Declined,
                                                    "Update aborted because locked files have not been released."));
                }

                lockingProcessManagerWithoutSelf.Shutdown();
                LockedFilesWatcher.Instance.LockedFiles.Clear();
                await UpdateAsync(pendingComponents, token).ConfigureAwait(false);

                return(LockedFilesWatcher.Instance.LockedFiles.Any()
                    ? new PendingHandledResult(HandlePendingComponentsStatus.HandledButStillPending,
                                               "Update failed because there are still locked files which have not been released.")
                    : new PendingHandledResult(HandlePendingComponentsStatus.Handled));
            }

            if (!UpdateConfiguration.Instance.SupportsRestart)
            {
                return(new PendingHandledResult(HandlePendingComponentsStatus.Declined,
                                                "Update requires a self-update which is not supported for this update configuration."));
            }

            var result = LauncherRestartManager.ShowSelfKillDialog(lockingProcessManager, token);

            Logger.Trace($"Kill locking processes: {result}, Launcher needs restart: {true}");
            if (!result)
            {
                return(new PendingHandledResult(HandlePendingComponentsStatus.Declined,
                                                "Update aborted because locked files have not been released."));
            }

            lockingProcessManagerWithoutSelf.Shutdown();
            return(new PendingHandledResult(HandlePendingComponentsStatus.Restart));
        }