fix(server,worker): asyncRoute для async-хендлеров и без рекурсии pub/sub

This commit is contained in:
dev
2026-10-11 21:11:48 +03:00
parent d891db5c2d
commit 449ff00259
2 changed files with 33 additions and 17 deletions
+15 -11
View File
@@ -45,7 +45,7 @@ const { pipeline } = require('stream/promises');
types.setTypeParser(1082, v => v);
const app = express();
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 20, connectionTimeoutMillis: 10000 });
const UPLOADS_DIR = path.join(__dirname, 'uploads');
const storage = createStorage({ dir: UPLOADS_DIR });
@@ -697,6 +697,10 @@ async function loadBans() {
app.set('trust proxy', 'loopback');
function asyncRoute(fn) {
return (req, res, next) => Promise.resolve().then(() => fn(req, res, next)).catch(next);
}
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 300,
@@ -2974,7 +2978,7 @@ app.post('/api/chat/threads/:id/messages', requireAuth, chatLimiter, (req, res,
if (err.message === 'Not allowed extension') return res.status(400).json({ error: 'Недопустимый тип файла' });
return res.status(400).json({ error: 'Недопустимый файл' });
});
}, async (req, res) => {
}, asyncRoute(async (req, res) => {
const acc = await chatThreadFor(req.user, req.params.id);
const files = req.files || [];
if (acc.error) {
@@ -3093,7 +3097,7 @@ app.post('/api/chat/threads/:id/messages', requireAuth, chatLimiter, (req, res,
publishChat({ type: 'message', thread_id: acc.thread.id, tutor_id: acc.thread.tutor_id, message });
await logAudit(req, 'chat.message.create', { thread_id: acc.thread.id, message_id: created.id, files: frows.length });
res.json({ message });
});
}));
app.put('/api/chat/threads/:id/messages/:messageId', requireAuth, chatLimiter, async (req, res) => {
try {
@@ -3217,17 +3221,17 @@ app.delete('/api/chat/threads/:id/messages/:messageId', requireAuth, chatLimiter
}
});
app.post('/api/chat/threads/:id/read', requireAuth, chatLimiter, async (req, res) => {
app.post('/api/chat/threads/:id/read', requireAuth, chatLimiter, asyncRoute(async (req, res) => {
const acc = await chatThreadFor(req.user, req.params.id);
if (acc.error) return res.status(acc.error).json({ error: acc.message });
const col = req.user.role === 'admin' ? 'admin_unread' : 'tutor_unread';
await pool.query(`UPDATE chat_threads SET ${col} = 0 WHERE id = $1`, [acc.thread.id]);
res.json({ ok: true, unread: await chatUnread(req.user) });
});
}));
app.get('/api/chat/unread', requireAuth, chatLimiter, async (req, res) => {
app.get('/api/chat/unread', requireAuth, chatLimiter, asyncRoute(async (req, res) => {
res.json({ unread: await chatUnread(req.user) });
});
}));
app.get('/api/chat/stream', async (req, res) => {
let user = null;
@@ -3709,7 +3713,7 @@ app.get('/api/feedbacks/:id', requireAuth, async (req, res) => {
res.json(fb);
});
app.post('/api/feedbacks', requireAuth, async (req, res) => {
app.post('/api/feedbacks', requireAuth, asyncRoute(async (req, res) => {
const feedbackDate = parseFeedbackDate(req.body?.feedback_date);
if (!feedbackDate) return res.status(400).json({ error: 'Некорректная дата' });
const resident = reqStr(req.body?.resident, FEEDBACK_RESIDENT_MAX);
@@ -3749,7 +3753,7 @@ app.post('/api/feedbacks', requireAuth, async (req, res) => {
} finally {
client.release();
}
});
}));
app.post('/api/shorts/ai-correct', requireAuth, aiLimiter, async (req, res) => {
try {
@@ -4247,7 +4251,7 @@ app.delete('/api/bookmarks/:id/photo', requireAdmin, async (req, res) => {
res.json({ ok: true });
});
app.put('/api/feedbacks/:id', requireAuth, async (req, res) => {
app.put('/api/feedbacks/:id', requireAuth, asyncRoute(async (req, res) => {
const id = parseInt(req.params.id, 10);
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
const fb = await feedbackById(req.user, id);
@@ -4301,7 +4305,7 @@ app.put('/api/feedbacks/:id', requireAuth, async (req, res) => {
if (aiCheck) wakeFeedbackWorker();
await logAudit(req, 'feedback.update', { id, ai_check: aiCheck, ai_profile: aiProfile || null });
res.json({ ok: true });
});
}));
app.delete('/api/feedbacks/:id', requireAuth, async (req, res) => {
const id = parseInt(req.params.id, 10);
+18 -6
View File
@@ -827,12 +827,16 @@ function createLessonReportChecker({ pool, getSetting, logAudit, aiUrl, model, d
});
}
function notify() {
function wakeLocal() {
if (wake) wake();
}
function notify() {
wakeLocal();
if (bus) bus.publish();
}
if (bus) bus.subscribe(() => notify());
if (bus) bus.subscribe(wakeLocal);
async function isEnabled() {
return String(await getSetting('lesson_ai_enabled', 'true')) !== 'false';
@@ -1494,12 +1498,16 @@ function createNoteTitleWorker({ pool, getSetting, logAudit, aiUrl, model, defau
});
}
function notify() {
function wakeLocal() {
if (wake) wake();
}
function notify() {
wakeLocal();
if (bus) bus.publish();
}
if (bus) bus.subscribe(() => notify());
if (bus) bus.subscribe(wakeLocal);
async function isEnabled() {
return String(await getSetting('notes_ai_enabled', 'true')) !== 'false';
@@ -1749,12 +1757,16 @@ function createFaceScanWorker({ pool, getSetting, logAudit, bus, scan, onDone })
});
}
function notify() {
function wakeLocal() {
if (wake) wake();
}
function notify() {
wakeLocal();
if (bus) bus.publish();
}
if (bus) bus.subscribe(() => notify());
if (bus) bus.subscribe(wakeLocal);
async function isEnabled() {
return String(await getSetting('face_id_enabled', 'false')) === 'true';