Add kill_ci_runner to ci_utils, will allow restarts

This commit is contained in:
Mikhail f. Shiryaev 2024-09-12 15:24:25 +02:00
parent cb1b7dc43c
commit 99ede620be
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4

View File

@ -1,4 +1,5 @@
import json
import logging
import os
import re
import subprocess
@ -6,10 +7,12 @@ import sys
import time
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Iterator, List, Union, Optional, Sequence
from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple, Union
import requests
logger = logging.getLogger(__name__)
class Envs:
GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY", "ClickHouse/ClickHouse")
@ -36,6 +39,34 @@ def cd(path: Union[Path, str]) -> Iterator[None]:
os.chdir(oldpwd)
def kill_ci_runner(message: str) -> None:
"""The function to kill the current process with all parents when it's possible.
Works only when run with the set `CI` environment"""
if not os.getenv("CI", ""): # cycle import env_helper
logger.info("Running outside the CI, won't kill the runner")
return
print(f"::error::{message}")
def get_ppid_name(pid: int) -> Tuple[int, str]:
# Avoid using psutil, it's not in stdlib
stats = Path(f"/proc/{pid}/stat").read_text(encoding="utf-8").split()
return int(stats[3]), stats[1]
pid = os.getpid()
pids = {} # type: Dict[str, str]
while pid:
ppid, name = get_ppid_name(pid)
pids[str(pid)] = name
pid = ppid
logger.error(
"Sleeping 5 seconds and killing all possible processes from following:\n %s",
"\n ".join(f"{p}: {n}" for p, n in pids.items()),
)
time.sleep(5)
# The current process will be killed too
subprocess.run(f"kill -9 {' '.join(pids.keys())}", check=False, shell=True)
class GH:
class ActionsNames:
RunConfig = "RunConfig"