File indexing completed on 2026-04-20 07:59:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 ARGV="$@"
0014
0015
0016 set -a
0017
0018
0019
0020 userName=atlpan
0021
0022 groupName=zp
0023
0024
0025
0026 VIRTUAL_ENV=/opt/harvester
0027
0028
0029
0030 LOG_DIR=/var/log/panda
0031
0032
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
0042 HSTDOUT=${LOG_DIR}/panda_harvester_stdout.log
0043 HSTDERR=${LOG_DIR}/panda_harvester_stderr.log
0044
0045
0046
0047 checkInterval=1
0048 maxCheckAttempts=30
0049
0050
0051 uwsgiSocket="127.0.0.1:3334"
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 nProcesses=2
0064 nThreads=4
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 set +a
0075
0076
0077 if [ -r ${VIRTUAL_ENV}/bin/activate ]; then
0078 . ${VIRTUAL_ENV}/bin/activate
0079 fi
0080
0081
0082 if [ -r ${VIRTUAL_ENV}/etc/sysconfig/panda_harvester ]; then
0083 . ${VIRTUAL_ENV}/etc/sysconfig/panda_harvester
0084 fi
0085
0086
0087 PRESCRIPT="python ${SITE_PACKAGES_PATH}/pandaharvester/harvesterscripts/prescript.py -f ${LOCAL_INFO_FILE}"
0088
0089
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