1
1
mirror of https://github.com/Fabio286/antares.git synced 2025-06-05 21:59:22 +02:00

Merge pull request #727 from antares-sql/flatpak-experiments

Flatpak experiments
This commit is contained in:
2023-12-02 14:12:51 +01:00
committed by GitHub
5 changed files with 99 additions and 111 deletions

View File

@@ -63,43 +63,39 @@ async function restartElectron () {
if (!manualRestart) process.exit(0); if (!manualRestart) process.exit(0);
}); });
} }
function startWorkers () {
function startMain () { const compiler = webpack(workersConfig);
const webpackSetup = webpack([mainConfig, workersConfig]);
webpackSetup.compilers.forEach((compiler) => {
const { name } = compiler; const { name } = compiler;
switch (name) { compiler.hooks.afterEmit.tap('afterEmit', () => {
case 'workers':
compiler.hooks.afterEmit.tap('afterEmit', async () => {
console.log(chalk.gray(`\nCompiled ${name} script!`)); console.log(chalk.gray(`\nCompiled ${name} script!`));
console.log( console.log(chalk.gray(`\nWatching file changes for ${name} script...`));
chalk.gray(`\nWatching file changes for ${name} script...`)
);
}); });
break;
case 'main': compiler.watch({ aggregateTimeout: 500 }, err => {
default: if (err) console.error(chalk.red(err));
});
}
function startMain () {
const compiler = webpack(mainConfig);
const { name } = compiler;
compiler.hooks.afterEmit.tap('afterEmit', async () => { compiler.hooks.afterEmit.tap('afterEmit', async () => {
console.log(chalk.gray(`\nCompiled ${name} script!`)); console.log(chalk.gray(`\nCompiled ${name} script!`));
manualRestart = true; manualRestart = true;
await restartElectron(); await restartElectron();
startWorkers();
setTimeout(() => { setTimeout(() => {
manualRestart = false; manualRestart = false;
}, 2500); }, 2500);
console.log( console.log(chalk.gray(`\nWatching file changes for ${name} script...`));
chalk.gray(`\nWatching file changes for ${name} script...`)
);
});
break;
}
}); });
webpackSetup.watch({ aggregateTimeout: 500 }, err => { compiler.watch({ aggregateTimeout: 500 }, err => {
if (err) console.error(chalk.red(err)); if (err) console.error(chalk.red(err));
}); });
} }

View File

