diff --git a/Dockerfile b/Dockerfile index 35e7c2c..6d73286 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.9-alpine # 设置时区为 GMT+8 -RUN apk add --no-cache tzdata +RUN apk add --no-cache tzdata ca-certificates ENV TZ=Asia/Shanghai # 设置工作目录 diff --git a/docker-compose.yml b/docker-compose.yml index e424ed7..b1665e7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,13 @@ -version: '3.8' services: nodeseek-signin: build: . + image: nodeseek-signin:latest + container_name: nodeseek-signin + command: ["python", "scheduler.py"] environment: - IN_DOCKER=true env_file: - .env volumes: - ./cookie:/app/cookie - restart: always \ No newline at end of file + restart: always diff --git a/scheduler.py b/scheduler.py index 08d4b3a..37ad80f 100644 --- a/scheduler.py +++ b/scheduler.py @@ -1,4 +1,5 @@ import os +import sys import time import datetime import random @@ -6,6 +7,10 @@ import subprocess import re from datetime import timezone, timedelta +# 测试程序时使用 +# from dotenv import load_dotenv +# load_dotenv() + GMT8 = timezone(timedelta(hours=8)) def get_run_config(): @@ -19,17 +24,17 @@ def get_run_config(): run_at_env = os.environ.get('RUN_AT', '08:00-10:59') if re.fullmatch(r'\d{2}:\d{2}', run_at_env): - print(f"检测到固定时间模式: {run_at_env}") + print(f"检测到固定时间模式: {run_at_env}", flush=True) return 'fixed', run_at_env if re.fullmatch(r'\d{2}:\d{2}-\d{2}:\d{2}', run_at_env): - print(f"检测到随机时间范围模式: {run_at_env}") + print(f"检测到随机时间范围模式: {run_at_env}", flush=True) return 'range', run_at_env if os.environ.get('RUN_AT'): - print(f"警告: 环境变量 RUN_AT 的格式 '{run_at_env}' 无效。") + print(f"警告: 环境变量 RUN_AT 的格式 '{run_at_env}' 无效。", flush=True) - print("将使用默认随机时间范围 '08:00-10:59'。") + print("将使用默认随机时间范围 '08:00-10:59'。", flush=True) return 'range', '08:00-10:59' def calculate_next_run_time(mode, value): @@ -78,26 +83,26 @@ def run_checkin_task(): """ 执行 nodeseek_sign.py 脚本。 """ - print(f"[{datetime.datetime.now(GMT8).strftime('%Y-%m-%d %H:%M:%S')}] 开始执行签到任务...") + print(f"[{datetime.datetime.now(GMT8).strftime('%Y-%m-%d %H:%M:%S')}] 开始执行签到任务...", flush=True) try: - subprocess.run(["python", "nodeseek_sign.py"], check=True) - print(f"[{datetime.datetime.now(GMT8).strftime('%Y-%m-%d %H:%M:%S')}] 签到任务执行完毕。") + subprocess.run([sys.executable, "nodeseek_sign.py"], check=True) + print(f"[{datetime.datetime.now(GMT8).strftime('%Y-%m-%d %H:%M:%S')}] 签到任务执行完毕。", flush=True) except FileNotFoundError: - print("错误: 'nodeseek_sign.py' 未找到。请确保它与 scheduler.py 位于同一目录。") + print("错误: 'nodeseek_sign.py' 未找到。请确保它与 scheduler.py 位于同一目录。", flush=True) except subprocess.CalledProcessError as e: - print(f"签到任务执行失败,返回码: {e.returncode}") + print(f"签到任务执行失败,返回码: {e.returncode}", flush=True) except Exception as e: - print(f"执行签到任务时发生未知错误: {e}") + print(f"执行签到任务时发生未知错误: {e}", flush=True) def main(): """ 主调度循环。 """ - print("调度器启动...") + print("调度器启动...", flush=True) mode, value = get_run_config() - print(f"调度模式: '{mode}', 配置值: '{value}'") + print(f"调度模式: '{mode}', 配置值: '{value}'", flush=True) - run_checkin_task() + # run_checkin_task() # 启动时执行,用于测试程序 while True: next_run_time = calculate_next_run_time(mode, value) @@ -105,13 +110,13 @@ def main(): sleep_duration = (next_run_time - now).total_seconds() if sleep_duration > 0: - print(f"下一次签到任务计划在: {next_run_time.strftime('%Y-%m-%d %H:%M:%S')}") + print(f"下一次签到任务计划在: {next_run_time.strftime('%Y-%m-%d %H:%M:%S')}", flush=True) hours, remainder = divmod(sleep_duration, 3600) minutes, _ = divmod(remainder, 60) - print(f"程序将休眠 {int(hours)} 小时 {int(minutes)} 分钟。") + print(f"程序将休眠 {int(hours)} 小时 {int(minutes)} 分钟。", flush=True) time.sleep(sleep_duration) else: - print("计算出的下一个运行时间已过,等待 60 秒后重试...") + print("计算出的下一个运行时间已过,等待 60 秒后重试...", flush=True) time.sleep(60) run_checkin_task()