@echo off
setlocal enabledelayedexpansion

set "crawler=%1"
set "path=%2"
set "log_type=%3"
set "env=%4"

if "%env%" == "dev" (
  set "piaoquan_crawler_dir=C:\Users\tzld\Desktop\piaoquan_crawler\"
  set "python=python.exe"
  set "log_path=!piaoquan_crawler_dir!main\main_logs\process-mq-!date:~0,4!-!date:~5,2!-!date:~8,2!.log"
) else if "%crawler%" == "zfqz" (
  set "piaoquan_crawler_dir=C:\Users\piaoquan\Desktop\piaoquan_crawler\"
  set "python=python.exe"
  set "log_path=!piaoquan_crawler_dir!main\main_logs\process-mq-!date:~0,4!-!date:~5,2!-!date:~8,2!.log"
) else (
  set "piaoquan_crawler_dir=C:\root\piaoquan_crawler\"
  set "python=python.exe"
  set "log_path=!piaoquan_crawler_dir!main\main_logs\process-mq-!date:~0,4!-!date:~5,2!-!date:~8,2!.log"
)

echo run_!crawler!_!log_type!.py
echo topic:!crawler!_!log_type!_!env!
echo GID:!crawler!_!log_type!_!env!

set "time=!time!"
echo !time! 开始监测爬虫进程状态 >> !log_path!

echo !time! 开始更新代码 >> !log_path!
cd !piaoquan_crawler_dir! && git pull origin master --force
echo !time! 代码更新完成！ >> !log_path!

echo !time! 正在监测 !crawler!_!log_type! 进程状态 >> !log_path!
tasklist /v | findstr "run_!crawler!_!log_type!.py" | findstr /v "findstr"
if "!errorlevel!" == "1" (
  echo !time! 异常停止,正在重启! >> !log_path!
  cd !piaoquan_crawler_dir! && start "Crawler" "!python!" -u !path!\!path!_main\run_!crawler!_!log_type!.py --log_type="!log_type!" --crawler="!path!" --topic_name="!crawler!_!log_type!_!env!" --group_id="!crawler!_!log_type!_!env!" --env="!env!" >> !path!\logs\!log_type!-shell.log 2>&1
  echo !time! 重启完成! >> !log_path!
) else (
  echo !time! !crawler!_!log_type! 进程状态正常 >> !log_path!
)

echo !time! 开始清理 10 天前的日志文件 >> !log_path!
forfiles /p !piaoquan_crawler_dir!main\main_logs\ /m *.log /d -10 /c "cmd /c del @file"
echo !time! 日志文件清理完毕 >> !log_path!
exit /b 0
