完善auth_build.sh和auto_cleaner.sh,提升自动打包过程的稳定性

This commit is contained in:
Aaron Chen 2023-07-06 09:56:23 +08:00
parent cdb3c27617
commit bfe0af9ea2
4 changed files with 124 additions and 4 deletions

3
.gitignore vendored
View File

@ -7,4 +7,5 @@ dist
.user.ini
.env
*.log
*.log.*
*.log.*
*.lock

View File

@ -60,3 +60,17 @@ php start.php start -d
```
> 需要开放程序所在服务器的8787端口或者做一个反向代理到8787端口的http站点。
### 避免因自动打包进程意外终止而导致永远无法自动打包
```sh
crontab -e
```
添加下面一行
```crontab
*/10 * * * * /path/to/auto_cleaner.sh /path/to/ >/dev/null 2>&1
```
其中`/path/to/`要替换成实际路径。

View File

@ -1,7 +1,75 @@
#!/bin/bash
git pull origin master
log_file="auto_build.log"
lock_file="auto_build.lock"
timeout=600 # 超时时间,单位为秒
yarn install
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$$] $1" >> "$log_file"
}
yarn build
# 记录开始执行日志
log "start"
# 检查是否有其他进程正在执行
while [ -f "$lock_file" ]; do
another_pid=$(cat "$lock_file")
log "另一个进程[$another_pid]正在执行,等待中……"
sleep 5
timeout=$((timeout - 1))
if [ "$timeout" -le 0 ]; then
log "等待超时,退出"
exit 1
fi
done
# 创建锁文件
echo $$ > "$lock_file"
# 执行git pull
execute() {
log "$1"
result=$(eval "$1" 2>&1)
log "$result"
}
# 执行git pull
execute "git pull"
# 检查git pull执行结果
if [ $? -eq 0 ]; then
# git pull执行成功执行yarn install
execute "yarn install"
# 检查yarn install执行结果
if [ $? -eq 0 ]; then
# yarn install执行成功执行yarn build
execute "yarn build"
# 检查yarn build执行结果
if [ $? -ne 0 ]; then
# yarn build执行失败异常退出
log "yarn build执行失败"
rm "$lock_file"
exit 1
fi
else
# yarn install执行失败异常退出
log "yarn install执行失败"
rm "$lock_file"
exit 1
fi
else
# git pull执行失败异常退出
log "git pull执行失败"
rm "$lock_file"
exit 1
fi
# 记录完成执行日志
log "done"
# 删除锁文件并正常退出
rm "$lock_file"
exit 0

37
auto_cleaner.sh Normal file
View File

@ -0,0 +1,37 @@
#!/bin/bash
log_file="auto_cleaner.log"
lock_dir="${1:-./}"
lock_file="${lock_dir}auto_build.lock"
timeout=600 # 超时时间,单位为秒
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$log_file"
}
# 拼接锁文件路径
lock_file="${lock_dir}auto_build.lock"
# 判断锁文件是否存在
if [ -f "$lock_file" ]; then
# 获取锁文件中的进程ID
pid=$(cat "$lock_file")
# 判断系统中是否有该进程
if ps -p "$pid" >/dev/null; then
# 判断进程名称是否包含auto_build.sh
process_name=$(ps -o comm= -p "$pid")
if [[ "$process_name" == *"auto_build.sh"* ]]; then
log "进程[$pid]异常,释放$lock_file"
rm "$lock_file"
else
log "进程[$pid]验证正常"
fi
else
log "进程[$pid]异常,释放$lock_file"
rm "$lock_file"
fi
else
log "未发现$lock_file"
fi