#!/usr/bin/env node /** * 将 news_morning_draft 切到 news002 并触发一次强制重生成(143 worker 拾取)。 * * 用法: * node scripts/trigger-news002-generate-draft.mjs * node scripts/trigger-news002-generate-draft.mjs --apply * node scripts/trigger-news002-generate-draft.mjs --apply --restore-template */ import process from 'node:process'; import mysql from 'mysql2/promise'; import { loadH5Environment } from './load-env.mjs'; import { NEWS002_DEFAULT_TASK_SPEC_APPEND } from '../news-morning-templates.mjs'; loadH5Environment(import.meta.dirname); const CONFIG_KEY = 'news_morning_draft'; const CONFIG_TABLE = 'h5_wechat_admin_config'; /** @deprecated 使用 NEWS002_DEFAULT_TASK_SPEC_APPEND */ export const NEWS002_FORCE_TASK_APPEND = NEWS002_DEFAULT_TASK_SPEC_APPEND; async function main() { const apply = process.argv.includes('--apply'); const restoreTemplate = process.argv.includes('--restore-template'); if (!process.env.DATABASE_URL) { console.error('DATABASE_URL missing'); process.exit(1); } const pool = mysql.createPool({ uri: process.env.DATABASE_URL, connectionLimit: 2 }); const [rows] = await pool.query( `SELECT config_json FROM ${CONFIG_TABLE} WHERE config_key = ? LIMIT 1`, [CONFIG_KEY], ); const current = rows[0]?.config_json ? (typeof rows[0].config_json === 'string' ? JSON.parse(rows[0].config_json) : rows[0].config_json) : {}; const previousTemplateId = current.templateId ?? 'news001'; const next = { ...current, enabled: current.enabled !== false, templateId: restoreTemplate ? 'news001' : 'news002', forceGenerateOnce: restoreTemplate ? false : true, taskSpecAppend: restoreTemplate ? (current.taskSpecAppend ?? '') : NEWS002_DEFAULT_TASK_SPEC_APPEND, }; console.log(JSON.stringify({ apply, restoreTemplate, previousTemplateId, nextTemplateId: next.templateId, forceGenerateOnce: next.forceGenerateOnce, sourceUserId: current.sourceUserId ?? null, autoGenerateEnabled: current.autoGenerateEnabled ?? null, autoPushEnabled: current.autoPushEnabled ?? null, }, null, 2)); if (apply) { const now = Date.now(); await pool.query( `INSERT INTO ${CONFIG_TABLE} (config_key, config_json, updated_by, updated_at) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE config_json = VALUES(config_json), updated_by = VALUES(updated_by), updated_at = VALUES(updated_at)`, [ CONFIG_KEY, JSON.stringify(next), restoreTemplate ? 'news002-restore-template' : 'news002-force-generate', now, ], ); console.log(restoreTemplate ? 'template restored to news001; forceGenerateOnce cleared' : 'templateId=news002 + forceGenerateOnce=true persisted'); } await pool.end(); } main().catch((error) => { console.error(error instanceof Error ? error.message : error); process.exit(1); });