createworker() destroyworker()

This commit is contained in:
Vincent Riquer 2013-04-01 23:10:37 +02:00
parent d8adee3177
commit 63e01692fc

83
atom
View File

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