-
Notifications
You must be signed in to change notification settings - Fork 0
/
workqueue.bash
executable file
·85 lines (65 loc) · 1.74 KB
/
workqueue.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
#
# Two batch queues are generated:
# A: queues user tasks (n jobs)
# Z: queues the queue-handler (queues respawn: 1 job)
# Queues with higher letters run with increased niceness
# A is higher than Z, so A will run before Z
#
# Config:
#
JOBS_FILE='/path/to/your/workqueue.jobs'
# load_avg maybe be something like: number of cores - 1
LOAD_AVG=3
# minimum interval in seconds between the start of two batch jobs
TIME_WINDOW=30
#
# Config end
#
pushd $(dirname $0) > /dev/null
SCRIPT_PATH=$(pwd -P)
popd > /dev/null
SCRIPT=$(basename $0)
SCRIPT_FULLPATH=$SCRIPT_PATH/$SCRIPT
if [ $# -lt 1 ]
then
echo "Usage : $SCRIPT_FULLPATH [start|stop|status|dump TASK_ID]"
exit
fi
case "$1" in
"stop")
for i in $(atq -q A | cut -f 1); do
atrm $i;
done
for i in $(atq -q Z | cut -f 1); do
atrm $i;
done
;;
"status")
if [ $(atq |wc -l) -gt 0 ]; then
echo 'Running, at queued jobs:'
for i in $(atq | cut -f 1); do
echo "$i";
done
fi
;;
"dump")
if [ $# -lt 2 ]; then
echo "Usage : $SCRIPT_FULLPATH dump TASK_ID"
exit
fi
at -c $2
;;
"start")
# rerun at-daemon with the specified parameters
service atd stop > /dev/null 2>&1
atd -l $LOAD_AVG -b $TIME_WINDOW > /dev/null 2>&1
if [ ! $(atq -q A|wc -l) -gt 1 ]; then
IFS=$'\r\n' TASKS=($(cat $JOBS_FILE))
for t in "${TASKS[@]}"; do
echo "$t" | at -M -q A now > /dev/nul 2>&1
done
fi
[ ! $(atq -q Z|wc -l) -gt 0 ] && echo "$SCRIPT_FULLPATH start" | at -q Z now > /dev/nul 2>&1
;;
esac