chocolatey / chocolatey/choco

Mechanism to gracefully shutdown application before updating library

Open
#2,193 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
11.5k
Forks
960
PR merge metrics
No merged PRs in 30d

Description

For software update perspective I have added mechanism to gracefully shutdown application before starting to update.
This technique can be used for choco API user, who actually wants to perform software update - but in similar manner this could be used as well for choco itself. Use example such that application, which user wants to upgrade is up-and-running and choco gets command to upgrade that application.

Grateful application closure could be done automatically and gracefully using windows reboot mechanism. (Or non automatically, maybe could be configured ?)

Windows itself send message `WM_QUERYENDSESSION` with subsequent `WM_ENDSESSION` to an application before windows is going to restart.

In similar manner choco could send same messages to application and gracefully close it.

Not all applications does support these message pair, but more serious application do support it.

In a turn we also need a mechanism of detecting what application is performing locking and which files.

https://github.com/Walkman100/FileLocks.git

File lock detection could be done using for example this library, using this API: https://github.com/Walkman100/FileLocks/blob/master/RestartManager.cs#L316

API itself could look like this:

```
using System.Collections.Generic;
using System.Diagnostics;

namespace chocolatey.infrastructure.app.services
{
public interface IRestartManager
{
///
/// Queries whether it's possible to install into particular directory.
///
/// where install will be performed
/// .exe names which can be auto-terminated
/// true whether locking by myself is allowed or not
/// If false is returned - list of processes to kill (shown to end-user)
/// List of processes, which create lock
/// true if can proceed with installation, false if not
bool can_install_into(string dir, bool selfLockAllowed, ref string lockingProcessesStr, ref List lockingProcesses, params string[] can_autokill_processes);

///
/// Attempts to gracefully close processes
///
/// processes to close
/// true if can proceed (all processes closed), false if not all processes agreed with closure
bool graceful_close_processes(IEnumerable processes);
}
}
```

Here are some hints on implementation and testing of graceful application shutdown:

https://stackoverflow.com/a/65392006/2338477

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the proposed IRestartManager interface, the referenced RestartManager.cs API, and the Stack Overflow guidance on graceful shutdown. Establish the repository entry points, installation flow, and tests affected before defining the scope; done would require an agreed design for process shutdown and file-lock detection, plus coverage for the supported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
cli, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.