move wait_pid to python

This commit is contained in:
Shreyas Goenka 2025-02-28 12:49:29 +01:00
parent fd271d33f8
commit fec9c4efae
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
7 changed files with 54 additions and 46 deletions

View File

@ -1,41 +0,0 @@
#!/bin/bash
# wait <pid> in bash only works for processes that are direct children to the calling
# shell. This script is more general purpose.
wait_pid() {
local pid=$1
local max_attempts=600 # 600 * 0.1 seconds = 1 Minute
local attempt=0
local sleep_time=0.1
while [ $attempt -lt $max_attempts ]; do
if [[ "$OSTYPE" == "msys"* || "$OSTYPE" == "cygwin"* ]]; then
# Windows approach
if ! tasklist | grep -q $pid; then
echo "[wait_pid] process has ended"
return 0
fi
else
# Linux/macOS approach
if ! kill -0 $pid 2>/dev/null; then
echo "[wait_pid] process has ended"
return 0
fi
fi
sleep $sleep_time
attempt=$((attempt + 1))
done
echo "Timeout: Process $pid did not end within 1 minute"
return 1
}
# Usage
if [ $# -eq 0 ]; then
echo "Usage: $0 <PID>"
exit 1
fi
wait_pid $1
exit $?

49
acceptance/bin/wait_pid.py Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env python3
import sys
import os
import time
import platform
import subprocess
# TODO CONTINUE: Making this script work and testing this on windows and mac.
def wait_pid(pid):
max_attempts = 600 # 600 * 0.1 seconds = 1 minute
sleep_time = 0.1
for i in range(max_attempts):
# Check if we are on Windows or a Unix system.
if platform.system().lower() == "windows":
# Windows approach: use tasklist to check for the existence of the process.
try:
# Get the output of 'tasklist'
output = subprocess.check_output(['tasklist'], text=True)
except subprocess.CalledProcessError:
print("[wait_pid] Error retrieving tasklist. Assuming process has ended.")
return 0
# if the PID is not found in the list then assume the process ended.
if str(pid) not in output:
print("[wait_pid] process has ended")
return 0
else:
# Linux/macOS approach: using os.kill with signal 0 to check if the process exists.
try:
os.kill(pid, 0)
except OSError:
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
try:
pid = int(sys.argv[1])
except ValueError:
print("Error: PID must be an integer.")
sys.exit(1)
exit_code = wait_pid(pid)
sys.exit(exit_code)

View File

@ -6,7 +6,7 @@ export DATABRICKS_CLI_TELEMETRY_UPLOAD_LOGS_FILE=./out.upload_process.txt
trace $CLI selftest send-telemetry
# Wait for the child telemetry process to finish
wait_pid $(cat ./telemetry.pid)
wait_pid.py $(cat ./telemetry.pid)
# cleanup the pid file
rm -f ./telemetry.pid

View File

@ -6,7 +6,7 @@ trace $CLI selftest send-telemetry
echo "waiting for telemetry process to finish"
# Wait for the child telemetry process to finish
wait_pid $(cat ./telemetry.pid)
wait_pid.py $(cat ./telemetry.pid)
# cleanup the pid file
rm -f ./telemetry.pid

View File

@ -13,7 +13,7 @@ trace $CLI selftest send-telemetry
echo "waiting for telemetry process to finish"
# Wait for the child telemetry process to finish
wait_pid $(cat ./telemetry.pid)
wait_pid.py $(cat ./telemetry.pid)
# cleanup the pid file
rm -f ./telemetry.pid

View File

@ -6,7 +6,7 @@ trace $CLI selftest send-telemetry
echo "waiting for telemetry process to finish"
# Wait for the child telemetry process to finish
wait_pid $(cat ./telemetry.pid)
wait_pid.py $(cat ./telemetry.pid)
# cleanup the pid file
rm -f ./telemetry.pid

View File

@ -10,7 +10,7 @@ trace $CLI selftest send-telemetry
echo "waiting for telemetry process to finish"
# Wait for the child telemetry process to finish
wait_pid $(cat ./telemetry.pid)
wait_pid.py $(cat ./telemetry.pid)
# cleanup the pid file
rm -f ./telemetry.pid