source: trunk/Mars/datacenter/scripts/sourcefile@ 9872

Last change on this file since 9872 was 9853, checked in by Daniela Dorner, 14 years ago
implemented faster queries for the dependencies between the steps
  • Property svn:executable set to *
File size: 12.5 KB
Line 
1#!/bin/sh
2#
3# ========================================================================
4#
5# *
6# * This file is part of MARS, the MAGIC Analysis and Reconstruction
7# * Software. It is distributed to you in the hope that it can be a useful
8# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9# * It is distributed WITHOUT ANY WARRANTY.
10# *
11# * Permission to use, copy, modify and distribute this software and its
12# * documentation for any purpose is hereby granted without fee,
13# * provided that the above copyright notice appear in all copies and
14# * that both that copyright notice and this permission notice appear
15# * in supporting documentation. It is provided "as is" without express
16# * or implied warranty.
17# *
18#
19#
20# Author(s): Daniela Dorner 05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
21#
22# Copyright: MAGIC Software Development, 2000-2010
23#
24#
25# ========================================================================
26#
27# This a resource file for the scripts, in which the standard paths and
28# functions, which are needed more often are stored.
29# Only constant variables are stored here, changing variables are stored
30# in datacenter/scripts/setup
31#
32
33# check if script has been started with absolute path
34if ! dirname $0 | grep -E '^/' >/dev/null 2>&1
35then
36 echo "Please start your script with an absolute path."
37 exit
38fi
39
40if [ "$AUTOMATIONSETUP" = "" ]
41then
42 echo "Please set the environment variable \$AUTOMATIONSETUP."
43 exit
44fi
45
46# possible solution for $0 problem:
47# ${BASH_SOURCE[0]}
48# but has to be checked and tested more carefully
49# should solve at least problem with login-shell and with source
50source `dirname $0`/setup.$AUTOMATIONSETUP
51
52datetime=`date +%F-%H-%M-%S`
53
54
55# function to make sure that a directory is made
56function makedir()
57{
58 if [ ! -d $@ ]
59 then
60 mkdir -pv $@
61 if [ ! -d $@ ]
62 then
63 if ! [ "$processlog" = "" ]
64 then
65 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] ERROR could not make dir "$@ >> $processlog
66 else
67 echo "could not make dir "$@
68 fi
69 if ls $lockfile >/dev/null 2>&1
70 then
71 rm -v $lockfile
72 fi
73 exit
74 fi
75 fi
76}
77
78# logging paths for runlogs and processlog
79runlogpath=$logpath/run/`date +%Y/%m/%d`
80processlogpath=$logpath/processlog
81makedir $runlogpath
82makedir $processlogpath
83processlog=$processlogpath/process`date +%F`.log
84
85makedir $lockpath
86
87
88# function to provide proper logging in a single logfile ($processlog)
89function printprocesslog
90{
91 makedir $processlogpath
92 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
93}
94
95# function to exit a script properly
96function finish()
97{
98 if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
99 then
100 printprocesslog "DEBUG " `rm -v $lockfile`
101 fi
102 printprocesslog "DEBUG finished $0"
103 exit
104}
105
106
107# set checkvalue to ok at the beginning of the scripts
108check="ok"
109
110# setup for jobmanager:
111# log files (can't be defined in script itself, as script can run longer
112# than one day
113jmerrorlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`-error.log
114jmscriptlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`.log
115
116# check if rc-files are available
117if ! ls $steps >/dev/null
118then
119 echo "Can't find steps.rc ($steps)"
120 finish
121fi
122if ! ls $sqlrc >/dev/null
123then
124 echo "Can't find sql.rc ($sqlrc)"
125 finish
126fi
127
128# resetting values for jobmanager
129pno=0
130totalpno=0
131running=0
132queued=0
133runningscript=0
134queuedscript=0
135stillinqueue=0
136
137
138# alias (we cannot check the beginning of the line due to
139# color codes in filldotraw.C)
140alias 'intgrep'='grep -E -o \\\(int\\\)[0-9]+$ | grep -E -o [0-9]+'
141
142
143# in the following the functions, which are needed by several scripts, are
144# defined
145
146# function to check if a process is already locked
147# command line option can be used to execute something, e.g. 'continue'
148function checklock()
149{
150 if ! echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "`uname -a` > $lockfile 2>/dev/null
151 then
152 printprocesslog "WARN lockfile $lockfile exists"
153 $@
154 exit
155 else
156 printprocesslog "DEBUG created lockfile $lockfile"
157 fi
158}
159
160# print the current status values
161function printstatusvalues()
162{
163 echo "the current values are:"
164 echo " starttime=$starttime"
165 echo " stoptime=$stoptime"
166 echo " returncode=$returncode"
167 echo "-- check: -$check-"
168 echo ""
169}
170
171# get the db-setup from the sql.rc
172function getdbsetup()
173{
174 db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
175 pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
176 us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
177 ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
178# echo "setup: "
179# echo " db: "$db
180# echo " pw: "$pw
181# echo " us: "$us
182# echo " ho: "$ho
183}
184
185# function to get information from the setupfile $steps
186function getfromsetup()
187{
188 grep $1"[.]"$2":" $steps | grep -v '#' | sed -e "s/$1[.]$2://"
189}
190
191# function to get the needed information from the dependencies-file steps.rc
192function getstepinfo()
193{
194 getdbsetup
195 needs=( `getfromsetup $step "Needs"` )
196 noderestricted=`getfromsetup $step "NodeRestricted"`
197 prims=( `getfromsetup $step "Primaries"` )
198 maintable=`getfromsetup $step "MainTable" | sed -e "s/\ //g"`
199# echo " maintable: "$maintable
200# echo " needs: "${needs[@]}
201# echo " noderestricted: "$noderestricted
202# echo " prims: "${prims[@]}
203}
204
205# function to get the joins needed for the get/set status queries
206function getalljoins()
207{
208 # add table
209 query=$query" "$maintable"Status"
210 # add special join
211 query=$query" "`getfromsetup $maintable "SpecialJoin"`
212 # add join for step unless step is the same as maintable
213 if ! [ "$step" = "$maintable" ]
214 then
215 query=$query" LEFT JOIN "$step"Status USING("${prims[@]}") "
216 fi
217 # add joins for influences or needs
218 for otherstep in ${othersteps[@]}
219 do
220 if ! [ "$otherstep" = "$maintable" ]
221 then
222 query=$query" LEFT JOIN "$otherstep"Status USING("`getfromsetup $otherstep "Primaries"`") "
223 fi
224 done
225}
226
227# function to create the middle part of a query
228# which is identical for the functions getstatus() and gettodo()
229function getstatusquery()
230{
231 # add from which table the information is queried
232 query=$query" FROM "
233 othersteps=${needs[@]}
234 getalljoins
235 # add condition
236 query=$query" WHERE "
237 # add condition for step, i.e. step is not yet done
238 query=$query" ISNULL("$step"Status.fStartTime) "
239 query=$query" AND ISNULL("$step"Status.fStopTime) "
240 query=$query" AND ISNULL("$step"Status.fReturnCode) "
241 # add requirement for production host in case it is needed
242 if [ "$1 " != " " ]
243 then
244 query=$query" AND fProductionHostKEY=$2 "
245 fi
246 if ! echo $query | grep UPDATE >/dev/null 2>&1
247 then
248 query=$query" GROUP BY "${prims[@]}
249 fi
250 # add condition for needs, i.e. that step is done
251 for (( k=0 ; k < ${#needs[@]} ; k++ ))
252 do
253 if [ $k -eq 0 ]
254 then
255 query=$query" HAVING "
256 else
257 query=$query" AND "
258 fi
259 query=$query" COUNT(*)=COUNT(IF("
260 query=$query" NOT ISNULL("${needs[$k]}"Status.fStartTime) "
261 query=$query" AND NOT ISNULL("${needs[$k]}"Status.fStopTime) "
262 query=$query" AND ISNULL("${needs[$k]}"Status.fReturnCode) "
263 query=$query" , 1, NULL)) "
264 done
265}
266
267# function to get todolist
268# returns the next or the list of next steps
269function gettodo()
270{
271 # reset the variable for the number of the next step
272 process=
273 printprocesslog "DEBUG getting todo for step $step..."
274 getstepinfo
275 # get query
276 query=" SELECT "${prims[@]}
277 getstatusquery $2
278 # order by priority to the the number of the next step to be done
279 query=$query" ORDER BY "$step"Status.fPriority desc "
280 # add limitation in case only one or a limited number of
281 # processes should be executed
282 if [ "$1 " != " " ]
283 then
284 query=$query" limit 0, $1 "
285 fi
286 # print query
287 printprocesslog "DEBUG gettodo for step $step QUERY: "$query
288 # execute query
289 if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
290 then
291 printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
292 finish
293 fi
294 # get numbers of next step from mysql result
295 if [ "$process" = "" ]
296 then
297 printprocesslog "DEBUG => nothing to do"
298 finish
299 else
300 primaries=( $process )
301 num=`expr ${#primaries[@]} / ${#prims[@]} `
302 fi
303}
304
305# function to get the number of processes which still have to be done
306function getstatus()
307{
308 # reset the variable for the number of steps to be done
309 numproc=0
310 getstepinfo
311 # get query
312 query=" SELECT "${prims[@]}
313 getstatusquery $1
314 # print query
315 printprocesslog "DEBUG getstatus for step $step QUERY: "$query
316 # execute query
317 if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query " | wc -l`
318 then
319 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
320 echo `date +%F\ %T`" ERROR could not query number of processes from db (program: $program, function getstatus)"
321 continue
322 fi
323}
324
325# function to set status of a process in the db
326function setstatus()
327{
328 # remark:
329 # this function does not include the 'Default' flag
330 # for resetting steps
331
332 # for dowebplots (there are steps which have no entry in the DB)
333 if [ "$step" = "no" ]
334 then
335 return
336 fi
337
338 # reset status values
339 starttime=NULL
340 stoptime=NULL
341 returncode=NULL
342 # evaluate the status values
343 case $@ in
344 start) printprocesslog "DEBUG setstatus start"
345 starttime="Now()"
346 ;;
347 stop) case $check in
348 ok) printprocesslog "DEBUB setstatus stop - ok"
349 starttime=noreset
350 stoptime="Now()"
351 ;;
352 no) printprocesslog "DEBUG setstatus stop - nothing new"
353 check="ok"
354 ;;
355 *) printprocesslog "DEBUG setstatus stop - failed"
356 starttime=noreset
357 stoptime="Now()"
358 if [ "$check" == "" ]
359 then
360 returncode=1
361 else
362 returncode=$check
363 fi
364 check="ok"
365 ;;
366 esac
367 ;;
368 *) printprocesslog "ERROR function setstatus got wrong variable"
369 finish
370 ;;
371 esac
372
373 # get
374 getstepinfo
375
376 # get the influences from the steps.rc by evaluating the needs of all steps
377 othersteps=`grep $step $steps | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
378
379 # get query
380 query=" UPDATE "
381 getalljoins
382 # set the status values according to the new status of the step
383 query=$query" SET "
384 if ! [ "$starttime" = "noreset" ]
385 then
386 query=$query" "$step"Status.fStartTime=$starttime, "
387 fi
388 query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fReturnCode=$returncode "
389 # set also the status values of the influenced steps
390 for influence in $influences
391 do
392 query=$query", "$influence"Status.fStartTime=NULL "
393 query=$query", "$influence"Status.fStopTime=NULL "
394 query=$query", "$influence"Status.fReturnCode=NULL "
395 done
396 # give the condition for which step the status values have to be set
397 query=$query" WHERE "
398 if [ "$s" = "" ]
399 then
400 s=0
401 fi
402 query=$query" "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
403 for (( j=1 ; j < ${#prims[@]} ; j++ ))
404 do
405 query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
406 done
407 # print query
408 printprocesslog "DEBUG setstatus for step $step QUERY: "$query
409 # execute query
410 if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
411 then
412 printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
413 finish
414 fi
415}
416
417# function to send a mysql query
418function sendquery()
419{
420 getdbsetup
421 printprocesslog "DEBUG sendquery QUERY: "$query
422 if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
423 then
424 printprocesslog "ERROR could not query db (program: $program, function sendquery)"
425 return 1
426 fi
427 if [ "$val" = "NULL" ]
428 then
429 val=
430 fi
431 echo $val
432 return 0
433}
434
Note: See TracBrowser for help on using the repository browser.