diff --git a/dashboard/index.js b/dashboard/index.js index 1b3d1acc..c094d03e 100644 --- a/dashboard/index.js +++ b/dashboard/index.js @@ -292,33 +292,37 @@ const server = createServer( (req, res) => { } if ( reqURL.pathname === '/debug' && rcscriptExists && process.env.owner.split('|').includes(sessionData.get(state).user_id) ) { - console.log( '- Dashboard: Requesting RcGcDb debug dump.' ); + let site = reqURL.searchParams.get('site'); + console.log( '- Dashboard: Requesting RcGcDb debug dump' + ( site ? ' for ' + site : '.' ) ); return new Promise( (resolve, reject) => { + let id = Date.now(); + if ( site ) id = '-1+' + id; let timeout = setTimeout( () => { - listenerMap.delete(timeout); + listenerMap.delete(id); reject('Timeout'); }, 5000 ).unref(); - listenerMap.set(timeout, resolve); - db.query( 'SELECT pg_notify($1, $2)', ['webhookupdates', 'DEBUG DUMP'] ).catch( dberror => { - console.log( '- Dashboard: Error while requesting the debug dump: ' + dberror ); - listenerMap.delete(timeout); - clearTimeout(timeout); - reject(dberror); - } ); - } ).then( body => { + listenerMap.set(id, {timeout, write, resolve}); res.writeHead(200, { - 'Content-Length': Buffer.byteLength(body), 'Content-Type': 'application/json' }); - res.write( body ); + db.query( 'SELECT pg_notify($1, $2)', ['webhookupdates', ( site ? 'DEBUG SITE ' + id + ' ' + site : 'DEBUG DUMP ' + id )] ).catch( dberror => { + console.log( '- Dashboard: Error while requesting the debug dump' + ( site ? ' for ' + site : '' ) + ': ' + dberror ); + listenerMap.delete(id); + clearTimeout(timeout); + reject(dberror); + } ); + /** @param {String} body */ + function write(body) { + return res.write( body, error => { + listenerMap.delete(id); + clearTimeout(timeout); + reject(error); + } ); + } + } ).then( () => { return res.end(); }, error => { - let body = 'Error: ' + error + ''; - res.writeHead(500, { - 'Content-Length': Buffer.byteLength(body), - 'Content-Type': 'text/html' - }); - res.write( body ); + res.write( JSON.stringify( {error} ) ); return res.end(); } ); } diff --git a/dashboard/util.js b/dashboard/util.js index 681adfa2..95aa5e3e 100644 --- a/dashboard/util.js +++ b/dashboard/util.js @@ -199,7 +199,7 @@ const settingsData = new Map(); const oauthVerify = new Map(); /** - * @type {Map} + * @type {Map Boolean, resolve: PromiseConstructor.resolve}>} */ const listenerMap = new Map(); @@ -212,11 +212,18 @@ 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(); + let listener = listenerMap.get(message.listener); + if ( !listener ) return; + if ( message.part === 'CHUNK' ) { + return listener.write(message.data); + } + if ( message.part === 'END' ) { + listenerMap.delete(message.listener); + clearTimeout(listener.timeout); + listener.resolve(); + return; + } + return; } if ( message?.id ) { if ( message.data.error ) messages.get(message.id).reject(message.data.error); diff --git a/main.js b/main.js index 1b6e2539..abd7c44d 100644 --- a/main.js +++ b/main.js @@ -468,22 +468,32 @@ db.query( 'LISTEN debugresponse' ).then( () => { 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(' '); + let [type, part, listener, ...payload] = msg.payload.split(' '); + if ( !type || !part || !listener ) return; if ( type === 'DUMP' ) { if ( typeof server !== 'undefined' && server.connected ) { return server.send( { id: 'debugresponse', + type, part, listener, data: payload.join(' ') } ); }; return; } - if ( typeof server !== 'undefined' && server.connected ) { - return server.send( { - id: 'debugresponse', - data: msg.payload - } ); - }; + if ( type === 'SITE' ) { + let shard = +listener.split('+')[0] + if ( Number.isNaN(shard) || shard >= manager.totalShards ) return; + if ( shard === -1 ) { + if ( typeof server !== 'undefined' && server.connected ) { + return server.send( { + id: 'debugresponse', + type, part, listener, + data: payload.join(' ') + } ); + }; + return; + } + } } );