feat: allow custom arguments to procmon callbacks as part of async job

This commit is contained in:
gmega 2025-06-23 16:46:32 -03:00
parent c678ba6de2
commit a0af4c4c90
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
2 changed files with 23 additions and 5 deletions

View File

@ -202,6 +202,7 @@ pm_async() {
-%-)
shift
proc_type="$1"
shift
break
;;
*)
@ -213,7 +214,7 @@ pm_async() {
(
set +e
_pm_job_started "${BASHPID}" "$proc_type"
_pm_job_started "${BASHPID}" "$proc_type" "$@"
trap '_pm_job_exited "${BASHPID}" "$proc_type" "killed"' TERM
trap '_pm_job_exited "${BASHPID}" "$proc_type" "$?"' EXIT
"${command[@]}"
@ -243,11 +244,12 @@ await_all() {
_pm_job_started() {
local pid=$1 proc_type=$2
echoerr "[procmon] job started: $pid ($proc_type)"
shift 2
echoerr "[procmon] job started: $pid ($proc_type), args: $*"
if [ ! -f "${_pm_output}/${pid}.pid" ]; then
touch "${_pm_output}/${pid}.pid"
fi
_pm_invoke_callback "start" "$proc_type" "$pid"
_pm_invoke_callback "start" "$proc_type" "$pid" "$@"
}
_pm_job_exited() {
@ -272,11 +274,12 @@ pm_register_callback() {
}
_pm_invoke_callback() {
local event="$1" proc_type="$2" pid="$3" exit_code="$4"
local event="$1" proc_type="$2" pid="$3"
shift 3
if [ -n "$proc_type" ]; then
# calls the callback for this proc type
if [ -n "${_pm_callbacks[$proc_type]}" ]; then
"${_pm_callbacks[$proc_type]}" "$event" "$proc_type" "$pid" "$exit_code"
"${_pm_callbacks[$proc_type]}" "$event" "$proc_type" "$pid" "$@"
fi
fi
}

View File

@ -161,6 +161,10 @@ callback() {
local event="$1" proc_type="$2" pid="$3" exit_code
if [ "$event" = "start" ]; then
shift 3
if [ "$#" -gt 0 ]; then
echo "$*" > "${_pm_output}/${pid}-${proc_type}-start-args"
fi
touch "${_pm_output}/${pid}-${proc_type}-start"
elif [ "$event" = "exit" ]; then
exit_code="$4"
@ -216,4 +220,15 @@ callback() {
assert [ -f "${_pm_output}/${pid1}-sleepy-killed-exit" ]
assert [ -f "${_pm_output}/${pid2}-sleepy-start" ]
assert [ -f "${_pm_output}/${pid2}-sleepy-1-exit" ]
}
@test "should allow passing custom arguments to lifecycle callback" {
pm_register_callback "sleepy" "callback"
pm_async sleep 0.1 -%- "sleepy" "arg1" "arg2"
pid=$result
await "$pid"
assert_equal "$(cat "${_pm_output}/${pid}-sleepy-start-args")" "arg1 arg2"
}