forked from unimatrix/web
35 lines
2.7 KiB
JavaScript
35 lines
2.7 KiB
JavaScript
import {readFile} from 'node:fs/promises';
|
|
import {existsSync} from 'node:fs';
|
|
import {pathToFileURL} from 'node:url';
|
|
import path from 'node:path';
|
|
|
|
// A singleton delivery process stays independent of blue/green web candidates.
|
|
// Existing outboxes retain their baseline, claim, revision and uncertain-state rules.
|
|
export async function createNewsDelivery(db,{discord,forum,config=null,forumEnabled=true,intervalMs=10000,logger=console}={}){
|
|
if(config)await discord.initializeDiscordNews(db);
|
|
if(forumEnabled)await forum.initializeForumNews(db);
|
|
let stopped=false,inflight=null;
|
|
function tick(){
|
|
if(stopped)return Promise.resolve();if(inflight)return inflight;
|
|
inflight=(async()=>{
|
|
if(config){try{const result=await discord.drainDiscordNews(db,config);if(result.length)logger.log('Discord news delivery: '+result.join(', '));}catch{logger.warn('Discord news queue check failed; existing delivery state retained.');}}
|
|
if(forumEnabled){try{const result=await forum.drainForumNews(db);if(result.length)logger.log('Forum news delivery: '+result.join(', '));}catch{logger.warn('Forum news queue check failed; existing delivery state retained.');}}
|
|
})().finally(()=>{inflight=null;});return inflight;
|
|
}
|
|
const timer=setInterval(()=>void tick(),intervalMs);
|
|
const stop=async()=>{if(stopped)return;stopped=true;clearInterval(timer);await inflight;await db.close();};
|
|
await tick();return {tick,stop};
|
|
}
|
|
export async function startInstalledNewsDelivery({settings=process.env,release='/home/dunerealms/apps/dunerealms/current',logger=console}={}){
|
|
const load=name=>import(pathToFileURL(path.join(release,'node',name)).href);
|
|
const [{openDatabase},discord,forum]=await Promise.all([load('mysql.mjs'),load('discord-news.mjs'),load('forum-news.mjs')]);
|
|
let config=null;
|
|
if(settings.DISCORD_NEWS_CONFIG_FILE){config=JSON.parse(await readFile(settings.DISCORD_NEWS_CONFIG_FILE,'utf8'));discord.validateNewsWebhook(config);}
|
|
const forumEnabled=existsSync('/home/dunerealms/apps/integrations/forum-news.php');
|
|
if(!config&&!forumEnabled)throw Error('No configured news delivery integration.');
|
|
const db=await openDatabase(settings);
|
|
try{return await createNewsDelivery(db,{discord,forum,config,forumEnabled,logger});}catch(error){await db.close();throw error;}
|
|
}
|
|
if(process.argv[1]&&import.meta.url===pathToFileURL(path.resolve(process.argv[1])).href){
|
|
try{const worker=await startInstalledNewsDelivery();console.log('News delivery worker started; existing outboxes are authoritative.');let stopping=false;const shutdown=async()=>{if(stopping)return;stopping=true;await worker.stop();process.exit(0);};process.once('SIGTERM',shutdown);process.once('SIGINT',shutdown);}catch{console.error('News delivery worker could not start; check private configuration and database access.');process.exitCode=1;}
|
|
}
|

Staff