move job control to module background and make it more failsave

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-20 21:40:14 +02:00
parent 8dfdf2e168
commit fd7ca77cc1
2 changed files with 46 additions and 41 deletions

View File

@ -11,7 +11,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.80-pre-8-gf1ebdbb #### $$VERSION$$ v0.80-pre-9-g8dfdf2e
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -542,45 +542,9 @@ if [ "$1" != "source" ]; then
fi fi
exit exit
;; ;;
"backgr"* | "resumeb"*) "resumeb"* | "killb"* | "suspendb"*)
_is_function start_proc || { echo -e "${RED}Module background not availible.${NC}"; exit 3; } _is_function job_control || { echo -e "${RED}Module background is not availible!${NC}"; exit 3; }
echo -e "${GREEN}Restart background processes ...${NC}" job_control "$1"
for FILE in "${TMPDIR:-.}/"*-back.cmd; do
if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then
echo -e "${RED}No background processes to start.${NC}"; break
else
CONTENT="$(< "${FILE}")"
CHAT[ID]="${CONTENT%%:*}"
JOB="${CONTENT#*:}"
PROG="${JOB#*:}"
JOB="${JOB%:*}"
fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")"
echo "restart background job ${PROG} ${fifo}"
start_proc "${CHAT[ID]}" "${PROG}" "back-${JOB}-"
fi
done
;;
"killb"* | "suspendb"*)
_is_function start_proc || { echo -e "${RED}Module background not availible.${NC}"; exit 3; }
echo -e "${GREEN}Stopping background processes ...${NC}"
for FILE in "${TMPDIR:-.}/"*-back.cmd; do
if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then
echo -e "${RED}No background processes.${NC}"; break
else
CONTENT="$(< "${FILE}")"
CHAT[ID]="${CONTENT%%:*}"
JOB="${CONTENT#*:}"
JOB="${JOB%:*}"
fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")"
if [[ "$1" = "killb"* ]]; then
rm -f "${FILE}" # remove job
echo "kill background job ${fifo}"
else
echo "suspend background job ${fifo}"
fi
kill_proc "${CHAT[ID]}" "back-${JOB}-"
fi
done
;; ;;
"help") "help")
less "README.txt" less "README.txt"

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.80-pre-3-g3c5ffdb #### $$VERSION$$ v0.80-pre-9-g8dfdf2e
# source from commands.sh if you want ro use interactive or background jobs # source from commands.sh if you want ro use interactive or background jobs
@ -103,3 +103,44 @@ send_interactive() {
inproc() { inproc() {
send_interactive "${CHAT[ID]}" "${MESSAGE}" send_interactive "${CHAT[ID]}" "${MESSAGE}"
} }
# start stopp all jobs
# $1 command
# killb*
# suspendb*
# resumeb*
job_control() {
local content proc CHAT job fifo
for FILE in "${TMPDIR:-.}/"*-back.cmd; do
if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then
echo -e "${RED}No background processes.${NC}"
break
else
content="$(< "${FILE}")"
CHAT="${content%%:*}"
job="${content#*:}"
proc="${job#*:}"
job="back-${job%:*}-"
fifo="$(procname "${CHAT}" "${job}")"
case "$1" in
"resumeb"*|"backgr"*)
echo "Restart Job: ${proc} ${fifo}"
start_proc "${CHAT}" "${proc}" "${job}"
;;
"suspendb"*)
echo "Suspend Job: ${proc} ${fifo}"
kill_proc "${CHAT}" "${proc}" "${job}"
rm -f "${TMPDIR:-.}/${fifo}"
[ -s "${TMPDIR:-.}/${fifo}.log" ] || rm -f "${TMPDIR:-.}/${fifo}.log"
;;
"killb"*)
echo "Kill Job: ${proc} ${fifo}"
kill_proc "${CHAT}" "${proc}" "${job}"
rm -f "${FILE}" # remove job
rm -f "${TMPDIR:-.}/${fifo}"
[ -s "${TMPDIR:-.}/${fifo}.log" ] || rm -f "${TMPDIR:-.}/${fifo}.log"
;;
esac
fi
done
}