Skip to content

Commit fe33da2

Browse files
DRIVERS-3298 Improve stop-orchestration behavior (#694)
Co-authored-by: Ezra Chung <[email protected]>
1 parent fca0ac5 commit fe33da2

File tree

3 files changed

+110
-25
lines changed

3 files changed

+110
-25
lines changed

.evergreen/orchestration/drivers_orchestration.py

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import urllib.error
2121
import urllib.request
2222
from datetime import datetime
23-
from pathlib import Path
23+
from pathlib import Path, PureWindowsPath
24+
25+
import psutil
2426

2527
# Get global values.
2628
HERE = Path(__file__).absolute().parent
@@ -196,6 +198,7 @@ def get_docker_cmd():
196198
docker = shutil.which("podman") or shutil.which("docker")
197199
if not docker:
198200
return None
201+
docker = PureWindowsPath(docker).as_posix()
199202
if "podman" in docker:
200203
docker = f"sudo {docker}"
201204
return docker
@@ -543,7 +546,10 @@ def clean_start(opts):
543546
"server.pid",
544547
]:
545548
if (mo_home / fname).exists():
546-
(mo_home / fname).unlink()
549+
try:
550+
(mo_home / fname).unlink()
551+
except PermissionError:
552+
pass
547553

548554

549555
def start(opts):
@@ -635,36 +641,93 @@ def start(opts):
635641
LOGGER.info("Starting mongo-orchestration... done.")
636642

637643

644+
def shutdown_proc(proc: psutil.Process) -> None:
645+
try:
646+
proc.terminate()
647+
try:
648+
proc.wait(10) # Wait up to 10 seconds.
649+
except psutil.TimeoutExpired:
650+
proc.kill()
651+
except Exception as e:
652+
LOGGER.exception(e)
653+
654+
655+
def shutdown_docker(docker: str, container_id: str) -> None:
656+
if "podman" in docker:
657+
cmd = f"{docker} rm -f {container_id}"
658+
else:
659+
cmd = f"{docker} kill {container_id}"
660+
run_command(cmd, exit_on_error=False)
661+
662+
638663
def stop(opts):
639664
mo_home = Path(opts.mongo_orchestration_home)
640665
pid_file = mo_home / "server.pid"
641666
container_file = mo_home / "container_id.txt"
642667
docker = get_docker_cmd()
668+
669+
# First try to shut down using pid file.
643670
if pid_file.exists():
644-
LOGGER.info("Stopping mongo-orchestration...")
645-
py_exe = normalize_path(sys.executable)
646-
run_command(f"{py_exe} -m mongo_orchestration.server stop")
671+
pid = int(pid_file.read_text().strip())
647672
pid_file.unlink(missing_ok=True)
648-
LOGGER.info("Stopping mongo-orchestration... done.")
649-
if container_file.exists():
650-
LOGGER.info("Stopping mongodb_atlas_local...")
651-
container_id = container_file.read_text()
652-
run_command(f"{docker} kill {container_id}", exit_on_error=False)
673+
if psutil.pid_exists(pid):
674+
LOGGER.info("Stopping mongo-orchestration using pid file...")
675+
shutdown_proc(psutil.Process(pid))
676+
LOGGER.info("Stopping mongo-orchestration using pid file... done.")
677+
678+
# Next try using a docker container file.
679+
if docker is not None and container_file.exists():
680+
LOGGER.info("Stopping mongodb_atlas_local using container file...")
681+
shutdown_docker(docker, container_file.read_text())
653682
container_file.unlink()
654-
LOGGER.info("Stopping mongodb_atlas_local... done.")
655-
elif docker:
656-
cmd = f"{docker} ps -a -q -f name=mongodb_atlas_local"
683+
LOGGER.info("Stopping mongodb_atlas_local using container file ... done.")
684+
685+
all_procs = list(psutil.process_iter())
686+
687+
# Next look for mongo-orchestration by command line arguments.
688+
for proc in all_procs:
657689
try:
658-
result = subprocess.check_output(shlex.split(cmd), encoding="utf-8").strip()
659-
except Exception:
660-
result = None
661-
if result:
662-
LOGGER.info("Stopping mongodb_atlas_local...")
663-
if "podman" in docker:
664-
run_command(f"{docker} rm -f {result}")
665-
else:
666-
run_command(f"{docker} kill {result}")
667-
LOGGER.info("Stopping mongodb_atlas_local... done.")
690+
cmdline = proc.cmdline()
691+
except (psutil.AccessDenied, psutil.NoSuchProcess):
692+
continue
693+
found = False
694+
for item in cmdline:
695+
if "mongo_orchestration.server" in item or "mongo-orchestration" in item:
696+
found = True
697+
break
698+
if not found:
699+
continue
700+
LOGGER.info("Stopping mongo-orchestration by process info...")
701+
shutdown_proc(proc)
702+
LOGGER.info("Stopping mongo-orchestration by process info... done.")
703+
704+
# Next look for running docker images.
705+
if docker:
706+
cmd = f"{docker} ps --format '{{{{.Image}}}}\t{{{{.ID}}}}'"
707+
try:
708+
response = subprocess.check_output(
709+
shlex.split(cmd), encoding="utf-8"
710+
).strip()
711+
except subprocess.CalledProcessError as e:
712+
LOGGER.exception(e)
713+
response = ""
714+
for line in response.splitlines():
715+
image, container_id = line.split("\t")
716+
if image in ["mongodb/mongodb-atlas-local", "mongo"]:
717+
LOGGER.info(f"Stopping {image} by image name...")
718+
shutdown_docker(docker, container_id)
719+
LOGGER.info(f"Stopping {image} by image name... done.")
720+
721+
# Finally, look for any processes that are named mongod or mongos.
722+
for proc in all_procs:
723+
try:
724+
name = proc.name()
725+
except psutil.NoSuchProcess:
726+
continue
727+
if name in ["mongod", "mongos"]:
728+
LOGGER.info(f"Stopping {name} by process name...")
729+
shutdown_proc(proc)
730+
LOGGER.info(f"Stopping {name} by process name... done.")
668731

669732

670733
def main():

.evergreen/orchestration/pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ classifiers = [
1717
"Programming Language :: Python",
1818
"Programming Language :: Python :: Implementation :: CPython",
1919
]
20-
dependencies = ["drivers-evergreen-tools @ {root:parent:uri}", "mongo-orchestration @ https://github.com/mongodb/mongo-orchestration/archive/master.tar.gz"]
20+
dependencies = [
21+
"drivers-evergreen-tools @ {root:parent:uri}",
22+
"mongo-orchestration @ https://github.com/mongodb/mongo-orchestration/archive/master.tar.gz",
23+
"psutil>=7.1"
24+
]
2125

2226
[project.scripts]
2327
drivers-orchestration = "drivers_orchestration:main"

.evergreen/orchestration/uv.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)