Commit ·
e01cfdd
1
Parent(s): 2d01f48
revert: SSE→1秒ポーリングに戻す(Gunicorn sync worker対策)
Browse filesSSEはGunicorn syncワーカーを最大2分間占有し、ソルバーdaemon threadと
GIL競合を起こすため削除。既存の /generate/log エンドポイントを
setInterval(1000ms) でポーリングする方式に変更。
3秒→1秒なので体感上は十分高速。
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- routes/generate.py +1 -45
- templates/generate/waiting.html +18 -18
routes/generate.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import logging
|
| 2 |
-
import os
|
| 3 |
import threading
|
| 4 |
-
import
|
| 5 |
-
from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app, jsonify, make_response, Response, stream_with_context
|
| 6 |
from db import repositories as repo
|
| 7 |
from optimizer.solver import solve, SolverConfig, PRIORITY_SCALE, SolveProgressCallback
|
| 8 |
from utils.solver_logger import logger, log_path
|
|
@@ -171,48 +169,6 @@ def view_log():
|
|
| 171 |
return jsonify({"error": str(e)}), 500
|
| 172 |
|
| 173 |
|
| 174 |
-
@bp.get("/log/stream")
|
| 175 |
-
def log_stream():
|
| 176 |
-
"""ソルバーログをSSEでリアルタイムストリーミング(生成中画面用)"""
|
| 177 |
-
def generate():
|
| 178 |
-
path = log_path()
|
| 179 |
-
last_pos = 0
|
| 180 |
-
|
| 181 |
-
# まず既存ログ全行を送信
|
| 182 |
-
if os.path.exists(path):
|
| 183 |
-
with open(path, encoding="utf-8", errors="replace") as f:
|
| 184 |
-
content = f.read()
|
| 185 |
-
last_pos = f.tell()
|
| 186 |
-
for line in content.splitlines():
|
| 187 |
-
if line.strip():
|
| 188 |
-
yield f"data: {line}\n\n"
|
| 189 |
-
|
| 190 |
-
# 以降は差分のみ(最大480回 × 0.25秒 ≒ 2分)
|
| 191 |
-
for _ in range(480):
|
| 192 |
-
time.sleep(0.25)
|
| 193 |
-
try:
|
| 194 |
-
cur_size = os.path.getsize(path)
|
| 195 |
-
except OSError:
|
| 196 |
-
yield ": hb\n\n"
|
| 197 |
-
continue
|
| 198 |
-
if cur_size > last_pos:
|
| 199 |
-
with open(path, encoding="utf-8", errors="replace") as f:
|
| 200 |
-
f.seek(last_pos)
|
| 201 |
-
chunk = f.read()
|
| 202 |
-
last_pos = f.tell()
|
| 203 |
-
for line in chunk.splitlines():
|
| 204 |
-
if line.strip():
|
| 205 |
-
yield f"data: {line}\n\n"
|
| 206 |
-
else:
|
| 207 |
-
yield ": hb\n\n"
|
| 208 |
-
|
| 209 |
-
return Response(
|
| 210 |
-
stream_with_context(generate()),
|
| 211 |
-
mimetype="text/event-stream",
|
| 212 |
-
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
|
| 216 |
@bp.get("/diag/<int:period_id>")
|
| 217 |
def diag(period_id):
|
| 218 |
"""シフト入力データの診断(DBの実データを確認)"""
|
|
|
|
| 1 |
import logging
|
|
|
|
| 2 |
import threading
|
| 3 |
+
from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app, jsonify, make_response
|
|
|
|
| 4 |
from db import repositories as repo
|
| 5 |
from optimizer.solver import solve, SolverConfig, PRIORITY_SCALE, SolveProgressCallback
|
| 6 |
from utils.solver_logger import logger, log_path
|
|
|
|
| 169 |
return jsonify({"error": str(e)}), 500
|
| 170 |
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
@bp.get("/diag/<int:period_id>")
|
| 173 |
def diag(period_id):
|
| 174 |
"""シフト入力データの診断(DBの実データを確認)"""
|
templates/generate/waiting.html
CHANGED
|
@@ -89,7 +89,7 @@
|
|
| 89 |
const scoreLabel = document.getElementById('score-label');
|
| 90 |
const scoreHint = document.getElementById('score-hint');
|
| 91 |
|
| 92 |
-
const logUrl = "{{ url_for('generate.
|
| 93 |
|
| 94 |
// ── 状態 ──────────────────────────────────────────────────────────────
|
| 95 |
const pageStart = Date.now(); // ページ読み込み基点
|
|
@@ -228,28 +228,28 @@
|
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
-
// ── ログパネル(
|
| 232 |
-
let
|
| 233 |
|
| 234 |
-
function
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
pre.appendChild(line);
|
| 243 |
-
pre.scrollTop = pre.scrollHeight;
|
| 244 |
-
};
|
| 245 |
-
evtSource.onerror = function() {
|
| 246 |
-
// SSE切断時は静かに閉じる
|
| 247 |
-
if (evtSource) { evtSource.close(); evtSource = null; }
|
| 248 |
-
};
|
| 249 |
}
|
| 250 |
|
| 251 |
function stopLogPolling() {
|
| 252 |
-
if (
|
| 253 |
}
|
| 254 |
|
| 255 |
function toggleLog() {
|
|
|
|
| 89 |
const scoreLabel = document.getElementById('score-label');
|
| 90 |
const scoreHint = document.getElementById('score-hint');
|
| 91 |
|
| 92 |
+
const logUrl = "{{ url_for('generate.view_log') }}";
|
| 93 |
|
| 94 |
// ── 状態 ──────────────────────────────────────────────────────────────
|
| 95 |
const pageStart = Date.now(); // ページ読み込み基点
|
|
|
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
+
// ── ログパネル(1秒ポーリング) ────────────────────────────────────────
|
| 232 |
+
let logTimer = null;
|
| 233 |
|
| 234 |
+
function fetchLog() {
|
| 235 |
+
fetch(logUrl)
|
| 236 |
+
.then(r => r.json())
|
| 237 |
+
.then(data => {
|
| 238 |
+
const pre = document.getElementById('log-pre');
|
| 239 |
+
if (!pre) return;
|
| 240 |
+
pre.textContent = data.log || '(ログなし)';
|
| 241 |
+
pre.scrollTop = pre.scrollHeight;
|
| 242 |
+
})
|
| 243 |
+
.catch(() => {});
|
| 244 |
+
}
|
| 245 |
|
| 246 |
+
function startLogStream() {
|
| 247 |
+
fetchLog();
|
| 248 |
+
logTimer = setInterval(fetchLog, 1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
}
|
| 250 |
|
| 251 |
function stopLogPolling() {
|
| 252 |
+
if (logTimer) { clearInterval(logTimer); logTimer = null; }
|
| 253 |
}
|
| 254 |
|
| 255 |
function toggleLog() {
|