aboutsummaryrefslogtreecommitdiff
path: root/pool.sh
diff options
context:
space:
mode:
Diffstat (limited to 'pool.sh')
-rwxr-xr-xpool.sh69
1 files changed, 55 insertions, 14 deletions
diff --git a/pool.sh b/pool.sh
index 9e97d86..7336545 100755
--- a/pool.sh
+++ b/pool.sh
@@ -30,7 +30,7 @@ write_status() {
}
create_properties() {
- file="${POOL}/proc_${procid}"
+ local file="${POOL}/proc_${procid}"
echo cmd=\"${PROC_CMD}\" > "$file"
echo "procpid=${procpid}" >> "$file"
echo "procid=${procid}" >> "$file"
@@ -38,19 +38,20 @@ create_properties() {
}
setp() {
- file="${POOL}/proc_${procid}"
- echo $1=$2 >> "$file"
+ local file="${POOL}/proc_${procid}"
+ sed -i "/${1}=/d" "$file"
+ echo $1=\"$2\" >> "$file"
}
getp() {
- file="${POOL}/proc_${procid}"
- source "$file"
- echo "${!1}"
+ local file="${POOL}/proc_${procid}"
+ value=$(grep "^$1=" "$file"||:)
+ [ -z "$value" ] && echo "Property $1 not found for process ${procid}" || { echo "$value" | sed "s/^$1=//" | xargs echo; }
}
create() {
- [ -d "$POOL" ] && abort "Pool \"$POOL\" already exists"
+ [ -d "$POOL" ] && abort "Pool \"$POOL\" already exists" || :
mkdir -p "$POOL"
# Write pool status
maxproc=$1
@@ -62,7 +63,7 @@ create() {
remove() {
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"
}
@@ -94,7 +95,7 @@ run() {
procid=$(( lastprocid + 1 ))
lastprocid=$procid
startat=$(date +"%s")
- $PROC_CMD > "$POOL/out_$procid" &
+ $PROC_CMD &> "$POOL/out_$procid" &
procpid=$!
[ -z "$procs" ] && procs="$procpid" || procs="$procs $procpid"
write_status # Update status
@@ -103,7 +104,7 @@ run() {
}
cat_output() {
- file="${POOL}/out_${procid}"
+ local file="${POOL}/out_${procid}"
cat "$file"
}
@@ -134,8 +135,31 @@ remove_force() {
rm -rf "$POOL"
}
-# Ensure pool exists
-[ ! "$CMD" == "create" ] && [ ! "$CMD" == "ls" ] && [ ! -d "$POOL" ] && abort "Pool $POOL_ID not found"
+check_pool() {
+ if [ ! -d "$POOL" ]; then abort "Pool ${POOL_ID} not found"; fi
+ if [ ! -f "$POOL_STATUS" ]; then abort "Pool ${POOL_ID} corrupted: status file not found"; fi
+ if [ $# -gt 0 ]
+ then
+ [ ! -f "${POOL}/proc_${1}" ] && echo abort "Process $1 does not exists in pool ${POOL_ID}" || :
+ fi
+}
+
+pause() {
+ refresh
+ for proc in $procs
+ do
+ kill -STOP $proc
+ done
+}
+
+resume() {
+ refresh
+ for proc in $procs
+ do
+ kill -CONT $proc
+ done
+}
+
# Launch command
case "$CMD" in
@@ -143,37 +167,54 @@ case "$CMD" in
create $1
;;
"remove")
+ check_pool
remove
;;
"run")
+ check_pool
run
;;
"cat")
+ check_pool $1
procid=$1
refresh
- [ $procid -gt $lastprocid ] && abort "Processus $procid not found"
- cat_output
+ [ $procid -gt $lastprocid ] && abort "Processus $procid not found" || cat_output
;;
"ls-output")
+ check_pool
list_output
;;
"ls")
+ check_pool
list_pool
;;
"wait")
+ check_pool
wait_pool
;;
"remove-force")
+ check_pool
remove_force
;;
"setp")
+ check_pool $1
procid=$1
setp $2 $3
;;
"getp")
+ check_pool $1
procid=$1
getp $2
;;
+ "pause")
+ check_pool
+ pause
+ ;;
+ "resume")
+ check_pool
+ resume
+ ;;
*)
abort "Command $CMD unknown"
esac
+