This commit is contained in:
Loic Guegan 2021-08-21 20:42:11 +02:00
parent 550b99022b
commit be4c8b2f37

48
pool.sh
View file

@ -11,11 +11,11 @@ then
fi fi
# Config: feel free to change at your own risks # Config: feel free to change at your own risks
tmpdir=/tmp TMPDIR=/tmp
pool_format="pool_${POOL_ID}" POOL_FORMAT="pool_${POOL_ID}"
pool="${tmpdir}/${pool_format}/" POOL="${TMPDIR}/${POOL_FORMAT}/"
pool_status="${pool}/status" POOL_STATUS="${POOL}/status"
REFRESH_EVERY=1 REFRESH_EVERY=2
abort() { abort() {
echo $@ echo $@
@ -23,14 +23,14 @@ abort() {
} }
write_status() { write_status() {
echo "maxproc=${maxproc}" > "$pool_status" echo "maxproc=${maxproc}" > "$POOL_STATUS"
echo "nproc=${nproc}" >> "$pool_status" echo "nproc=${nproc}" >> "$POOL_STATUS"
echo "procs=\"${procs}\"" >> "$pool_status" echo "procs=\"${procs}\"" >> "$POOL_STATUS"
echo "lastprocid=$lastprocid" >> "$pool_status" echo "lastprocid=$lastprocid" >> "$POOL_STATUS"
} }
write_process() { create_properties() {
file="${pool}/proc_${procid}" file="${POOL}/proc_${procid}"
echo cmd=\"${PROC_CMD}\" > "$file" echo cmd=\"${PROC_CMD}\" > "$file"
echo "procpid=${procpid}" >> "$file" echo "procpid=${procpid}" >> "$file"
echo "procid=${procid}" >> "$file" echo "procid=${procid}" >> "$file"
@ -38,20 +38,20 @@ write_process() {
} }
setp() { setp() {
file="${pool}/proc_${procid}" file="${POOL}/proc_${procid}"
echo $1=$2 >> "$file" echo $1=$2 >> "$file"
} }
getp() { getp() {
file="${pool}/proc_${procid}" file="${POOL}/proc_${procid}"
source "$file" source "$file"
echo "${!1}" echo "${!1}"
} }
create() { create() {
[ -d "$pool" ] && abort "Pool \"$pool\" already exists" [ -d "$POOL" ] && abort "Pool \"$POOL\" already exists"
mkdir -p "$pool" mkdir -p "$POOL"
# Write pool status # Write pool status
maxproc=$1 maxproc=$1
nproc=0 nproc=0
@ -63,11 +63,11 @@ create() {
remove() { remove() {
refresh # Refresh process status and load status refresh # Refresh process status and load status
[ $nproc -gt 0 ] && abort "Processes are still running in the pool!" [ $nproc -gt 0 ] && abort "Processes are still running in the pool!"
rm -rf "$pool" rm -rf "$POOL"
} }
refresh() { refresh() {
source "$pool_status" source "$POOL_STATUS"
procs_new="" procs_new=""
for proc in $procs for proc in $procs
do do
@ -94,21 +94,21 @@ run() {
procid=$(( lastprocid + 1 )) procid=$(( lastprocid + 1 ))
lastprocid=$procid lastprocid=$procid
startat=$(date +"%s") startat=$(date +"%s")
$PROC_CMD > "$pool/out_$procid" & $PROC_CMD > "$POOL/out_$procid" &
procpid=$! procpid=$!
[ -z "$procs" ] && procs="$procpid" || procs="$procs $procpid" [ -z "$procs" ] && procs="$procpid" || procs="$procs $procpid"
write_status # Update status write_status # Update status
write_process # Create process properties create_properties # Create process properties
echo $procid # Return process id echo $procid # Return process id
} }
cat_output() { cat_output() {
file="${pool}/out_${procid}" file="${POOL}/out_${procid}"
cat "$file" cat "$file"
} }
list_pool() { list_pool() {
find "${tmpdir}" -name "${pool_format}*" -exec basename {} \; 2>/dev/null | sed "s/${pool_format}//g" find "${TMPDIR}" -name "${POOL_FORMAT}*" -exec basename {} \; 2>/dev/null | sed "s/${POOL_FORMAT}//g"
} }
wait_pool() { wait_pool() {
@ -122,7 +122,7 @@ wait_pool() {
list_output() { list_output() {
refresh refresh
find "${pool}" -name "out_*" find "${POOL}" -name "out_*"
} }
remove_force() { remove_force() {
@ -131,11 +131,11 @@ remove_force() {
do do
kill -9 $proc kill -9 $proc
done done
rm -rf "$pool" rm -rf "$POOL"
} }
# Ensure pool exists # Ensure pool exists
[ ! "$CMD" == "create" ] && [ ! "$CMD" == "ls" ] && [ ! -d "$pool" ] && abort "Pool $POOL_ID not found" [ ! "$CMD" == "create" ] && [ ! "$CMD" == "ls" ] && [ ! -d "$POOL" ] && abort "Pool $POOL_ID not found"
# Launch command # Launch command
case "$CMD" in case "$CMD" in