#! /bin/sh

### BEGIN INIT INFO
# Description: 重启部署脚本
#执行教程：
#1.放到/server/research/gitPro.sh
#2.chmod -R 755 /server/research/gitPro.sh
#3.  直接执行 /server/research/gitPro.sh restart

### END INIT INFO
# 主目录 脚本所在目录
master_home_prefix=/server/research
# research 安装目录 代码根目录
research_prefix=/server/research/spider-flow-cloud
# maven 目录
maven_prefix=/usr/local/apache-maven-3.8.5
currentTimeStamp=''
jar_name=research-web.jar

# JVM参数
JVM_OPTS="-Duser.timezone=Asia/Shanghai"
#jar 运行日志打印
LOG_PATH=/server/research/nohup.out
#jar 运行pid进程
JAVAPID=/server/research/rjava.pid
function getTime() {
    current=`date "+%Y-%m-%d %H:%M:%S"`
    timeStamp=`date -d "$current" +%s`
    #将current转换为时间戳，精确到毫秒
    currentTimeStamp=$((timeStamp*1000+`date "+%N"`/1000000))
    return $currentTimeStamp
}

case "$1" in
  start)
    getTime
    echo "start research service"
    echo "====================================>>>>>>>>"
    #进入项目目录
    cd $research_prefix
    #拉取代码
    git pull
    echo "拉取最新代码"
    echo "====================================>>>>>>>>"
    #maven 打包
    $maven_prefix/bin/mvn package
    echo "正在maven打包"
    echo "====================================>>>>>>>>"

    # 判断旧的jar包是否存在  #!
    if [ -f "$master_home_prefix/$jar_name" ]; then
      echo "备份上一次的jar包"
      echo "====================================>>>>>>>>"
      #存在，做备份
      mv $master_home_prefix/$jar_name $master_home_prefix/$jar_name-$currentTimeStamp
    fi
    #移动jar包
    echo "复制新的jar包至 $master_home_prefix/$jar_name"
    echo "====================================>>>>>>>>"
    mv $research_prefix/research-web/target/$jar_name $master_home_prefix/$jar_name
    #启动jar包
    echo "启动jar包"
    echo "====================================>>>>>>>>"
    nohup java -jar  $JVM_OPTS $master_home_prefix/$jar_name > $LOG_PATH 2>&1 & echo $! > $JAVAPID &
    #追踪打印日志
    tail -f $LOG_PATH
  ;;
  stop)
    echo "stop research service"
    echo "====================================>>>>>>>>"
    echo "停止jar进程 `cat $JAVAPID`"
    echo "====================================>>>>>>>>"
    kill -9 `cat $JAVAPID`
    echo "====================================>>>>>>>>"
    echo "已停止"
  ;;
  restart)
    $0 stop
    echo "====================================>>>>>>>>"
    $0 start
    echo "====================================>>>>>>>>"
  ;;
esac



