From 4f0b1e52c6c4a0cf9ad31b571971b0f2e57318ba Mon Sep 17 00:00:00 2001 From: darkatse Date: Sun, 3 Apr 2022 13:54:16 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E5=AF=B9docker?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 32 +++++++++++--------------- docker-compose.yml | 10 ++++++++ docker.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 docker-compose.yml create mode 100644 docker.py 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/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9e8f307 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3" + +services: + mihoyo-bbs: + image: darkatse/mihoyo-bbs + environment: + - CRON_SIGNIN=30 9 * * * + - MULTI=TRUE + volumes: + - .:/var/app 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..00cab0b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ httpx>=0.21.1 requests>=2.26.0 +crontab~=0.22.9 From 70e0264ecb6e9f77f229b1eb4d57b89347136419 Mon Sep 17 00:00:00 2001 From: Darkatse <39111369+Darkatse@users.noreply.github.com> Date: Sun, 3 Apr 2022 14:29:31 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=AE=80=E5=8D=95=E6=9B=B4=E6=96=B0README?= =?UTF-8?q?=E4=B8=ADdocker=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 43 +++++++++---------------------------------- docker-compose.yml | 4 ++++ 2 files changed, 13 insertions(+), 34 deletions(-) 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 index 9e8f307..3490cbe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,3 +8,7 @@ services: - MULTI=TRUE volumes: - .:/var/app + logging: + driver: "json-file" + options: + max-size: "1m" From 0cfe004239ec645094150b970a564c08670f5a48 Mon Sep 17 00:00:00 2001 From: Darkatse <39111369+Darkatse@users.noreply.github.com> Date: Sun, 3 Apr 2022 14:34:10 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E7=BC=A9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3490cbe..0365d15 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,13 +2,13 @@ version: "3" services: mihoyo-bbs: - image: darkatse/mihoyo-bbs - environment: - - CRON_SIGNIN=30 9 * * * - - MULTI=TRUE - volumes: - - .:/var/app - logging: - driver: "json-file" - options: + image: darkatse/mihoyo-bbs + environment: + - CRON_SIGNIN=30 9 * * * + - MULTI=TRUE + volumes: + - .:/var/app + logging: + driver: "json-file" + options: max-size: "1m" From cc5efa2a20262485d92ad66104f0873991ffc168 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Apr 2022 16:09:12 +0000 Subject: [PATCH 4/4] Update crontab requirement from ~=0.22.9 to ~=0.23.0 Updates the requirements on [crontab](https://github.com/josiahcarlson/parse-crontab) to permit the latest version. - [Release notes](https://github.com/josiahcarlson/parse-crontab/releases) - [Commits](https://github.com/josiahcarlson/parse-crontab/commits/0.23.0) --- updated-dependencies: - dependency-name: crontab dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 00cab0b..f511043 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ httpx>=0.21.1 requests>=2.26.0 -crontab~=0.22.9 +crontab~=0.23.0