service.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/bin/bash
  2. #
  3. # Copyright 2019 The FATE Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. if [[ -z "${FATE_PROJECT_BASE}" ]]; then
  18. PROJECT_BASE=$(cd "$(dirname "$0")";cd ../;cd ../;pwd)
  19. else
  20. PROJECT_BASE="${FATE_PROJECT_BASE}"
  21. fi
  22. FATE_FLOW_BASE=${PROJECT_BASE}/fateflow
  23. echo "PROJECT_BASE: "${PROJECT_BASE}
  24. # source init_env.sh
  25. INI_ENV_SCRIPT=${PROJECT_BASE}/bin/init_env.sh
  26. if test -f "${INI_ENV_SCRIPT}"; then
  27. source ${PROJECT_BASE}/bin/init_env.sh
  28. echo "PYTHONPATH: "${PYTHONPATH}
  29. else
  30. echo "file not found: ${INI_ENV_SCRIPT}"
  31. exit
  32. fi
  33. log_dir=${FATE_FLOW_BASE}/logs
  34. module=fate_flow_server.py
  35. parse_yaml() {
  36. local prefix=$2
  37. local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
  38. sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
  39. -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
  40. awk -F$fs '{
  41. indent = length($1)/2;
  42. vname[indent] = $2;
  43. for (i in vname) {if (i > indent) {delete vname[i]}}
  44. if (length($3) > 0) {
  45. vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
  46. printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
  47. }
  48. }'
  49. }
  50. getport() {
  51. service_conf_path=${PROJECT_BASE}/conf/service_conf.yaml
  52. if test -f "${service_conf_path}"; then
  53. echo "found service conf: ${service_conf_path}"
  54. eval $(parse_yaml ${service_conf_path} "service_config_")
  55. echo "fate flow http port: ${service_config_fateflow_http_port}, grpc port: ${service_config_fateflow_grpc_port}"
  56. echo
  57. else
  58. echo "service conf not found: ${service_conf_path}"
  59. exit
  60. fi
  61. }
  62. getport
  63. getpid() {
  64. echo "check process by http port and grpc port"
  65. pid1=`lsof -i:${service_config_fateflow_http_port} | grep 'LISTEN' | awk 'NR==1 {print $2}'`
  66. pid2=`lsof -i:${service_config_fateflow_grpc_port} | grep 'LISTEN' | awk 'NR==1 {print $2}'`
  67. if [[ -n ${pid1} && "x"${pid1} = "x"${pid2} ]];then
  68. pid=$pid1
  69. elif [[ -z ${pid1} && -z ${pid2} ]];then
  70. pid=
  71. fi
  72. }
  73. mklogsdir() {
  74. if [[ ! -d $log_dir ]]; then
  75. mkdir -p $log_dir
  76. fi
  77. }
  78. status() {
  79. getpid
  80. if [[ -n ${pid} ]]; then
  81. echo "status:`ps aux | grep ${pid} | grep -v grep`"
  82. lsof -i:${service_config_fateflow_http_port} | grep 'LISTEN'
  83. lsof -i:${service_config_fateflow_grpc_port} | grep 'LISTEN'
  84. else
  85. echo "service not running"
  86. fi
  87. }
  88. start() {
  89. getpid
  90. if [[ ${pid} == "" ]]; then
  91. mklogsdir
  92. if [[ $1x == "front"x ]];then
  93. export FATE_PROJECT_BASE=${PROJECT_BASE}
  94. exec python ${FATE_FLOW_BASE}/python/fate_flow/fate_flow_server.py >> "${log_dir}/console.log" 2>>"${log_dir}/error.log"
  95. unset FATE_PROJECT_BASE
  96. else
  97. export FATE_PROJECT_BASE=${PROJECT_BASE}
  98. nohup python ${FATE_FLOW_BASE}/python/fate_flow/fate_flow_server.py >> "${log_dir}/console.log" 2>>"${log_dir}/error.log" &
  99. unset FATE_PROJECT_BASE
  100. fi
  101. for((i=1;i<=100;i++));
  102. do
  103. sleep 0.1
  104. getpid
  105. if [[ -n ${pid} ]]; then
  106. echo "service start sucessfully. pid: ${pid}"
  107. return
  108. fi
  109. done
  110. if [[ -n ${pid} ]]; then
  111. echo "service start sucessfully. pid: ${pid}"
  112. else
  113. echo "service start failed, please check ${log_dir}/error.log and ${log_dir}/console.log"
  114. fi
  115. else
  116. echo "service already started. pid: ${pid}"
  117. fi
  118. }
  119. stop() {
  120. getpid
  121. if [[ -n ${pid} ]]; then
  122. echo "killing: `ps aux | grep ${pid} | grep -v grep`"
  123. for((i=1;i<=100;i++));
  124. do
  125. sleep 0.1
  126. kill ${pid}
  127. getpid
  128. if [[ ! -n ${pid} ]]; then
  129. echo "killed by SIGTERM"
  130. return
  131. fi
  132. done
  133. kill -9 ${pid}
  134. if [[ $? -eq 0 ]]; then
  135. echo "killed by SIGKILL"
  136. else
  137. echo "kill error"
  138. fi
  139. else
  140. echo "service not running"
  141. fi
  142. }
  143. case "$1" in
  144. start)
  145. start
  146. status
  147. ;;
  148. starting)
  149. start front
  150. ;;
  151. stop)
  152. stop
  153. ;;
  154. status)
  155. status
  156. ;;
  157. restart)
  158. stop
  159. sleep 10
  160. start
  161. status
  162. ;;
  163. *)
  164. echo "usage: $0 {start|stop|status|restart}"
  165. exit -1
  166. esac