2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
const electron = require('electron');
|
2020-02-07 19:37:04 +00:00
|
|
|
const Queue = require('p-queue').default;
|
2020-04-01 18:59:11 +00:00
|
|
|
const sql = require('../ts/sql/Server').default;
|
2018-08-28 21:53:05 +00:00
|
|
|
const { remove: removeUserConfig } = require('./user_config');
|
|
|
|
const { remove: removeEphemeralConfig } = require('./ephemeral_config');
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
const { ipcMain } = electron;
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
initialize,
|
|
|
|
};
|
|
|
|
|
|
|
|
let initialized = false;
|
|
|
|
|
|
|
|
const SQL_CHANNEL_KEY = 'sql-channel';
|
|
|
|
const ERASE_SQL_KEY = 'erase-sql-key';
|
|
|
|
|
2019-09-03 20:10:32 +00:00
|
|
|
let singleQueue = null;
|
|
|
|
let multipleQueue = null;
|
|
|
|
|
|
|
|
function makeNewSingleQueue() {
|
2020-09-18 20:40:41 +00:00
|
|
|
singleQueue = new Queue({ concurrency: 1, timeout: 1000 * 60 * 2 });
|
2019-09-03 20:10:32 +00:00
|
|
|
return singleQueue;
|
|
|
|
}
|
|
|
|
function makeNewMultipleQueue() {
|
2020-09-18 20:40:41 +00:00
|
|
|
multipleQueue = new Queue({ concurrency: 10, timeout: 1000 * 60 * 2 });
|
2019-09-03 20:10:32 +00:00
|
|
|
return multipleQueue;
|
|
|
|
}
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
function makeSQLJob(fn, callName, jobId, args) {
|
|
|
|
// console.log(`Job ${jobId} (${callName}) queued`);
|
|
|
|
return async () => {
|
|
|
|
// const start = Date.now();
|
|
|
|
// console.log(`Job ${jobId} (${callName}) started`);
|
|
|
|
const result = await fn(...args);
|
|
|
|
// const end = Date.now();
|
|
|
|
// console.log(`Job ${jobId} (${callName}) succeeded in ${end - start}ms`);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleCall(callName, jobId, args) {
|
|
|
|
const fn = sql[callName];
|
|
|
|
if (!fn) {
|
|
|
|
throw new Error(`sql channel: ${callName} is not an available function`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let result;
|
|
|
|
|
|
|
|
// We queue here to keep multi-query operations atomic. Without it, any multistage
|
|
|
|
// data operation (even within a BEGIN/COMMIT) can become interleaved, since all
|
|
|
|
// requests share one database connection.
|
|
|
|
|
|
|
|
// A needsSerial method must be run in our single concurrency queue.
|
|
|
|
if (fn.needsSerial) {
|
|
|
|
if (singleQueue) {
|
|
|
|
result = await singleQueue.add(makeSQLJob(fn, callName, jobId, args));
|
|
|
|
} else if (multipleQueue) {
|
|
|
|
makeNewSingleQueue();
|
|
|
|
|
|
|
|
singleQueue.add(() => multipleQueue.onIdle());
|
|
|
|
multipleQueue = null;
|
|
|
|
|
|
|
|
result = await singleQueue.add(makeSQLJob(fn, callName, jobId, args));
|
|
|
|
} else {
|
|
|
|
makeNewSingleQueue();
|
|
|
|
result = await singleQueue.add(makeSQLJob(fn, callName, jobId, args));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The request can be parallelized. To keep the same structure as the above block
|
|
|
|
// we force this section into the 'lonely if' pattern.
|
|
|
|
// eslint-disable-next-line no-lonely-if
|
|
|
|
if (multipleQueue) {
|
|
|
|
result = await multipleQueue.add(makeSQLJob(fn, callName, jobId, args));
|
|
|
|
} else if (singleQueue) {
|
|
|
|
makeNewMultipleQueue();
|
|
|
|
multipleQueue.pause();
|
|
|
|
|
|
|
|
const multipleQueueRef = multipleQueue;
|
|
|
|
const singleQueueRef = singleQueue;
|
|
|
|
|
|
|
|
singleQueue = null;
|
|
|
|
const promise = multipleQueueRef.add(
|
|
|
|
makeSQLJob(fn, callName, jobId, args)
|
|
|
|
);
|
|
|
|
await singleQueueRef.onIdle();
|
|
|
|
|
|
|
|
multipleQueueRef.start();
|
|
|
|
result = await promise;
|
|
|
|
} else {
|
|
|
|
makeNewMultipleQueue();
|
|
|
|
result = await multipleQueue.add(makeSQLJob(fn, callName, jobId, args));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-16 17:07:38 +00:00
|
|
|
function initialize() {
|
2018-07-27 01:13:56 +00:00
|
|
|
if (initialized) {
|
|
|
|
throw new Error('sqlChannels: already initialized!');
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
|
|
|
|
ipcMain.on(SQL_CHANNEL_KEY, async (event, jobId, callName, ...args) => {
|
|
|
|
try {
|
2019-09-26 19:56:31 +00:00
|
|
|
const result = await handleCall(callName, jobId, args);
|
2018-07-27 01:13:56 +00:00
|
|
|
event.sender.send(`${SQL_CHANNEL_KEY}-done`, jobId, null, result);
|
|
|
|
} catch (error) {
|
|
|
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
|
|
|
console.log(
|
|
|
|
`sql channel error with call ${callName}: ${errorForDisplay}`
|
|
|
|
);
|
2019-08-20 13:24:43 +00:00
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send(`${SQL_CHANNEL_KEY}-done`, jobId, errorForDisplay);
|
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.on(ERASE_SQL_KEY, async event => {
|
|
|
|
try {
|
2018-08-28 21:53:05 +00:00
|
|
|
removeUserConfig();
|
|
|
|
removeEphemeralConfig();
|
2018-07-27 01:13:56 +00:00
|
|
|
event.sender.send(`${ERASE_SQL_KEY}-done`);
|
|
|
|
} catch (error) {
|
|
|
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
|
|
|
console.log(`sql-erase error: ${errorForDisplay}`);
|
|
|
|
event.sender.send(`${ERASE_SQL_KEY}-done`, error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|