diff --git a/Dockerfile b/Dockerfile index 7c9b6ac..3ec3b53 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,25 +1,19 @@ -FROM python:3.8-slim-buster +FROM python:3-alpine LABEL maintainer="mailto@wolfbolin.com" -# Why need these step? -# - fast mirror source -# - procps contains useful proccess control commands like: free, kill, pkill, ps, top -# - wget is quite basic tool -# - vim for online debugging -# - sync timezone -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 +ENV CRON_SIGNIN='30 9 * * *' +ENV MULTI=TRUE +ENV TZ=Asia/Shanghai +RUN adduser app -D +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \ + && apk add --no-cache tzdata -# 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 COPY . /var/app -RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip \ - && 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 +CMD ["python3", "./docker.py" ] diff --git a/README.md b/README.md index 0ec9bea..d0bd5c4 100644 --- a/README.md +++ b/README.md @@ -65,46 +65,21 @@ Docker的运行脚本基于Linux平台编写,暂未在Win平台测试。 -将本项目Clone至本地后,请先按照上述步骤添加或修改配置文件。随后运行`make-docker.sh`脚本本地构建Docker镜像,同时脚本会自动启动Docker容器(默认容器名为mihoyo-bbs),进行首次运行并显示Log信息。 - -```shell -sh make-docker.sh +将本项目Clone至本地后,请先按照上述步骤添加或修改配置文件。随后执行 ``` - -或手动执行 - +docker-compose up -d ``` -# 编译容器 -docker build -f Dockerfile --tag mihoyo-bbs:latest . +启动docker容器。 +  +容器运行成功后可用 ``` - +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文件夹中修改,修改的内容将实时同步在容器中。 -每次运行Docker容器后,容器内将自动按照参数执行签到活动,签到完成后容器将自动停止运行。手动重启容器即可重新运行脚本。 - -``` -docker restart mihoyo-bbs && docker logs -f mihoyo-bbs -``` - -关于每日定时,用户可在容器外部设计定时触发(启动)程序,每日定时运行脚本。 - -(若有需要可自行编写相关脚本通知完成状态 +每次运行Docker容器后,容器内将自动按照参数执行签到活动,签到完成后容器将默认在每天上午9:30运行一次,如果想自行修改时间可自行编辑`docker-compose.yml`文件中的`CRON_SIGNIN`,将其修改成想运行的时间。 ## 使用云函数运行 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0365d15 --- /dev/null +++ b/docker-compose.yml @@ -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" diff --git a/docker.py b/docker.py new file mode 100644 index 0000000..28b2ea1 --- /dev/null +++ b/docker.py @@ -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() diff --git a/requirements.txt b/requirements.txt index f613a62..f511043 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ httpx>=0.21.1 requests>=2.26.0 +crontab~=0.23.0