feat(rest): queue for async messages /api/queue

This commit is contained in:
Alexey Kasyanchuk 2023-06-06 03:48:00 +03:00
parent 78c6df5557
commit f9ec0c2cbb

View File

@ -58,11 +58,32 @@ io.on('connection', (socket) =>
} }
}) })
let responceRestQueue = [];
if (appConfig.restApi) {
app.get('/api/queue', (req, res) => {
const uniqueId = Math.random().toString(16).slice(2) + '_' + (new Date()).getTime();
logT('rest', 'queue responce', uniqueId, 'size', responceRestQueue.length);
res.send({id: uniqueId, queue: responceRestQueue})
// clear queue after the read of json queue
responceRestQueue = [];
});
}
const start = async () => const start = async () =>
{ {
({ sphinx } = await startSphinx(() => { ({ sphinx } = await startSphinx(() => {
dbPatcher(() => { dbPatcher(() => {
spider = new spiderCall((...data) => io.sockets.emit(...data), (message, callback) => { spider = new spiderCall((...data) => {
if (appConfig.restApi) {
if (responceRestQueue.length < 1000) {
responceRestQueue.push(data);
} else {
logTE('rest', 'max 1000 queue records, please use /api/queue to clean records')
}
}
return io.sockets.emit(...data)
}, (message, callback) => {
socketMessages[message] = callback socketMessages[message] = callback
if (appConfig.restApi) { if (appConfig.restApi) {
app.get('/api/' + message, (req, res) => { app.get('/api/' + message, (req, res) => {