我正在尝试编写一个响应事件的“while true”bash 脚本。 (这是一个用于 git 推送通知的 Webhook,但目前还不太相关。)
我可以阻塞,等待下一个回调,运行构建,然后再次阻塞,但是如果我在构建中间收到回调,我会错过它,所以我尝试按如下方式解决它:
# Build loop, run in the background (see "done &" at the bottom)
while true
do
# If last build start is newer than last push do nothing.
if [ last-build-start -nt last-push ]
then
echo "last-build-start newer than last-push. Doing nothing."
sleep 3
continue
fi
log "Build triggered $(date)"
touch last-build-start
./build-all-branches.sh
log "Done. Waiting for next trigger..."
done &
# Terminate above loop upon Ctrl+C
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
# Listen for HTTP requests, do nothing but touch last-push
# to trigger a build in the build loop above.
while true
do
# Wait for hook to be called
echo -e "HTTP/1.1 200 OK\n\n $(date)" \
| nc -l 8787 -q 1 > /dev/null
# Trigger new build
echo "Triggered... touching last-push"
touch last-push
done
我遇到的问题是构建循环并不总是被触发。已touch last-push
执行,但这就像构建循环的另一个实例正在touch last-build-start
以一种活泼的方式启动并执行。
我是否不小心在上述代码中启动了多个构建循环?剧本中还有其他愚蠢的错误吗?