Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-20 07:59:01

0001 #!/bin/sh
0002 #
0003 # chkconfig: - 85 15
0004 #
0005 # description: Panda Harvester uWSGI
0006 # processname: uwsgi
0007 # config: ${VIRTUAL_ENV}/etc/sysconfig/panda_harvester
0008 # pidfile: ${LOG_DIR}/panda_harvester.pid
0009 
0010 # When multiple arguments are given, only the error from the
0011 # last one is reported.
0012 #
0013 ARGV="$@"
0014 #
0015 
0016 set -a
0017 #======= START CONFIGURATION SECTION ==========================
0018 # user and group to run harvester uWSGI
0019 # FIXME
0020 userName=atlpan
0021 # FIXME
0022 groupName=zp
0023 
0024 # setup python and virtual env
0025 # FIXME
0026 VIRTUAL_ENV=/opt/harvester
0027 
0028 # set log directory
0029 # FIXME
0030 LOG_DIR=/var/log/panda
0031 
0032 # pid and local info files
0033 if [[ -z "${PANDA_LOCK_DIR}" ]]; then
0034     PIDFILE=${LOG_DIR}/panda_harvester.pid
0035     LOCAL_INFO_FILE=${LOG_DIR}/.harvester_info
0036 else
0037     PIDFILE=${PANDA_LOCK_DIR}/panda_harvester.pid
0038     LOCAL_INFO_FILE=${PANDA_LOCK_DIR}/.harvester_info
0039 fi
0040 
0041 # log files
0042 HSTDOUT=${LOG_DIR}/panda_harvester_stdout.log
0043 HSTDERR=${LOG_DIR}/panda_harvester_stderr.log
0044 
0045 
0046 # check interval in second and max number of check attempts of process handle
0047 checkInterval=1
0048 maxCheckAttempts=30
0049 
0050 # uWSGI socket address
0051 uwsgiSocket="127.0.0.1:3334"
0052 
0053 # http and/or https router
0054 # Make uWSGI spawn an http proxy process. Use this only when http is needed and
0055 # no other frontend proxy/webserver (nginx, apache, etc.) is set for harvester
0056 # For http, only address required
0057 # For https, the concatenated string of address,hostcert,hostkey should be set
0058 # and more optional string (,cipher_suite,ca_cert) can be concatenated after
0059 #httpRouter=":25080"
0060 #httpsRouter=":25443,hostcert.pem,hostkey.pem,HIGH,ca-bundle.pem"
0061 
0062 # number of uwsgi processes and threads
0063 nProcesses=2
0064 nThreads=4
0065 
0066 # uWSGI configuration file
0067 # Path of the configuration file in .ini format. Optional.
0068 # If set, uWSGI will run according to the configuration file alone and the
0069 # env variables set above will not take effect. However, one can still
0070 # use them in the configuration file in form of $(ENV_VAR) as well.
0071 #uwsgiConfig=${VIRTUAL_ENV}/etc/panda/panda_harvester-uwsgi.ini
0072 
0073 #======= END CONFIGURATION SECTION ============================
0074 set +a
0075 
0076 # load module python
0077 if [ -r ${VIRTUAL_ENV}/bin/activate ]; then
0078     . ${VIRTUAL_ENV}/bin/activate
0079 fi
0080 
0081 # source harvester env variables
0082 if [ -r ${VIRTUAL_ENV}/etc/sysconfig/panda_harvester ]; then
0083     . ${VIRTUAL_ENV}/etc/sysconfig/panda_harvester
0084 fi
0085 
0086 # prescript
0087 PRESCRIPT="python ${SITE_PACKAGES_PATH}/pandaharvester/harvesterscripts/prescript.py -f ${LOCAL_INFO_FILE}"
0088 
0089 # handle uWSGI program
0090 if [ "x${uwsgiConfig}y" != "xy" ]; then
0091     PROGNAME="uwsgi ${uwsgiConfig}"
0092 else
0093     [ "x${httpRouter}y" != "xy" ] && httpRouter_option="--http ${httpRouter}"
0094     [ "x${httpsRouter}y" != "xy" ] && httpsRouter_option="--https ${httpsRouter}"
0095     PROGNAME="uwsgi --socket ${uwsgiSocket} ${httpRouter_option} ${httpsRouter_option}
0096         --master --lazy-apps --chdir ${VIRTUAL_ENV}
0097         --wsgi-file ${SITE_PACKAGES_PATH}/pandaharvester/harvesterbody/master.py
0098         --processes ${nProcesses} --threads ${nThreads} --worker-reload-mercy 1
0099         --safe-pidfile ${PIDFILE} --uid ${userName} --gid ${groupName}
0100         --post-buffering 32768 --buffer 32768"
0101 fi
0102 
0103 
0104 function check_pidfile_running () {
0105     itry=0
0106     while [ "$itry" -lt "$maxCheckAttempts" ]; do
0107         itry=$((itry + 1))
0108         sleep $checkInterval
0109         if [ ! -f ${PIDFILE} ]; then
0110             continue
0111         fi
0112         if ps -o pid -p `cat ${PIDFILE}` >& /dev/null ; then
0113             return 0
0114         fi
0115     done
0116     echo "ERROR: check_pidfile_running timeout"
0117     return 1
0118 }
0119 
0120 function check_pidfile_killed () {
0121     if [ ! -f ${PIDFILE} ]; then
0122         echo "ERROR: pidfile:${PIDFILE} does not exist"
0123         return 1
0124     fi
0125     itry=0
0126     while [ "$itry" -lt "$maxCheckAttempts" ]; do
0127         itry=$((itry + 1))
0128         sleep $checkInterval
0129         if ! ps -o pid -p `cat ${PIDFILE}` >& /dev/null ; then
0130             return 0
0131         fi
0132     done
0133     echo "ERROR: check_pidfile_killed timeout"
0134     return 1
0135 }
0136 
0137 function check_pidfile_children_respawn () {
0138     if [ ! -f ${PIDFILE} ]; then
0139         echo "ERROR: pidfile:${PIDFILE} does not exist"
0140         return 1
0141     fi
0142     itry=0
0143     while [ "$itry" -lt "$maxCheckAttempts" ]; do
0144         itry=$((itry + 1))
0145         sleep $checkInterval
0146         _old_processes=`echo $@`
0147         _processes=$(ps -o pid --ppid `cat ${PIDFILE}`)
0148         ERROR=$?
0149         if [ $ERROR = 0 ]; then
0150             _old_processes=$(python -c "print(' '.join('$_old_processes'.split()))")
0151             _processes=$(python -c "print(' '.join('`echo $_processes`'.split()))")
0152             _python_command="if set('`echo $_old_processes`'.split()).intersection(set('`echo $_processes`'.split())) == set(['PID']) and len('`echo $_processes`'.split()) >= int(${nProcesses}) + bool('${httpRouter_option}'): print(0)"
0153             _ret=$(python -c "$_python_command")
0154             [ "$_ret" == "0" ] && return 0
0155         fi
0156     done
0157     echo "ERROR: check_pidfile_children_running timeout"
0158     return 1
0159 }
0160 
0161 
0162 case $ARGV in
0163 start)
0164     if [ -f ${PIDFILE} ]; then
0165         echo ERROR: Harvester is already running with pidfile:${PIDFILE}
0166     else
0167         echo Run harvester prescript
0168         ${PRESCRIPT}
0169         echo Start Harvester
0170         ${PROGNAME} >> ${HSTDOUT} 2>> ${HSTDERR} &
0171         check_pidfile_running
0172         ERROR=$?
0173         if [ $ERROR = 0 ]; then
0174         echo "succeeded"
0175             break
0176         else
0177             echo "failed with exit code $ERROR"
0178             [ -f ${PIDFILE} ] && rm -f ${PIDFILE}
0179         fi
0180     fi
0181     ;;
0182 runfg)
0183     echo Run harvester prescript
0184     ${PRESCRIPT}
0185     echo Run Harvester in foreground
0186     ${PROGNAME} >> ${HSTDOUT} 2>> ${HSTDERR}
0187     rm -f ${PIDFILE}
0188     ;;
0189 stop)
0190     if [ ! -f ${PIDFILE} ]; then
0191         echo "WANRING: pidfile:${PIDFILE} does not exist. Nothing done"
0192     else
0193         echo Stop Harvester
0194         uwsgi --stop ${PIDFILE}
0195         check_pidfile_killed
0196         ERROR=$?
0197         if [ $ERROR = 0 ]; then
0198             echo "succeeded"
0199             rm -f ${PIDFILE}
0200         else
0201             echo "failed with exit code $ERROR"
0202         fi
0203     fi
0204     rm -f ${PIDFILE}
0205     ;;
0206 reload)
0207     if [ ! -f ${PIDFILE} ]; then
0208         echo "ERROR: pidfile:${PIDFILE} does not exist"
0209     else
0210         echo Reload Harvester
0211         _old_processes=$(ps -o pid --ppid `cat ${PIDFILE}`)
0212         uwsgi --reload ${PIDFILE}
0213         check_pidfile_children_respawn $_old_processes
0214         ERROR=$?
0215         if [ $ERROR = 0 ]; then
0216             echo "succeeded"
0217         else
0218             echo "failed with exit code $ERROR"
0219         fi
0220     fi
0221     ;;
0222 *)
0223     echo ERROR: unknown command $ARGV
0224     ERROR=1
0225 esac
0226 
0227 exit $ERROR