databricks-cli/acceptance/bin/wait_pid.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.6 KiB
Python
Raw Normal View History

2025-02-28 11:49:29 +00:00
#!/usr/bin/env python3
import sys
import os
import time
import platform
import subprocess
2025-03-02 16:06:24 +00:00
2025-03-02 15:48:01 +00:00
def is_finished_windows(pid):
assert int(pid) > 0
# Run task list but filter the list by pid..
2025-03-02 16:06:24 +00:00
output = subprocess.check_output(f'tasklist /fi "PID eq {pid}"', text=True)
2025-03-02 15:48:01 +00:00
# If tasklist does not find any process, that means the process we are
# waiting for has terminated.
unaliveMsg = "No tasks are running which match the specified criteria."
if unaliveMsg in output:
return True
return False
2025-03-02 16:06:24 +00:00
2025-03-02 15:48:01 +00:00
def is_finished_unix(pid):
assert int(pid) > 0
# Send signal 0 to the process to check if it exists.
# From the docs:
# If sig is 0, then no signal is sent, but existence and permission
# checks are still performed; this can be used to check for the
# existence of a process ID or process group ID that the caller is
# permitted to signal.
# ref: https://man7.org/linux/man-pages/man2/kill.2.html
try:
os.kill(pid, 0)
except OSError:
return True
return False
2025-02-28 11:57:12 +00:00
2025-02-28 11:49:29 +00:00
def wait_pid(pid):
2025-02-28 11:57:12 +00:00
max_attempts = 600 # 600 * 0.1 seconds = 1 minute
2025-02-28 11:49:29 +00:00
sleep_time = 0.1
for i in range(max_attempts):
if platform.system().lower() == "windows":
2025-03-02 15:48:01 +00:00
if is_finished_windows(pid):
2025-02-28 11:49:29 +00:00
print("[wait_pid] process has ended")
return 0
2025-03-02 15:48:01 +00:00
2025-02-28 11:49:29 +00:00
else:
2025-03-02 15:48:01 +00:00
if is_finished_unix(pid):
2025-02-28 11:49:29 +00:00
print("[wait_pid] process has ended")
return 0
time.sleep(sleep_time)
print(f"Timeout: Process {pid} did not end within 1 minute")
return 1
2025-03-02 16:06:24 +00:00
2025-03-02 15:48:01 +00:00
exit_code = wait_pid(int(sys.argv[1]))
2025-02-28 11:49:29 +00:00
sys.exit(exit_code)