Update Dockerfile
Browse files- Dockerfile +63 -34
Dockerfile
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
FROM gitea/gitea:1.24.6
|
| 2 |
|
| 3 |
-
# 以 root 用户设置权限(Spaces 允许构建时 root)
|
| 4 |
USER root
|
| 5 |
|
| 6 |
# 安装 Python、pip、git 和异步库
|
|
@@ -109,7 +109,7 @@ if __name__ == "__main__":
|
|
| 109 |
exit(1)
|
| 110 |
EOF
|
| 111 |
|
| 112 |
-
# 复制上传脚本,
|
| 113 |
COPY --chown=git:git <<'EOF' /uploadhf.py
|
| 114 |
import os
|
| 115 |
import time
|
|
@@ -123,12 +123,21 @@ from watchdog.observers import Observer
|
|
| 123 |
from watchdog.events import FileSystemEventHandler
|
| 124 |
from huggingface_hub import HfApi
|
| 125 |
import base64
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
# 配置日志
|
|
|
|
| 128 |
logging.basicConfig(
|
| 129 |
level=logging.INFO,
|
| 130 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
)
|
|
|
|
|
|
|
| 132 |
logger = logging.getLogger(__name__)
|
| 133 |
|
| 134 |
class DataDirectoryHandler(FileSystemEventHandler):
|
|
@@ -166,7 +175,7 @@ class DataDirectoryHandler(FileSystemEventHandler):
|
|
| 166 |
self.pending_changes = [] # 清空缓冲区
|
| 167 |
for attempt in range(max_retries):
|
| 168 |
try:
|
| 169 |
-
commit_message = f"自动提交: {change_summary} - {
|
| 170 |
logger.info(f"开始上传: {commit_message}")
|
| 171 |
await asyncio.to_thread(self.api.upload_folder,
|
| 172 |
folder_path=self.data_directory,
|
|
@@ -213,37 +222,58 @@ def start_directory_monitoring(data_directory="/data", repo_id=None, hf_token=No
|
|
| 213 |
return observer
|
| 214 |
|
| 215 |
def start_gitea_server(port=7860):
|
| 216 |
-
"""启动 Gitea 服务器"""
|
| 217 |
def run_gitea():
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
break
|
| 232 |
-
if output:
|
| 233 |
-
logger.info(f"Gitea: {output.strip()}")
|
| 234 |
-
|
| 235 |
-
return_code = gitea_process.poll()
|
| 236 |
-
if return_code != 0:
|
| 237 |
-
error_output = gitea_process.stderr.read()
|
| 238 |
-
logger.error(f"❌ Gitea 服务器异常退出,返回码: {return_code}")
|
| 239 |
-
logger.error(f"错误信息: {error_output}")
|
| 240 |
-
else:
|
| 241 |
-
logger.info("✅ Gitea 服务器正常退出")
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
gitea_thread = threading.Thread(target=run_gitea)
|
| 249 |
gitea_thread.daemon = True
|
|
@@ -332,5 +362,4 @@ ENV GITEA__database__DB_TYPE=sqlite3 \
|
|
| 332 |
USER git
|
| 333 |
|
| 334 |
# 运行 Gitea,显式指定 Spaces 默认端口
|
| 335 |
-
RUN cat /uploadhf.py
|
| 336 |
CMD ["/set_gitea_env.sh", "python3", "/uploadhf.py"]
|
|
|
|
| 1 |
FROM gitea/gitea:1.24.6
|
| 2 |
|
| 3 |
+
# 以 root 用户设置权限(Spaces 允许构建时使用 root)
|
| 4 |
USER root
|
| 5 |
|
| 6 |
# 安装 Python、pip、git 和异步库
|
|
|
|
| 109 |
exit(1)
|
| 110 |
EOF
|
| 111 |
|
| 112 |
+
# 复制上传脚本,修改为使用北京时间并添加 Gitea 自动重启逻辑
|
| 113 |
COPY --chown=git:git <<'EOF' /uploadhf.py
|
| 114 |
import os
|
| 115 |
import time
|
|
|
|
| 123 |
from watchdog.events import FileSystemEventHandler
|
| 124 |
from huggingface_hub import HfApi
|
| 125 |
import base64
|
| 126 |
+
from datetime import datetime
|
| 127 |
+
import pytz
|
| 128 |
|
| 129 |
+
# 配置日志,使用北京时间(CST,UTC+8)
|
| 130 |
+
beijing_tz = pytz.timezone('Asia/Shanghai')
|
| 131 |
logging.basicConfig(
|
| 132 |
level=logging.INFO,
|
| 133 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 134 |
+
datefmt='%Y-%m-%d %H:%M:%S %Z',
|
| 135 |
+
handlers=[
|
| 136 |
+
logging.StreamHandler()
|
| 137 |
+
]
|
| 138 |
)
|
| 139 |
+
# 设置日志的时区
|
| 140 |
+
logging.Formatter.converter = lambda *args: datetime.now(beijing_tz).timetuple()
|
| 141 |
logger = logging.getLogger(__name__)
|
| 142 |
|
| 143 |
class DataDirectoryHandler(FileSystemEventHandler):
|
|
|
|
| 175 |
self.pending_changes = [] # 清空缓冲区
|
| 176 |
for attempt in range(max_retries):
|
| 177 |
try:
|
| 178 |
+
commit_message = f"自动提交: {change_summary} - {datetime.now(beijing_tz).strftime('%Y-%m-%d %H:%M:%S')}"
|
| 179 |
logger.info(f"开始上传: {commit_message}")
|
| 180 |
await asyncio.to_thread(self.api.upload_folder,
|
| 181 |
folder_path=self.data_directory,
|
|
|
|
| 222 |
return observer
|
| 223 |
|
| 224 |
def start_gitea_server(port=7860):
|
| 225 |
+
"""启动 Gitea 服务器,包含自动重启逻辑"""
|
| 226 |
def run_gitea():
|
| 227 |
+
max_restart_attempts = 5
|
| 228 |
+
restart_delay = 10 # 每次重启前的等待时间(秒)
|
| 229 |
+
attempt = 0
|
| 230 |
+
|
| 231 |
+
while attempt < max_restart_attempts:
|
| 232 |
+
try:
|
| 233 |
+
gitea_process = subprocess.Popen(
|
| 234 |
+
['gitea', 'web', '--port', str(port)],
|
| 235 |
+
stdout=subprocess.PIPE,
|
| 236 |
+
stderr=subprocess.PIPE,
|
| 237 |
+
universal_newlines=True
|
| 238 |
+
)
|
| 239 |
+
logger.info(f"🚀 Gitea 服务器已启动,端口: {port}")
|
| 240 |
+
logger.info(f"📊 访问地址: http://localhost:{port}")
|
| 241 |
+
|
| 242 |
+
while True:
|
| 243 |
+
output = gitea_process.stdout.readline()
|
| 244 |
+
if output == '' and gitea_process.poll() is not None:
|
| 245 |
+
break
|
| 246 |
+
if output:
|
| 247 |
+
logger.info(f"Gitea: {output.strip()}")
|
| 248 |
+
|
| 249 |
+
return_code = gitea_process.poll()
|
| 250 |
+
if return_code != 0:
|
| 251 |
+
error_output = gitea_process.stderr.read()
|
| 252 |
+
logger.error(f"❌ Gitea 服务器异常退出,返回码: {return_code}")
|
| 253 |
+
logger.error(f"错误信息: {error_output}")
|
| 254 |
+
attempt += 1
|
| 255 |
+
if attempt < max_restart_attempts:
|
| 256 |
+
logger.info(f"将在 {restart_delay} 秒后尝试重启 Gitea (尝试 {attempt + 1}/{max_restart_attempts})")
|
| 257 |
+
time.sleep(restart_delay)
|
| 258 |
+
else:
|
| 259 |
+
logger.error(f"❌ 达到最大重启次数 ({max_restart_attempts}),停止重试")
|
| 260 |
+
break
|
| 261 |
+
else:
|
| 262 |
+
logger.info("✅ Gitea 服务器正常退出")
|
| 263 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
except FileNotFoundError:
|
| 266 |
+
logger.error("❌ 未找到 gitea 命令,请确保 Gitea 已正确安装")
|
| 267 |
+
break
|
| 268 |
+
except Exception as e:
|
| 269 |
+
logger.error(f"❌ 启动 Gitea 服务器时发生错误: {e}")
|
| 270 |
+
attempt += 1
|
| 271 |
+
if attempt < max_restart_attempts:
|
| 272 |
+
logger.info(f"将在 {restart_delay} 秒后尝试重启 Gitea (尝试 {attempt + 1}/{max_restart_attempts})")
|
| 273 |
+
time.sleep(restart_delay)
|
| 274 |
+
else:
|
| 275 |
+
logger.error(f"❌ 达到最大重启次数 ({max_restart_attempts}),停止重试")
|
| 276 |
+
break
|
| 277 |
|
| 278 |
gitea_thread = threading.Thread(target=run_gitea)
|
| 279 |
gitea_thread.daemon = True
|
|
|
|
| 362 |
USER git
|
| 363 |
|
| 364 |
# 运行 Gitea,显式指定 Spaces 默认端口
|
|
|
|
| 365 |
CMD ["/set_gitea_env.sh", "python3", "/uploadhf.py"]
|