mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-22 07:25:10 +00:00
move job control to module background and make it more failsave
This commit is contained in:
parent
8dfdf2e168
commit
fd7ca77cc1
44
bashbot.sh
44
bashbot.sh
@ -11,7 +11,7 @@
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
#
|
||||
#### $$VERSION$$ v0.80-pre-8-gf1ebdbb
|
||||
#### $$VERSION$$ v0.80-pre-9-g8dfdf2e
|
||||
#
|
||||
# Exit Codes:
|
||||
# - 0 sucess (hopefully)
|
||||
@ -542,45 +542,9 @@ if [ "$1" != "source" ]; then
|
||||
fi
|
||||
exit
|
||||
;;
|
||||
"backgr"* | "resumeb"*)
|
||||
_is_function start_proc || { echo -e "${RED}Module background not availible.${NC}"; exit 3; }
|
||||
echo -e "${GREEN}Restart background processes ...${NC}"
|
||||
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
|
||||
"resumeb"* | "killb"* | "suspendb"*)
|
||||
_is_function job_control || { echo -e "${RED}Module background is not availible!${NC}"; exit 3; }
|
||||
job_control "$1"
|
||||
;;
|
||||
"help")
|
||||
less "README.txt"
|
||||
|
@ -5,7 +5,7 @@
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# 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
|
||||
|
||||
@ -103,3 +103,44 @@ send_interactive() {
|
||||
inproc() {
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user