about summary refs log tree commit diff
path: root/src/ci/scripts/free-disk-space-windows-wait.py
blob: d510781d534e3b3b873a027f5381e26e3d14a9e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Wait for the background Windows disk cleanup process.
"""

import ctypes
import time
from free_disk_space_windows_util import get_pid_file, get_log_file, run_main


def is_process_running(pid: int) -> bool:
    PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
    processHandle = ctypes.windll.kernel32.OpenProcess(
        PROCESS_QUERY_LIMITED_INFORMATION, 0, pid
    )
    if processHandle == 0:
        # The process is not running.
        # If you don't have the sufficient rights to check if a process is running,
        # zero is also returned. But in GitHub Actions we have these rights.
        return False
    else:
        ctypes.windll.kernel32.CloseHandle(processHandle)
        return True


def print_logs():
    """Print the logs from the cleanup script."""
    log_file = get_log_file()
    if log_file.exists():
        print("free-disk-space logs:")
        # Print entire log; replace undecodable bytes to avoid exceptions.
        try:
            with open(log_file, "r", encoding="utf-8", errors="replace") as f:
                print(f.read())
        except Exception as e:
            raise Exception(f"Failed to read log file '{log_file}'") from e
    else:
        print(f"::warning::Log file '{log_file}' not found")


def read_pid_from_file() -> int:
    """Read the PID from the pid file."""

    pid_file = get_pid_file()
    if not pid_file.exists():
        raise Exception(
            f"No background free-disk-space process to wait for: pid file {pid_file} not found"
        )

    pid_file_content = pid_file.read_text().strip()

    # Delete the file if it exists
    pid_file.unlink(missing_ok=True)

    try:
        # Read the first line and convert to int.
        pid = int(pid_file_content.splitlines()[0])
        return pid
    except Exception as e:
        raise Exception(
            f"Error while parsing the pid file with content '{pid_file_content!r}'"
        ) from e


def wait_for_process(pid: int):
    timeout_duration_seconds = 5 * 60
    interval_seconds = 3
    max_attempts = timeout_duration_seconds / interval_seconds
    attempts = 0

    # Poll until process exits
    while is_process_running(pid):
        if attempts >= max_attempts:
            print(
                "::warning::Timeout expired while waiting for the disk cleanup process to finish."
            )
            break
        time.sleep(interval_seconds)
        attempts += 1


def main() -> int:
    pid = read_pid_from_file()

    wait_for_process(pid)

    print_logs()

    return 0


if __name__ == "__main__":
    run_main(main)