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