This commit is contained in:
Womsxd 2022-04-20 13:02:53 +08:00
commit 4c019eef61
No known key found for this signature in database
GPG Key ID: 0FE76418EE689B68
5 changed files with 94 additions and 53 deletions

View File

@ -1,25 +1,19 @@
FROM python:3.8-slim-buster FROM python:3-alpine
LABEL maintainer="mailto@wolfbolin.com" LABEL maintainer="mailto@wolfbolin.com"
# Why need these step? ENV CRON_SIGNIN='30 9 * * *'
# - fast mirror source ENV MULTI=TRUE
# - procps contains useful proccess control commands like: free, kill, pkill, ps, top ENV TZ=Asia/Shanghai
# - wget is quite basic tool RUN adduser app -D
# - vim for online debugging RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
# - sync timezone && apk add --no-cache tzdata
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list \
&& sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list \
&& apt-get update && apt-get install -y --no-install-recommends procps wget vim \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# Project environment WORKDIR /tmp
ADD requirements.txt ./
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
USER app
WORKDIR /var/app WORKDIR /var/app
COPY . /var/app COPY . /var/app
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip \ CMD ["python3", "./docker.py" ]
&& pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
# Docker operation parameters
ENV MULTI TRUE
CMD if [ $MULTI==TRUE ];then python main_multi.py autorun;else python main.py;fi

View File

@ -65,46 +65,21 @@
Docker的运行脚本基于Linux平台编写暂未在Win平台测试。 Docker的运行脚本基于Linux平台编写暂未在Win平台测试。
将本项目Clone至本地后请先按照上述步骤添加或修改配置文件。随后运行`make-docker.sh`脚本本地构建Docker镜像同时脚本会自动启动Docker容器默认容器名为mihoyo-bbs进行首次运行并显示Log信息。 将本项目Clone至本地后请先按照上述步骤添加或修改配置文件。随后执行
```shell
sh make-docker.sh
``` ```
docker-compose up -d
或手动执行
``` ```
# 编译容器 启动docker容器。
docker build -f Dockerfile --tag mihoyo-bbs:latest .  
容器运行成功后可用
``` ```
docker-compose logs -f
``` ```
# 运行容器(默认自动多配置文件) 命令来查看程序输出。
docker run -itd \  
--name mihoyo-bbs \
--log-opt max-size=1m \
-v $(pwd):/var/app \
mihoyo-bbs:latest
# 运行容器直接运行main.py
docker run -itd \
--name mihoyo-bbs \
--log-opt max-size=1m \
-v $(pwd):/var/app \
-e MULTI=FALSE \
mihoyo-bbs:latest
```
若需要添加配置文件或修改配置文件可直接在主机config文件夹中修改修改的内容将实时同步在容器中。 若需要添加配置文件或修改配置文件可直接在主机config文件夹中修改修改的内容将实时同步在容器中。
每次运行Docker容器后容器内将自动按照参数执行签到活动签到完成后容器将自动停止运行。手动重启容器即可重新运行脚本。 每次运行Docker容器后容器内将自动按照参数执行签到活动签到完成后容器将默认在每天上午9:30运行一次如果想自行修改时间可自行编辑`docker-compose.yml`文件中的`CRON_SIGNIN`,将其修改成想运行的时间。
```
docker restart mihoyo-bbs && docker logs -f mihoyo-bbs
```
关于每日定时,用户可在容器外部设计定时触发(启动)程序,每日定时运行脚本。
(若有需要可自行编写相关脚本通知完成状态
## 使用云函数运行 ## 使用云函数运行

14
docker-compose.yml Normal file
View File

@ -0,0 +1,14 @@
version: "3"
services:
mihoyo-bbs:
image: darkatse/mihoyo-bbs
environment:
- CRON_SIGNIN=30 9 * * *
- MULTI=TRUE
volumes:
- .:/var/app
logging:
driver: "json-file"
options:
max-size: "1m"

57
docker.py Normal file
View File

@ -0,0 +1,57 @@
import datetime
import os
import logging
import signal
import time
#from settings import *
from crontab import CronTab
time_format = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S')
log = logging
def stop_me(_signo, _stack):
log.info("Docker container has stoped....")
exit(-1)
def main():
signal.signal(signal.SIGINT, stop_me)
log.info("使用DOCKER运行米游社签到")
env = os.environ
cron_signin = env["CRON_SIGNIN"]
cron = CronTab(cron_signin, loop=True, random_seconds=True)
def next_run_time():
nt = datetime.datetime.now().strftime(time_format)
delayt = cron.next(default_utc=False)
nextrun = datetime.datetime.now() + datetime.timedelta(seconds=delayt)
nextruntime = nextrun.strftime(time_format)
log.info(f"Current running datetime: {nt}")
log.info(f"Next run datetime: {nextruntime}")
def sign():
log.info("Starting signing")
multi = env["MULTI"]
if multi:
os.system("python3 ./main_multi.py autorun")
else:
os.system("python3 ./main.py")
sign()
next_run_time()
while True:
ct = cron.next(default_utc=False)
time.sleep(ct)
sign()
next_run_time()
if __name__ == '__main__':
main()

View File

@ -1,2 +1,3 @@
httpx>=0.21.1 httpx>=0.21.1
requests>=2.26.0 requests>=2.26.0
crontab~=0.23.0