#! /bin/bash
env=$1  # 环境
if [ ${env} = "dev" ];then
  piaoquan_crawler_dir=/Users/wangkun/Desktop/crawler/piaoquan_crawler/
  profile_path=/etc/profile
  python=python3
  log_path=${piaoquan_crawler_dir}main/main_logs/process-$(date +%Y-%m-%d).log
elif [ ${env} = "hk" ];then
  piaoquan_crawler_dir=/root/piaoquan_crawler/
  profile_path=/etc/profile
  python=python3
  log_path=${piaoquan_crawler_dir}main/main_logs/process-$(date +%Y-%m-%d).log
else
  piaoquan_crawler_dir=/root/piaoquan_crawler/
  profile_path=/etc/profile
  python=/usr/bin/python3
  log_path=${piaoquan_crawler_dir}main/main_logs/process-$(date +%Y-%m-%d).log
fi

time=$(date +%H:%M:%S)
echo "$(date "+%Y-%m-%d %H:%M:%S") 开始监测爬虫进程状态" >> ${log_path}

echo "$(date "+%Y-%m-%d %H:%M:%S") 更新环境变量..." >> ${log_path}
cd ~ && source /etc/profile
echo "$(date "+%Y-%m-%d %H:%M:%S") 更新环境变量完成！" >> ${log_path}

# youtube定向爬虫策略
echo "$(date "+%Y-%m-%d %H:%M:%S") 正在监测 youtube定向爬虫策略 进程状态" >> ${log_path}
ps -ef | grep "run_youtube_follow.py" | grep -v "grep"
if [ "$?" -eq 1 ];then
  echo "$(date "+%Y-%m-%d_%H:%M:%S") 异常停止,正在重启!" >> ${log_path}
  if [ ${env} = "dev" ];then
    cd ${piaoquan_crawler_dir} && sh main/scheduling_main.sh ./youtube/youtube_main/run_youtube_follow.py --log_type="author" --crawler="youtube" --env="dev" youtube/logs/nohup-follow.log
  else
    cd ${piaoquan_crawler_dir} && /usr/bin/sh ./main/main.sh ./youtube/youtube_main/run_youtube_follow.py --log_type="author" --crawler="youtube" --strategy="定向爬虫策略" --oss_endpoint="hk" --env="hk" --machine="aliyun_hk" youtube/logs/nohup-follow.log
  fi
  echo "$(date "+%Y-%m-%d %H:%M:%S") 重启完成!" >> ${log_path}
else
  echo "$(date "+%Y-%m-%d %H:%M:%S") youtube定向爬虫策略 进程状态正常" >> ${log_path}
fi


# 微信指数监控
echo "$(date "+%Y-%m-%d %H:%M:%S") 正在监测 微信指数 bot 爬虫 进程状态" >> ${log_path}
ps -ef | grep "run_weixinzhishu_bot" | grep -v "grep"
if [ "$?" -eq 1 ];then
  echo "$(date "+%Y-%m-%d_%H:%M:%S") 异常停止,正在重启!" >> ${log_path}
  if [ ${env} = "dev" ];then
    cd ${piaoquan_crawler_dir} && sh main/scheduling_main.sh ./weixinzhishu/weixinzhishu_main/run_weixinzhishu_bot.py --log_type="bot" --crawler="weixinzhishu" --env="dev" weixinzhishu/logs/nohup-bot.log
  else
    cd ${piaoquan_crawler_dir} && /usr/bin/sh /root/piaoquan_crawler/main/scheduling_main.sh ./weixinzhishu/weixinzhishu_main/run_weixinzhishu_bot.py --log_type="bot" --crawler="weixinzhishu" --env="prod"  weixinzhishu/logs/nohup-bot.log
  fi
  echo "$(date "+%Y-%m-%d %H:%M:%S") 重启完成!" >> ${log_path}
else
  echo "$(date "+%Y-%m-%d %H:%M:%S") 微信指数 bot 爬虫 进程状态正常" >> ${log_path}
fi


# 删除日志
echo "$(date "+%Y-%m-%d %H:%M:%S") 开始清理 5 天前的日志文件" >> ${log_path}
find ${piaoquan_crawler_dir}main/main_logs/ -mtime +5 -name "*.log" -exec rm -rf {} \;
echo "$(date "+%Y-%m-%d %H:%M:%S") 日志文件清理完毕" >> ${log_path}
exit 0
