Jpp 20.0.0-72-g597b30bc9
the software that should make you happy
Loading...
Searching...
No Matches
mkqueue.sh
Go to the documentation of this file.
1#!/usr/bin/env zsh
2#
3# \author mdejong
4#
5script=${0##*/}
6
7# ----------------------------------------------------------------------------------------
8# Simple desktop batch queue processor.
9#
10# The batch queue is defined by a standard FIFO and
11# the entries in the queue are processed line-by-line.
12# Each entry is executed as a separate process in the background,
13# commonly referred to as a job.
14# The number of jobs that can simultaneously run is limited by the queue size.
15# The batch queue can be terminated by submitting a job with contents "exit".
16#
17# The batch queue may also be an existing regular file.
18# The entries in the queue are processed as normal but
19# the batch queue will terminate when the end-of-file is reached.
20#
21# A job can be submitted using the puts_queue function (see qlib.sh or qlib.csh).
22# A series of joint commands can be submitted using a sequence of
23# calls to put_queue, terminated by a single puts_queue call.
24# ----------------------------------------------------------------------------------------
25
26if [ -z $JPP_DIR ]; then
27 echo "Variable JPP_DIR undefined."
28 exit
29fi
30
31source $JPP_DIR/setenv.sh $JPP_DIR >& /dev/null
32source $JPP_DIR/software/scripts/qlib.sh
33
34set_variable: DEBUG MKQUEUE_DEBUG 3
35set_variable: SLEEP_TIME MKQUEUE_SLEEP_TIME 1s # sleep time
36set_variable QUEUE_SIZE 1 # default size of queue
37set_variable COMMAND mkqueue.sh
38
39if do_usage $*; then
40 usage "$script <queue name> [queue size] <option>"\
41 "\nPossible options: start, stop, continue, wait, restart."
42fi
43
44case $# in
45 3) set_variable QUEUE_SIZE $argv[2];&
46 2) set_variable QUEUE_NAME $argv[1];
47 set_variable OPTION $argv[-1];;
48 *) fatal "Wrong number of options.";;
49esac
50
51if ( is_CCLyon ); then
52 fatal "This batch queue processor is intended for use on a desktop."
53fi
54
55if (( $QUEUE_SIZE < 1 )); then
56 fatal "Invalid queue size $QUEUE_SIZE."
57fi
58
59if [[ "$OPTION" != "start" ]] && [[ "$OPTION" != "stop" ]] && [[ "$OPTION" != "continue" ]] && [[ "$OPTION" != "wait" ]] && [[ "$OPTION" != "restart" ]]; then
60 fatal "Invalid option $OPTION."
61fi
62
63
64# short-hand to get PID of batch queue processor
65
66alias get_mypid="pgrep -f \"$COMMAND *$QUEUE_NAME\""
67
68
69#
70# Run queue.
71#
72# \param 1 queue name
73# \param 2 queue size
74# \param 3 sleep time [s]
75#
76function queue()
77{
78 queue_name=$1
79 queue_size=$2
80 sleep_time=$3
81
82 if [[ -e $queue_name ]]; then
83
84 if [[ -p $queue_name ]]; then
85 fatal "Queue $queue_name in use."
86 fi
87 else
88
89 mkfifo $queue_name # create a new FIFO
90 fi
91
92 fd=7 # file descriptor
93
94 eval "exec $fd<>$queue_name" # attach file descriptor to FIFO
95
96 while read job <& $fd; do # read job from file descriptor
97
98 if [[ -n $job ]]; then
99
100 if [[ $job = "exit" ]]; then
101 break
102 fi
103
104 if [[ $job = "wait" ]]; then
105
106 wait # wait child processes
107
108 break
109 fi
110
111 notice "submit $job"
112 eval exec $job & # submit job
113
114 # wait for free slot
115
116 while (( ${#jobstates} >= $queue_size )); do
117 sleep $sleep_time
118 done
119 fi
120 done
121
122 rm -f $queue_name # remove FIFO
123}
124
125
126if [[ "$OPTION" == "stop" || "$OPTION" == "restart" ]]; then
127
128 PID=(`get_mypid`)
129
130 PID[$PID[(i)$$]]=
131
132 if [[ -n $PID ]]; then
133 notice "Stop $COMMAND $QUEUE_NAME."
134 kill -9 $PID
135 fi
136
137 rm -f $QUEUE_NAME # remove FIFO
138
139 OPTION=${OPTION/restart/start}
140
141elif [[ "$OPTION" == "continue" ]]; then
142
143 PID=(`get_mypid`)
144
145 PID[$PID[(i)$$]]=
146
147 if [[ -n $PID ]]; then
148 notice "$script is running with PID(s) $PID"
149 else
150 OPTION=start
151 fi
152
153elif [[ "$OPTION" == "wait" ]]; then
154
155 puts_queue $QUEUE_NAME wait
156
157 while (( 1 )); do
158
159 PID=(`get_mypid`)
160
161 PID[$PID[(i)$$]]=
162
163 if [[ -n $PID ]]; then
164 sleep $SLEEP_TIME
165 else
166 break
167 fi
168 done
169fi
170
171if [[ "$OPTION" = "start" ]]; then
172
173 notice "Start batch queue ${QUEUE_NAME} >& ${QUEUE_NAME}_log"
174
175 trap "" HUP # trap hangup
176
177 queue $QUEUE_NAME $QUEUE_SIZE $SLEEP_TIME >& ${QUEUE_NAME}_log &
178
179 while [[ ! -e $QUEUE_NAME ]]; do
180 sleep $SLEEP_TIME
181 done
182fi