add bash script for waiting

This commit is contained in:
Shreyas Goenka 2025-02-04 15:34:25 +01:00
parent 0423b09733
commit 981dbf787d
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
11 changed files with 95 additions and 11 deletions

View File

@ -36,7 +36,7 @@ var (
// In order to debug CLI running under acceptance test, set this to full subtest name, e.g. "bundle/variables/empty"
// Then install your breakpoints and click "debug test" near TestAccept in VSCODE.
// example: var SingleTest = "bundle/variables/empty"
var SingleTest = ""
var SingleTest = "telemetry/dummy-without-server"
// If enabled, instead of compiling and running CLI externally, we'll start in-process server that accepts and runs
// CLI commands. The $CLI in test scripts is a helper that just forwards command-line arguments to this server (see bin/callserver.py).
@ -44,7 +44,8 @@ var SingleTest = ""
var InprocessMode bool
func init() {
flag.BoolVar(&InprocessMode, "inprocess", SingleTest != "", "Run CLI in the same process as test (for debugging)")
// flag.BoolVar(&InprocessMode, "inprocess", SingleTest != "", "Run CLI in the same process as test (for debugging)")
flag.BoolVar(&InprocessMode, "inprocess", false, "Run CLI in the same process as test (for debugging)")
flag.BoolVar(&KeepTmp, "keeptmp", false, "Do not delete TMP directory after run")
flag.BoolVar(&NoRepl, "norepl", false, "Do not apply any replacements (for debugging)")
}

27
acceptance/bin/wait_file Executable file
View File

@ -0,0 +1,27 @@
#!/bin/bash
wait_file() {
local file_path="$1"
local max_attempts=100
local attempt=0
while [ $attempt -lt $max_attempts ]; do
if [ -e "$file_path" ]; then
echo "File $file_path exists"
return 0
fi
sleep 0.1
attempt=$((attempt + 1))
done
echo "Timeout: File $file_path did not appear within 10 seconds"
return 1
}
if [ $# -eq 0 ]; then
echo "Usage: $0 <file_path>"
exit 1
fi
wait_file "$1"
exit $?

41
acceptance/bin/wait_pid Executable file
View File

@ -0,0 +1,41 @@
#!/bin/bash
# wait <pid> in bash only works for child process. This script is more general.
wait_pid() {
local pid=$1
local max_attempts=100 # 100 * 0.1 seconds = 10 seconds
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 "Process has ended"
return 0
fi
else
# Linux/macOS approach
if ! kill -0 $pid 2>/dev/null; then
echo "Process has ended"
return 0
fi
fi
sleep $sleep_time
attempt=$((attempt + 1))
done
echo "Timeout: Process $pid did not end within 10 seconds"
return 1
}
# Usage
if [ $# -eq 0 ]; then
echo "Usage: $0 <PID>"
exit 1
fi
wait_pid $1
exit $?

View File

@ -1 +0,0 @@
error: telemetry endpoint not found

View File

@ -1,2 +1,4 @@
>>> $CLI telemetry dummy
File ./telemetry.pid exists
Process has ended

View File

@ -1,5 +1,9 @@
export DATABRICKS_TELEMETRY_UPLOAD_LOGS_FILE=./out.upload.txt
export DATABRICKS_CLI_TELEMETRY_PID_FILE=./telemetry.pid
# This test ensures that the main CLI command does not error even if
# telemetry upload fails.
trace $CLI telemetry dummy
# Wait for the child telemetry process to finish
trace wait_file ./telemetry.pid
trace wait_pid $(cat ./telemetry.pid)

View File

@ -1,3 +0,0 @@
Successfully uploaded telemetry logs
Response:
{"errors":null,"numProtoSuccess":2}

View File

@ -1,2 +1,4 @@
>>> $CLI telemetry dummy
File ./telemetry.pid exists
Process has ended

View File

@ -1,3 +1,7 @@
export DATABRICKS_TELEMETRY_UPLOAD_LOGS_FILE=./out.upload.txt
export DATABRICKS_CLI_TELEMETRY_PID_FILE=./telemetry.pid
trace $CLI telemetry dummy
# Wait for the child telemetry process to finish
trace wait_file ./telemetry.pid
trace wait_pid $(cat ./telemetry.pid)

View File

@ -1,7 +1,4 @@
RecordRequests = true
EventuallyFiles = [
"out.upload.txt"
]
[[Server]]
Pattern = "POST /telemetry-ext"

View File

@ -10,6 +10,7 @@ import (
"os/exec"
"runtime"
"slices"
"strconv"
"strings"
"time"
@ -164,6 +165,8 @@ func inheritEnvVars() []string {
return out
}
const telemetryPidFileEnvVar = "DATABRICKS_CLI_TELEMETRY_PID_FILE"
func uploadTelemetry(ctx context.Context, cmdStr string, start, end time.Time, exitCode int) {
// Nothing to upload.
if !telemetry.HasLogs(ctx) {
@ -213,6 +216,13 @@ func uploadTelemetry(ctx context.Context, cmdStr string, start, end time.Time, e
return
}
if pidFilePath := env.Get(ctx, telemetryPidFileEnvVar); pidFilePath != "" {
err = os.WriteFile(pidFilePath, []byte(strconv.Itoa(telemetryCmd.Process.Pid)), 0o644)
if err != nil {
log.Debugf(ctx, "failed to write telemetry worker PID file: %s", err)
}
}
_, err = stdin.Write(b)
if err != nil {
log.Debugf(ctx, "failed to write to telemetry worker: %s", err)