feat: autogen run_hour/run_minute, publish_slots, scheduled_posts tables and routes

This commit is contained in:
Alexey Pavlov
2026-05-31 16:45:15 +03:00
parent c1d5337680
commit 213dc104f5
3 changed files with 103 additions and 10 deletions
+27 -6
View File
@@ -129,15 +129,37 @@ async function runAutogenForCategory(category) {
* Основная функция cron — проверяет какие категории нужно генерировать.
*/
async function runAutogen({ forceCategory = null } = {}) {
let whereClause, params = [];
if (forceCategory) {
// Ручной запуск — игнорируем время
whereClause = `WHERE enabled=true AND category=$1`;
params = [forceCategory];
} else {
// Автоматический запуск — проверяем время по расписанию
// Берём текущий час/минуту в московском времени (UTC+3)
const now = new Date();
const mskOffset = 3 * 60; // UTC+3
const mskTime = new Date(now.getTime() + mskOffset * 60000);
const currentHour = mskTime.getUTCHours();
const currentMinute = mskTime.getUTCMinutes();
console.log(`[Autogen] Check time MSK ${String(currentHour).padStart(2,'0')}:${String(currentMinute).padStart(2,'0')}`);
whereClause = `WHERE enabled=true
AND run_hour=$1
AND run_minute BETWEEN $2 AND $3
AND (last_run_at IS NULL OR last_run_at < NOW() - INTERVAL '6 hours')`;
params = [currentHour, currentMinute - 5, currentMinute + 5];
}
const { rows: settings } = await query(
`SELECT * FROM autogen_settings WHERE enabled=true
${forceCategory ? `AND category=$1` : `AND (next_run_at IS NULL OR next_run_at <= NOW())`}
ORDER BY category`,
forceCategory ? [forceCategory] : []
`SELECT * FROM autogen_settings ${whereClause} ORDER BY category`,
params
);
if (!settings.length) {
console.log('[Autogen] Nothing to generate');
console.log('[Autogen] Nothing to generate at this time');
return { processed: 0, results: [] };
}
@@ -145,7 +167,6 @@ async function runAutogen({ forceCategory = null } = {}) {
for (const s of settings) {
const result = await runAutogenForCategory(s.category);
results.push({ category: s.category, ...result });
// Пауза между категориями чтобы не перегружать API
if (settings.indexOf(s) < settings.length - 1) {
await new Promise(r => setTimeout(r, 5000));
}