refactor(deploy): 重构部署流程并添加容器锁机制

- 修改 Docker Compose 配置,简化网络设置
- 新增 Docker 入口脚本,实现容器锁机制
- 更新 Dockerfile,使用新的入口脚本
- 添加 Makefile,简化部署流程
- 优化 Python 脚本,去除冗余的循环结构
This commit is contained in:
danial
2024-12-19 00:44:56 +08:00
parent 911db7d319
commit b56184c673
5 changed files with 32 additions and 24 deletions

3
Makefile Normal file
View File

@@ -0,0 +1,3 @@
deploy:
@echo "Deploying to Docker Compose"
VERSION=0.1 DIR=123 docker compose -f ./deploy/docker-compose.yml up -d

View File

@@ -15,4 +15,4 @@ RUN pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/we
pip install -r requirements.txt --no-cache-dir && \
pip cache purge
CMD ["python","-u", "main.py"]
CMD ["./deploy/docker-entrypoint.sh"]

View File

@@ -3,13 +3,7 @@ services:
container_name: kami_redeem_itunes
image: itunes_kami:latest
build:
context: ..
context: ../.
dockerfile: ./deploy/Dockerfile
networks:
- 1panel-network
labels:
createdBy: Developer
networks:
1panel-network:
external: true

View File

@@ -0,0 +1,15 @@
#!/bin/bash
# 定义锁文件路径
LOCKFILE="/tmp/my_python_task.lock"
while true; do
# 使用 flock -n 尝试获取锁,如果无法获取则立即退出
(
echo "$(date): Running Python script..."
flock -n 200 || exit 1
# 如果成功获取锁,则运行 Python 脚本
python -u main.py
) 200>$LOCKFILE
sleep 5
done

View File

@@ -181,7 +181,6 @@ def run_task():
"""
# 注册信号处理程序
has_been_console = False
while True:
master_node_service = MasterNodeService()
# 1s获取两次订单
master_order = master_node_service.query_order()
@@ -194,8 +193,6 @@ def run_task():
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\t暂时没有订单")
if datetime.now().second % 10 != 0 and has_been_console:
has_been_console = False
time.sleep(1)
def run():
# signal.signal(signal.SIGINT, signal_handler)
@@ -215,4 +212,3 @@ def run():
process.close()
process = Process(target=run_task, args=(), daemon=True)
process.start()
time.sleep(5)