@@ -1,17 +1,14 @@
import { ChildProcess, fork, spawn } from 'child_process';
import * as antares from 'common/interfaces/antares'; import * as antares from 'common/interfaces/antares';
import * as workers from 'common/interfaces/workers'; import * as workers from 'common/interfaces/workers';
import { dialog, ipcMain } from 'electron'; import { dialog, ipcMain } from 'electron';
import * as fs from 'fs'; import * as fs from 'fs';
import { Worker } from 'worker_threads';
import { validateSender } from '../libs/misc/validateSender'; import { validateSender } from '../libs/misc/validateSender';
const isDevelopment = process.env.NODE_ENV !== 'production';
const isFlatpak = process.platform === 'linux' && process.env.DISTRIBUTION === 'flatpak';
export default (connections: {[key: string]: antares.Client}) => { export default (connections: {[key: string]: antares.Client}) => {
let exporter: ChildProcess = null; let exporter: Worker = null;
let importer: ChildProcess = null; let importer: Worker = null;
ipcMain.handle('create-schema', async (event, params) => { ipcMain.handle('create-schema', async (event, params) => {
if (!validateSender(event.senderFrame)) return { status: 'error', response: 'Unauthorized process' }; if (!validateSender(event.senderFrame)) return { status: 'error', response: 'Unauthorized process' };
@@ -202,7 +199,7 @@ export default (connections: {[key: string]: antares.Client}) => {
if (!validateSender(event.senderFrame)) return { status: 'error', response: 'Unauthorized process' }; if (!validateSender(event.senderFrame)) return { status: 'error', response: 'Unauthorized process' };
if (exporter !== null) { if (exporter !== null) {
exporter.kill(); exporter.terminate();
return; return;
} }
@@ -227,21 +224,12 @@ export default (connections: {[key: string]: antares.Client}) => {
} }
} }
// Init exporter process // Init exporter thread
if (isFlatpak) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment
exporter = spawn('flatpak-spawn', ['--watch-bus', '--host', 'node', './exporter.js'], { // @ts-ignore
shell: true exporter = new Worker(new URL('../workers/exporter', import.meta.url));
});
}
else {
exporter = fork(isDevelopment ? './dist/exporter.js' : './exporter.js', [], {
execArgv: isDevelopment ? ['--inspect=9224'] : undefined,
silent: true
});
// exporter = spawn('node', [isDevelopment ? '--inspect=9224' : '', isDevelopment ? './dist/exporter.js' : './exporter.js']);
}
exporter.stdin.write(JSON.stringify({ exporter.postMessage({
type: 'init', type: 'init',
client: { client: {
name: type, name: type,
@@ -269,21 +257,19 @@ export default (connections: {[key: string]: antares.Client}) => {
event.sender.send('export-progress', payload); event.sender.send('export-progress', payload);
break; break;
case 'end': case 'end':
setTimeout(() => { // Ensures that writing process has finished setTimeout(() => { // Ensures that writing thread has finished
if (exporter) { exporter?.terminate();
exporter.kill();
exporter = null; exporter = null;
}
}, 2000); }, 2000);
resolve({ status: 'success', response: payload }); resolve({ status: 'success', response: payload });
break; break;
case 'cancel': case 'cancel':
exporter.kill(); exporter?.terminate();
exporter = null; exporter = null;
resolve({ status: 'error', response: 'Operation cancelled' }); resolve({ status: 'error', response: 'Operation cancelled' });
break; break;
case 'error': case 'error':
exporter.kill(); exporter?.terminate();
exporter = null; exporter = null;
resolve({ status: 'error', response: payload }); resolve({ status: 'error', response: payload });
break; break;
@@ -314,7 +300,7 @@ export default (connections: {[key: string]: antares.Client}) => {
if (result.response === 1) { if (result.response === 1) {
willAbort = true; willAbort = true;
exporter.stdin.write(JSON.stringify({ type: 'cancel' })); exporter.postMessage({ type: 'cancel' });
} }
} }
@@ -325,7 +311,7 @@ export default (connections: {[key: string]: antares.Client}) => {
if (!validateSender(event.senderFrame)) return { status: 'error', response: 'Unauthorized process' }; if (!validateSender(event.senderFrame)) return { status: 'error', response: 'Unauthorized process' };
if (importer !== null) { if (importer !== null) {
importer.kill(); importer.terminate();
return; return;
} }
@@ -333,20 +319,12 @@ export default (connections: {[key: string]: antares.Client}) => {
(async () => { (async () => {
const dbConfig = await connections[options.uid].getDbConfig(); const dbConfig = await connections[options.uid].getDbConfig();
// Init importer process // Init importer thread
if (isFlatpak) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment
importer = spawn('flatpak-spawn', ['--watch-bus', 'node', './importer.js'], { // @ts-ignore
cwd: __dirname importer = new Worker(new URL('../workers/importer', import.meta.url));
});
}
else {
importer = fork(isDevelopment ? './dist/importer.js' : './importer.js', [], {
execArgv: isDevelopment ? ['--inspect=9224'] : undefined,
silent: true
});
}
importer.stdin.write(JSON.stringify({ importer.postMessage({
type: 'init', type: 'init',
dbConfig, dbConfig,
options options
@@ -374,18 +352,18 @@ export default (connections: {[key: string]: antares.Client}) => {
break; break;
case 'end': case 'end':
setTimeout(() => { // Ensures that writing process has finished setTimeout(() => { // Ensures that writing process has finished
importer?.kill(); importer?.terminate();
importer = null; importer = null;
}, 2000); }, 2000);
resolve({ status: 'success', response: payload }); resolve({ status: 'success', response: payload });
break; break;
case 'cancel': case 'cancel':
importer.kill(); importer.terminate();
importer = null; importer = null;
resolve({ status: 'error', response: 'Operation cancelled' }); resolve({ status: 'error', response: 'Operation cancelled' });
break; break;
case 'error': case 'error':
importer.kill(); importer.terminate();
importer = null; importer = null;
resolve({ status: 'error', response: payload }); resolve({ status: 'error', response: payload });
break; break;
@@ -416,7 +394,7 @@ export default (connections: {[key: string]: antares.Client}) => {
if (result.response === 1) { if (result.response === 1) {
willAbort = true; willAbort = true;
importer.stdin.write(JSON.stringify({ type: 'cancel' })); importer.postMessage({ type: 'cancel' });
} }
} }

View File

@@ -1,7 +1,7 @@
import * as antares from 'common/interfaces/antares'; import * as antares from 'common/interfaces/antares';
import * as log from 'electron-log/main'; import * as log from 'electron-log/main';
import * as fs from 'fs'; import * as fs from 'fs';
import { nextTick } from 'process'; import { parentPort } from 'worker_threads';
import { MySQLClient } from '../libs/clients/MySQLClient'; import { MySQLClient } from '../libs/clients/MySQLClient';
import { PostgreSQLClient } from '../libs/clients/PostgreSQLClient'; import { PostgreSQLClient } from '../libs/clients/PostgreSQLClient';
@@ -14,8 +14,9 @@ log.transports.file.fileName = 'workers.log';
log.transports.console = null; log.transports.console = null;
log.errorHandler.startCatching(); log.errorHandler.startCatching();
process.stdin.on('data', async buff => { // eslint-disable-next-line @typescript-eslint/no-explicit-any
const { type, client, tables, options } = JSON.parse(buff.toString()); const exportHandler = async (data: any) => {
const { type, client, tables, options } = data;
if (type === 'init') { if (type === 'init') {
try { try {
@@ -35,7 +36,7 @@ process.stdin.on('data', async buff => {
exporter = new PostgreSQLExporter(connection as PostgreSQLClient, tables, options); exporter = new PostgreSQLExporter(connection as PostgreSQLClient, tables, options);
break; break;
default: default:
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'error', type: 'error',
payload: `"${client.name}" exporter not aviable` payload: `"${client.name}" exporter not aviable`
})); }));
@@ -44,31 +45,26 @@ process.stdin.on('data', async buff => {
exporter.once('error', err => { exporter.once('error', err => {
log.error(err.toString()); log.error(err.toString());
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'error', type: 'error',
payload: err.toString() payload: err.toString()
})); }));
}); });
exporter.once('end', () => { exporter.once('end', () => {
nextTick(() => { parentPort.postMessage({
process.stdout.write(JSON.stringify({
type: 'end', type: 'end',
payload: { cancelled: exporter.isCancelled } payload: { cancelled: exporter.isCancelled }
}));
connection.destroy();
}); });
}); });
exporter.once('cancel', () => { exporter.once('cancel', () => {
nextTick(() => {
fs.unlinkSync(exporter.outputFile); fs.unlinkSync(exporter.outputFile);
process.stdout.write(JSON.stringify({ type: 'cancel' })); parentPort.postMessage({ type: 'cancel' });
});
}); });
exporter.on('progress', state => { exporter.on('progress', state => {
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'export-progress', type: 'export-progress',
payload: state payload: state
})); }));
@@ -78,7 +74,7 @@ process.stdin.on('data', async buff => {
} }
catch (err) { catch (err) {
log.error(err.toString()); log.error(err.toString());
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'error', type: 'error',
payload: err.toString() payload: err.toString()
})); }));
@@ -86,4 +82,6 @@ process.stdin.on('data', async buff => {
} }
else if (type === 'cancel') else if (type === 'cancel')
exporter.cancel(); exporter.cancel();
}); };
parentPort.on('message', exportHandler);

View File

@@ -2,6 +2,7 @@ import * as antares from 'common/interfaces/antares';
import * as log from 'electron-log/main'; import * as log from 'electron-log/main';
import * as mysql from 'mysql2'; import * as mysql from 'mysql2';
import * as pg from 'pg'; import * as pg from 'pg';
import { parentPort } from 'worker_threads';
import { MySQLClient } from '../libs/clients/MySQLClient'; import { MySQLClient } from '../libs/clients/MySQLClient';
import { PostgreSQLClient } from '../libs/clients/PostgreSQLClient'; import { PostgreSQLClient } from '../libs/clients/PostgreSQLClient';
@@ -14,9 +15,14 @@ log.transports.file.fileName = 'workers.log';
log.transports.console = null; log.transports.console = null;
log.errorHandler.startCatching(); log.errorHandler.startCatching();
process.stdin.on('data', async (buff: Buffer) => { const importHandler = async (data: {
const { type, dbConfig, options } = JSON.parse(buff.toString()); type: string;
dbConfig: mysql.ConnectionOptions & { schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean }
| pg.ClientConfig & { schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean }
| { databasePath: string; readonly: boolean };
options: ImportOptions;
}) => {
const { type, dbConfig, options } = data;
if (type === 'init') { if (type === 'init') {
try { try {
const connection = await ClientsFactory.getClient({ const connection = await ClientsFactory.getClient({
@@ -39,7 +45,7 @@ process.stdin.on('data', async (buff: Buffer) => {
importer = new PostgreSQLImporter(pool as unknown as pg.PoolClient, options); importer = new PostgreSQLImporter(pool as unknown as pg.PoolClient, options);
break; break;
default: default:
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'error', type: 'error',
payload: `"${options.type}" importer not aviable` payload: `"${options.type}" importer not aviable`
})); }));
@@ -48,32 +54,32 @@ process.stdin.on('data', async (buff: Buffer) => {
importer.once('error', err => { importer.once('error', err => {
log.error(err.toString()); log.error(err.toString());
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'error', type: 'error',
payload: err.toString() payload: err.toString()
})); }));
}); });
importer.once('end', () => { importer.once('end', () => {
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'end', type: 'end',
payload: { cancelled: importer.isCancelled } payload: { cancelled: importer.isCancelled }
})); }));
}); });
importer.once('cancel', () => { importer.once('cancel', () => {
process.stdout.write(JSON.stringify({ type: 'cancel' })); parentPort.postMessage({ type: 'cancel' });
}); });
importer.on('progress', state => { importer.on('progress', state => {
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'import-progress', type: 'import-progress',
payload: state payload: state
})); }));
}); });
importer.on('query-error', state => { importer.on('query-error', state => {
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'query-error', type: 'query-error',
payload: state payload: state
})); }));
@@ -83,7 +89,7 @@ process.stdin.on('data', async (buff: Buffer) => {
} }
catch (err) { catch (err) {
log.error(err.toString()); log.error(err.toString());
process.stdout.write(JSON.stringify({ parentPort.postMessage({
type: 'error', type: 'error',
payload: err.toString() payload: err.toString()
})); }));
@@ -91,4 +97,6 @@ process.stdin.on('data', async (buff: Buffer) => {
} }
else if (type === 'cancel') else if (type === 'cancel')
importer.cancel(); importer.cancel();
}); };
parentPort.on('message', importHandler);

View File

@@ -19,7 +19,15 @@ module.exports = { // Main
output: { output: {
libraryTarget: 'commonjs2', libraryTarget: 'commonjs2',
path: path.join(__dirname, 'dist'), path: path.join(__dirname, 'dist'),
filename: '[name].js' filename: '[name].js',
assetModuleFilename: (pathData) => {
const { filename } = pathData;
if (filename.endsWith('.ts'))
return '[name].js';
else
return '[name][ext]';
}
}, },
node: { node: {
global: true, global: true,