queued/sources/queued
2013-12-24 00:38:40 +04:00

166 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# queued is a simple daemon for starting jobs to queue of calculations
# Copyright (C) 2013 Evgeniy Alekseev
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see http://www.gnu.org/licenses
# or write to the Free Software Foundation,Inc., 51 Franklin Street,
# Fifth Floor, Boston, MA 02110-1301 USA
# functions
error_mes() {
case "$1" in
"config" ) echo "[EE] Configuration file is not set";;
"file" ) echo "[EE] '$2' is a file";;
"flag" ) echo "[EE] Unknown flag";;
"number" ) echo "[EE] '$2' is not a number";;
"unknown" ) echo "[EE] Unknown error";;
esac
exit 1
}
func_help() {
echo -e "Simple daemon written on BASH for starting jobs to queue of calculations"
echo -e "\nUsage: queued [ -c /etc/queued.conf ] [ -v | --version ] [ -h | --help ]"
echo -e "\nParametrs:"
echo -e " -c PATH - path to configuration file. Default is '/etc/queued.conf'"
echo -e "\n -v --version - show version and exit"
echo -e " -h --help - show this help and exit"
exit 0
}
func_ver() {
echo -e " queued "
echo -e "Simple daemon for starting jobs to queue of calculations"
echo -e "Version : 1.2.0 License : GPLv3"
echo -e "Author : Evgeniy Alexeev aka arcanis"
echo -e "E-mail : esalexeev (at) gmail.com"
exit 0
}
isnum() {
(t=$(( 0$1+0 ))) 2>/dev/null
}
start_job() {
echo "[II] Running job '$CURJOB' (priority '$CURJOB_PRIOR') as '$CURJOB_USER'"
su -c "/bin/sh "$JOBDIR/$(basename "$CURJOB")" &> "$JOBDIR/$(basename "$CURJOB")".log" $CURJOB_USER &
}
CONF_FILE="/etc/queued.conf"
# parametrs parsing
until [ -z $1 ]; do
case "$1" in
"-h" | "--help" ) func_help;;
"-v" | "--version" ) func_ver;;
"-c" ) [ -z "$2" ] && error_mes "config" || CONF_FILE="$2" && shift;;
* ) error_mes "flag";;
esac
shift
done
# default values
JOBDIR="/var/lib/queued/job"
PRIORITY=0
QUEUEFILE="/var/lib/queued/queue"
SLEEPTIME=5
STARTASUSER="root"
WORKDIR="/var/lib/queued/work"
echo "[II] Reading configuration from '$CONF_FILE'"
[ -e "$CONF_FILE" ] || error_mes "config"
for VAR in "$(cat "$CONF_FILE")"; do eval "$VAR"; done
# prepare
# creating directories if doesn't exist
if [ ! -d "$JOBDIR/done" ]; then
[ -e "$JOBDIR/done" ] && error_mes "file" "$JOBDIR/done"
echo "[II] Creating directory '$JOBDIR/done'"
mkdir -m777 -p "$JOBDIR/done" || error_mes "unknown"
fi
if [ ! -d "$WORKDIR" ]; then
[ -e "$WORKDIR" ] && error_mes "file" "$WORKDIR"
echo "[II] Creating directory '$WORKDIR'"
mkdir -m777 -p "$WORKDIR" || error_mes "unknown"
fi
if [ ! -d "$(dirname "$QUEUEFILE")" ]; then
[ -e "$(dirname "$QUEUEFILE")" ] && error_mes "file" "$(dirname "$QUEUEFILE")"
echo "[II] Creating directory '$(dirname "$QUEUEFILE")'"
mkdir -m777 -p "$(dirname "$QUEUEFILE")" || error_mes "unknown"
echo "[II] Creating file '$QUEUEFILE'"
touch "$QUEUEFILE" || error_mes "unknown"
chmod 777 "$QUEUEFILE"
fi
# check priority
isnum "$PRIORITY" || error_mes "number" "$PRIORITY"
# check sleep time
isnum "$SLEEPTIME" && SLEEPTIME=$(($SLEEPTIME*60)) || error_mes "number" "$SLEEPTIME"
# work block
# change cwd
cd /
< /dev/null > /dev/null 2>&1 &
# forking
(
while true; do
# check files
echo "[II] Checking new files"
for FILE in $(ls "$WORKDIR/"); do
[ -d "$WORKDIR/$FILE" ] && continue
FILE_EXT="${FILE##*.}"
[ "$FILE_EXT" = "pr" ] && continue
[ "$FILE_EXT" = "user" ] && continue
[ -e "$QUEUEFILE" ] && grep --quiet "$WORKDIR/$FILE" "$QUEUEFILE" && continue
echo "[II] Adding file '$FILE' to list"
if [ -e "$WORKDIR/$FILE".pr ]; then
CURJOB_PRIOR=$(cat "$WORKDIR/$FILE".pr)
isnum "$CURJOB_PRIOR" || CURJOB_PRIOR="$PRIORITY"
else
CURJOB_PRIOR="$PRIORITY"
fi
echo "[II] Setting priority to '$CURJOB_PRIOR'"
echo "$CURJOB_PRIOR==$WORKDIR/$FILE" >> "$QUEUEFILE"
done
# check running job
CALC=0
for FILE in $(ls "$JOBDIR/"); do
[ -d "$JOBDIR/$FILE" ] && continue
(ps aux | grep "$JOBDIR/$FILE" | grep --quiet --invert-match "grep") && CALC=$(($CALC+1))
done
# running job
if [[ "$CALC" = "0" ]]; then
CURJOB_PRIOR=$(head -1 "$QUEUEFILE" | awk -F "==" '{print $1}')
CURJOB=$(head -1 "$QUEUEFILE" | awk -F "==" '{print $2}')
for JOB in $(cat "$QUEUEFILE"); do
if [[ "$(echo "$JOB" | awk -F "==" '{print $1}')" > "$CURJOB_PRIOR" ]]; then
CURJOB_PRIOR=$(echo "$JOB" | awk -F "==" '{print $1}')
CURJOB=$(echo "$JOB" | awk -F "==" '{print $2}')
fi
done
[ -e "$CURJOB".user ] && CURJOB_USER="$(cat "$CURJOB".user)" || CURJOB_USER="$STARTASUSER"
sed '/'"$(basename "$CURJOB")"'/d' -i "$QUEUEFILE"
if [ -z "$CURJOB" ]; then
echo "[II] You haven't job"
else
mv "$CURJOB"* "$JOBDIR/" && start_job || echo "[WW] Missing files for job '$CURJOB'"
fi
else
echo "[II] You have a running job"
fi
# wait
sleep $SLEEPTIME
done
) &