createworker() destroyworker()
This commit is contained in:
parent
d8adee3177
commit
63e01692fc
83
atom
83
atom
@ -1260,6 +1260,8 @@ encodeFile::vorbis() {
|
|||||||
|
|
||||||
worker() {
|
worker() {
|
||||||
trap "kill -USR1 $masterpid" EXIT
|
trap "kill -USR1 $masterpid" EXIT
|
||||||
|
trap - USR1 ALRM PIPE
|
||||||
|
exec 2>>"$tempdir/errors.log"
|
||||||
while :
|
while :
|
||||||
do
|
do
|
||||||
echo work
|
echo work
|
||||||
@ -1271,6 +1273,7 @@ worker() {
|
|||||||
done
|
done
|
||||||
if [[ $line == AtOM:Die ]]
|
if [[ $line == AtOM:Die ]]
|
||||||
then
|
then
|
||||||
|
trap EXIT
|
||||||
break
|
break
|
||||||
elif [[ $line == AtOM:Sleep ]]
|
elif [[ $line == AtOM:Sleep ]]
|
||||||
then
|
then
|
||||||
@ -1353,8 +1356,8 @@ worker() {
|
|||||||
do
|
do
|
||||||
[ -z "${cmd_arg[key]}" ] && unset cmd_arg[key]
|
[ -z "${cmd_arg[key]}" ] && unset cmd_arg[key]
|
||||||
done
|
done
|
||||||
(( debug >= 2 )) && echo "${cmd_arg[@]}" >>"$tempdir/errors.log"
|
(( debug >= 2 )) && echo "${cmd_arg[@]}" >&2
|
||||||
if "${cmd_arg[@]}" >/dev/null 2>>"$tempdir/errors.log"
|
if "${cmd_arg[@]}" >/dev/null
|
||||||
then
|
then
|
||||||
echo "finished $taskid|$sourcefileid|$destfileid|$destfilename"
|
echo "finished $taskid|$sourcefileid|$destfileid|$destfilename"
|
||||||
read line
|
read line
|
||||||
@ -1408,13 +1411,7 @@ master() {
|
|||||||
'work')
|
'work')
|
||||||
if [ -n "$quit" ]
|
if [ -n "$quit" ]
|
||||||
then
|
then
|
||||||
dyingworker=${workers[workerid]}
|
destroyworker $workerid
|
||||||
unset workers[workerid]
|
|
||||||
eval echo AtOM:Die '>&'$((100+workerid))
|
|
||||||
wait $dyingworker
|
|
||||||
eval $((100+workerid))'>&-'
|
|
||||||
eval $((200+workerid))'<&-'
|
|
||||||
rm "$tempdir"/worker${workerid}{in,out}
|
|
||||||
elif (( active < concurrency ))
|
elif (( active < concurrency ))
|
||||||
then
|
then
|
||||||
echo '
|
echo '
|
||||||
@ -1474,13 +1471,7 @@ master() {
|
|||||||
read -u4 ready
|
read -u4 ready
|
||||||
if (( remaining == 0 ))
|
if (( remaining == 0 ))
|
||||||
then
|
then
|
||||||
dyingworker=${workers[workerid]}
|
destroyworker $workerid
|
||||||
unset workers[workerid]
|
|
||||||
eval echo AtOM:Die '>&'$((100+workerid))
|
|
||||||
wait $dyingworker
|
|
||||||
eval $((100+workerid))'>&-'
|
|
||||||
eval $((200+workerid))'<&-'
|
|
||||||
rm "$tempdir"/worker${workerid}{in,out}
|
|
||||||
continue
|
continue
|
||||||
elif (( ready == 0 ))
|
elif (( ready == 0 ))
|
||||||
then
|
then
|
||||||
@ -1572,17 +1563,45 @@ master() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getworkerid() {
|
||||||
|
local i
|
||||||
|
for (( i=0 ; i < 100 ; i++ ))
|
||||||
|
do
|
||||||
|
if [ -z "${workers[i]}" ]
|
||||||
|
then
|
||||||
|
echo $i
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
# If we reach this, we have reached the hardcoded 100 workers limit
|
||||||
|
(( concurrency-- ))
|
||||||
|
}
|
||||||
|
|
||||||
|
createworker() {
|
||||||
|
mkfifo "$tempdir"/worker$1{in,out}
|
||||||
|
worker $1 <"$tempdir"/worker$1in >"$tempdir"/worker$1out &
|
||||||
|
workers[$1]=$!
|
||||||
|
eval exec $((100+$1))'>"$tempdir"/worker$1in'
|
||||||
|
eval exec $((200+$1))'<"$tempdir"/worker$1out'
|
||||||
|
}
|
||||||
|
|
||||||
|
destroyworker() {
|
||||||
|
dyingworker=${workers[$1]}
|
||||||
|
unset workers[$1]
|
||||||
|
[ -z "$2" ] && eval echo AtOM:Die '>&'$((100+$1))
|
||||||
|
wait $dyingworker
|
||||||
|
eval $((100+$1))'>&-'
|
||||||
|
eval $((200+$1))'<&-'
|
||||||
|
rm "$tempdir"/worker$1{in,out}
|
||||||
|
}
|
||||||
|
|
||||||
checkworkers() {
|
checkworkers() {
|
||||||
for key in ${!workers[@]}
|
for key in ${!workers[@]}
|
||||||
do
|
do
|
||||||
if ! kill -0 ${workers[key]}
|
if ! kill -0 ${workers[key]} 2>/dev/null
|
||||||
then
|
then
|
||||||
worker $key \
|
destroyworker $key nokill
|
||||||
<"$tempdir"/worker${key}in \
|
createworker $key
|
||||||
>"$tempdir"/worker${key}out &
|
|
||||||
workers[key]=$!
|
|
||||||
eval exec $((100+key))'>"$tempdir"/worker${key}in'
|
|
||||||
eval exec $((200+key))'<"$tempdir"/worker${key}out'
|
|
||||||
(( ++failed ))
|
(( ++failed ))
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@ -2031,19 +2050,14 @@ echo 'COMMIT;' >&3
|
|||||||
echo -e "\rCreated ${count:-0} tasks for $filecount files (${copies:-0} immediate copies)"
|
echo -e "\rCreated ${count:-0} tasks for $filecount files (${copies:-0} immediate copies)"
|
||||||
|
|
||||||
masterpid=$$
|
masterpid=$$
|
||||||
trap checkworkers USR1 ALRM
|
trap checkworkers USR1 ALRM PIPE
|
||||||
rm -f "$tempdir"/worker*
|
rm -f "$tempdir"/worker*
|
||||||
concurrency=$(( maxload / 2 ))
|
concurrency=$(( maxload / 2 ))
|
||||||
(( concurrency )) || concurrency=1
|
(( concurrency )) || concurrency=1
|
||||||
active=0
|
active=0
|
||||||
for (( i=0 ; i < concurrency ; i++ ))
|
for (( i=0 ; i < concurrency ; i++ ))
|
||||||
do
|
do
|
||||||
(( ++wnum ))
|
createworker $(getworkerid)
|
||||||
mkfifo "$tempdir"/worker${wnum}{in,out}
|
|
||||||
worker $wnum <"$tempdir"/worker${wnum}in >"$tempdir"/worker${wnum}out &
|
|
||||||
workers[wnum]=$!
|
|
||||||
eval exec $((100+wnum))'>"$tempdir"/worker${wnum}in'
|
|
||||||
eval exec $((200+wnum))'<"$tempdir"/worker${wnum}out'
|
|
||||||
done
|
done
|
||||||
concurrencychange=$(date +%s)
|
concurrencychange=$(date +%s)
|
||||||
starttime=$concurrencychange
|
starttime=$concurrencychange
|
||||||
@ -2078,14 +2092,7 @@ do
|
|||||||
then
|
then
|
||||||
concurrencychange=$(date +%s)
|
concurrencychange=$(date +%s)
|
||||||
(( ++concurrency ))
|
(( ++concurrency ))
|
||||||
(( ++wnum ))
|
createworker $(getworkerid)
|
||||||
mkfifo "$tempdir"/worker${wnum}{in,out}
|
|
||||||
worker $wnum \
|
|
||||||
<"$tempdir"/worker${wnum}in \
|
|
||||||
>"$tempdir"/worker${wnum}out &
|
|
||||||
workers[wnum]=$!
|
|
||||||
eval exec $((100+wnum))'>"$tempdir"/worker${wnum}in'
|
|
||||||
eval exec $((200+wnum))'<"$tempdir"/worker${wnum}out'
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
master
|
master
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user