Skip to content

Commit

Permalink
move db listener to main
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Rost committed Jul 20, 2024
1 parent 7da3e94 commit 5a608e4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
9 changes: 2 additions & 7 deletions dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { extname } from 'node:path';
import * as pages from './oauth.js';
import dashboard from './guilds.js';
import { posts, beta } from './functions.js';
import { db, dbListener, listenerMap, sessionData, settingsData } from './util.js';
import { db, listenerMap, sessionData, settingsData } from './util.js';
import Lang from './i18n.js';
const allLangs = Lang.allLangs();

Expand Down Expand Up @@ -358,12 +358,7 @@ function graceful(signal) {
console.log( '- Dashboard: ' + signal + ': Closed the dashboard server.' );
db.end().then( () => {
console.log( '- Dashboard: ' + signal + ': Closed the database connection.' );
dbListener.end().then( () => {
console.log( '- Dashboard: ' + signal + ': Closed the listener database connection.' );
process.exit(0);
}, dberror => {
console.log( '- Dashboard: ' + signal + ': Error while closing the listener database connection: ' + dberror );
} );
process.exit(0);
}, dberror => {
console.log( '- Dashboard: ' + signal + ': Error while closing the database connection: ' + dberror );
} );
Expand Down
39 changes: 13 additions & 26 deletions dashboard/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,6 @@ db.on( 'error', dberror => {
console.log( '- Dashboard: Error while connecting to the database: ' + dberror );
} );

const dbListener = new pg.Client(process.env.PGSSL === 'true' ? {ssl: true} : {});
dbListener.on( 'error', dberror => {
console.log( '- Dashboard: Error while connecting the listener to the database: ' + dberror );
} ).connect().catch( dberror => {
console.log( '- Dashboard: Error while connecting the listener to the database: ' + dberror );
} );
dbListener.query( 'LISTEN debugresponse' ).then( () => {
console.log( '- Dashboard: Added database debug response listener.' );
}, dberror => {
console.log( '- Dashboard: Error while adding the database debug response listener: ' + dberror );
} );

/** @type {Map<NodeJS.Timeout, Promise.resolve>} */
const listenerMap = new Map();
dbListener.on( 'notification', msg => {
if ( isDebug ) console.log( '- Dashboard: Notification received.', msg );
if ( msg.channel !== 'debugresponse' ) return;
listenerMap.forEach( (resolve, timeout) => {
clearTimeout(timeout);
resolve(msg.payload);
} );
listenerMap.clear();
} );

const oauth = new DiscordOauth2( {
clientId: process.env.bot,
clientSecret: process.env.secret,
Expand Down Expand Up @@ -222,6 +198,11 @@ const settingsData = new Map();
*/
const oauthVerify = new Map();

/**
* @type {Map<NodeJS.Timeout, Promise.resolve>}
*/
const listenerMap = new Map();

/**
* @type {Map<Number, PromiseConstructor>}
*/
Expand All @@ -230,6 +211,13 @@ var messageId = 1;

process.on( 'message', message => {
if ( message?.id === 'verifyUser' ) return oauthVerify.set(message.state, message.user);
if ( message?.id === 'debugresponse' ) {
listenerMap.forEach( (resolve, timeout) => {
clearTimeout(timeout);
resolve(message.data);
} );
return listenerMap.clear();
}
if ( message?.id ) {
if ( message.data.error ) messages.get(message.id).reject(message.data.error);
else messages.get(message.id).resolve(message.data.response);
Expand Down Expand Up @@ -509,14 +497,13 @@ function hasPerm(all = 0n, ...permission) {
export {
got,
db,
dbListener,
listenerMap,
oauth,
enabledOAuth2,
canRcGcDwButtons,
sessionData,
settingsData,
oauthVerify,
listenerMap,
sendMsg,
addWidgets,
createNotice,
Expand Down
4 changes: 1 addition & 3 deletions database.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ export default await db.connect().then( () => {
console.log( '- Error while connecting to the database: ' + dberror );
return Promise.reject();
} ).then( () => {
db.end().catch( dberror => {
console.log( '- Error while closing the database connection: ' + dberror );
} );
return db;
}, () => {
return db.end().then( () => {
console.log( '- Closed the database connection.' );
Expand Down
35 changes: 34 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {shardIdForGuildId} = ShardClientUtil;

var isDebug = ( process.argv[2] === 'debug' );
if ( process.argv[2] === 'readonly' ) process.env.READONLY = 'true';
import './database.js';
import db from './database.js';

const got = gotDefault.extend( {
throwHttpErrors: false,
Expand Down Expand Up @@ -116,6 +116,7 @@ manager.spawn( {
} );
} );

/** @type {import('node:child_process').ChildProcess|undefined} */
var server;
if ( process.env.dashboard ) {
const dashboard = forkChildProcess('./dashboard/index.js', ( isDebug ? ['debug'] : [] ));
Expand Down Expand Up @@ -450,6 +451,33 @@ function postStats(botList = JSON.parse(process.env.botlist), shardCount = manag
}, error => console.log( '- Error while getting the guild count: ' + error ) );
}

db.query( 'LISTEN debugresponse' ).then( () => {
console.log( '- Added database debug response listener.' );
}, dberror => {
console.log( '- Error while adding the database debug response listener: ' + dberror );
} );

db.on( 'notification', msg => {
if ( isDebug ) console.log( '- Database notification received:', msg );
if ( msg.channel !== 'debugresponse' || !msg.payload ) return;
let [type, ...payload] = msg.payload.split(' ');
if ( type === 'DUMP' ) {
if ( typeof server !== 'undefined' && server.connected ) {
return server.send( {
id: 'debugresponse',
data: payload.join(' ')
} );
};
return;
}
if ( typeof server !== 'undefined' && server.connected ) {
return server.send( {
id: 'debugresponse',
data: msg.payload
} );
};
} );


/**
* End the process gracefully.
Expand All @@ -458,6 +486,11 @@ function postStats(botList = JSON.parse(process.env.botlist), shardCount = manag
function graceful(signal) {
console.log( '- ' + signal + ': Disabling respawn...' );
manager.respawn = false;
db.end().then( () => {
console.log( '- ' + signal + ': Closed the listener database connection.' );
}, dberror => {
console.log( '- ' + signal + ': Error while closing the listener database connection: ' + dberror );
} );
}

process.once( 'SIGINT', graceful );
Expand Down

0 comments on commit 5a608e4

Please sign in to comment.