diff --git a/docs/lab-builds.md b/docs/lab-builds.md index 8d2afd8d6..ef0a40d98 100644 --- a/docs/lab-builds.md +++ b/docs/lab-builds.md @@ -11,6 +11,10 @@ change. and `mergeDexRelease`), packs the linked resources and dex files into `overlay.apk`, writes `overlay.json`, and stages both in the app's `files/lab/next`. It then recompiles the app without an app image and restarts DroidDeck. +- Run by the app's own agent (`DD_AGENT` set), it writes `files/agent/lab-request` instead, and the + app runs the build in a process of its own, logging to `~/.cache/dd-lab-build.log`; the agent's + copy follows that log and exits with the build. A turn's processes end with the turn, so a build + started there would otherwise die when the agent stops or finishes early. - At start, `LabOverlay` (called from `App.attachBaseContext`) promotes `next` to `active`, checks that the build was made for this install, puts its dex files ahead of the APK's, and adds its resources through `ResourcesLoader` to the app and every activity. Contexts made from them diff --git a/tools/agent/dd-lab-build b/tools/agent/dd-lab-build index c3aed3332..8ab92102e 100755 --- a/tools/agent/dd-lab-build +++ b/tools/agent/dd-lab-build @@ -28,6 +28,33 @@ trap 'progress failed 0 stopped; exit 143' INT TERM trap 'progress failed 0 "a command failed on line $LINENO"' ERR device() { adb -s "$serial" shell "$@" | tr -d '\r'; } +# The app's agent loses every process it started when its turn ends, a build included, so it asks +# the app to run the build in a process of its own and follows that build here. +agent=/data/user/0/$pkg/files/agent +if [ -n "${DD_AGENT:-}" ] && [ -z "${DD_LAB_DETACHED:-}" ] && [ -d "$agent" ]; then + trap - ERR + log=$cache/dd-lab-build.log + rm -f "$progress_file" + python3 -c 'import json,os,sys; print(json.dumps({"cwd": os.getcwd(), "args": sys.argv[1:]}))' "$@" > "$agent/lab-request.part" + mv "$agent/lab-request.part" "$agent/lab-request" + echo "DroidDeck runs this build itself, so it keeps going if this turn ends. Its log follows." + for _ in $(seq 1 30); do [ -f "$progress_file" ] && break; sleep 1; done + [ -f "$progress_file" ] || { echo "dd-lab-build: DroidDeck did not start the build; is it running?" >&2; exit 1; } + tail -n +1 -F "$log" 2>/dev/null & follow=$! + result=1 + while sleep 2; do + stage=$(sed -n 's/.*"stage":"\([a-z]*\)".*/\1/p' "$progress_file") + pid=$(sed -n 's/.*"pid":\([0-9]*\).*/\1/p' "$progress_file") + case $stage in + ready|prepared) result=0; break ;; + failed) break ;; + esac + [ -n "$pid" ] && [ -d "/proc/$pid" ] || break + done + sleep 1; kill $follow 2>/dev/null || true + exit $result +fi + progress checking repo=$(git rev-parse --show-toplevel) || fail "run it inside the DroidDeck checkout" cd "$repo"