network chunk v18
This commit is contained in:
@@ -5,7 +5,6 @@ import { MemoryManager } from './memory_manager';
|
||||
import { JsonManager } from './json_manager';
|
||||
import { TcpCommunicator } from './tcp_communicator';
|
||||
import { operationCodes } from '../network/operation_codes';
|
||||
import { parentPort } from 'worker_threads';
|
||||
import { ParsedMessage } from "../network/message_handler";
|
||||
|
||||
export class BackupManager {
|
||||
@@ -143,9 +142,9 @@ export class BackupManager {
|
||||
}
|
||||
|
||||
if (unsentFiles.length > 0) {
|
||||
parentPort?.postMessage({ success: false, message: 'Backup could not be completed for all files', unsentFiles });
|
||||
process.send?.({type: 'log', message: 'Backup could not be completed for all files'});
|
||||
} else {
|
||||
parentPort?.postMessage({ success: true, message: 'Backup completed successfully' });
|
||||
process.send?.({type: 'log', message: 'Backup completed successfully' });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { JsonManager } from './json_manager';
|
||||
import { TcpCommunicator } from "./tcp_communicator";
|
||||
import { operationCodes } from '../network/operation_codes';
|
||||
import { parentPort } from 'worker_threads';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import crypto from 'crypto';
|
||||
@@ -62,10 +61,10 @@ export class BackupRetrievalWorker {
|
||||
this.log(`Backup retrieved successfully from ${ip}`);
|
||||
}
|
||||
|
||||
parentPort?.postMessage({ success: true, message: 'Backup retrieval completed successfully.' });
|
||||
process.send?.({ type: 'log', message: 'Backup retrieval completed successfully.' });
|
||||
} catch (error: any) {
|
||||
this.log(`Error in BackupRetrievalWorker: ${error.message}`, 'error');
|
||||
parentPort?.postMessage({ success: false, message: `A problem occurred: ${error.message}` });
|
||||
process.send?.({ type: 'log', message: `A problem occurred: ${error.message}` });
|
||||
}
|
||||
|
||||
if (global.gc) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {JsonManager} from "./json_manager";
|
||||
import {UdpClient} from "../network/udp/udp_client";
|
||||
import {parentPort} from "worker_threads";
|
||||
import {TcpCommunicator} from "./tcp_communicator";
|
||||
import {operationCodes} from "../network/operation_codes";
|
||||
import {ParsedMessage} from "../network/message_handler";
|
||||
@@ -65,19 +64,19 @@ export class NetworkScanner {
|
||||
if (!storedIp || storedIp !== ipAddress) {
|
||||
await this.applicationInfo.writeValue('serverIp', ipAddress);
|
||||
if (!this.appStarted) {
|
||||
parentPort?.postMessage({ type: 'changeContent', page: this.okPage });
|
||||
process.send?.({ type: 'changeContent', page: this.okPage });
|
||||
}
|
||||
this.appStarted = true;
|
||||
} else if (!this.appStarted) {
|
||||
parentPort?.postMessage({ type: 'changeContent', page: this.okPage });
|
||||
process.send?.({ type: 'changeContent', page: this.okPage });
|
||||
this.appStarted = true;
|
||||
}
|
||||
} else {
|
||||
parentPort?.postMessage({ type: 'changeContent', page: this.errorPage });
|
||||
process.send?.({ type: 'changeContent', page: this.errorPage });
|
||||
}
|
||||
} catch (err) {
|
||||
this.log(`Error checking UC: ${err}`, 'error', 'startUCCheck');
|
||||
parentPort?.postMessage({ type: 'changeContent', page: this.errorPage });
|
||||
process.send?.({ type: 'changeContent', page: this.errorPage });
|
||||
} finally {
|
||||
this.ucCheckBusy = false;
|
||||
}
|
||||
@@ -152,7 +151,7 @@ export class NetworkScanner {
|
||||
if (response?.operationCode !== operationCodes.OK) {
|
||||
await this.userConfig.resetFile();
|
||||
await this.userConfig.writeValue('app_type', app_type);
|
||||
parentPort?.postMessage({ type: 'changeContent', page: this.databaseResetPage });
|
||||
process.send?.({ type: 'changeContent', page: this.databaseResetPage });
|
||||
}
|
||||
} catch (err) {
|
||||
this.log(`Error during login request: ${err}`, 'error', 'sendLoginRequest');
|
||||
|
||||
@@ -1,174 +1,97 @@
|
||||
import { Worker } from 'worker_threads';
|
||||
import { fork, ChildProcess } from 'child_process';
|
||||
import path from 'path';
|
||||
import {WindowManager} from "./window_manager";
|
||||
import { WindowManager } from "./window_manager";
|
||||
|
||||
export class WorkerManager {
|
||||
private readonly pathToWorkerDir: string;
|
||||
private windowManager: WindowManager
|
||||
private workers: Worker[]; // Array to store running workers
|
||||
private windowManager: WindowManager;
|
||||
private workers: ChildProcess[]; // Array to store running child processes
|
||||
|
||||
constructor(pathToWorkerDir: string, windowManager: WindowManager) {
|
||||
this.pathToWorkerDir = pathToWorkerDir;
|
||||
this.windowManager = windowManager;
|
||||
this.workers = []; // Initialize the array to store workers
|
||||
this.workers = []; // Initialize the array to store child processes
|
||||
}
|
||||
|
||||
async startNetworkScannerWorker(udpPort: number, tcpPort: number, okPage: string, errorPage: string, databaseResetPage: string, userConfigPath: string, applicationInfoPath: string): Promise<void> {
|
||||
return this.startForkedWorker('network_scanner_worker.js', {
|
||||
UDP_PORT: udpPort.toString(),
|
||||
TCP_PORT: tcpPort.toString(),
|
||||
OK_PAGE: okPage,
|
||||
ERROR_PAGE: errorPage,
|
||||
DATABASE_RESET_PAGE: databaseResetPage,
|
||||
USER_CONFIG_PATH: userConfigPath,
|
||||
APPLICATION_INFO_PATH: applicationInfoPath
|
||||
});
|
||||
}
|
||||
|
||||
async startDirectoriesWatchersWorker(memoryManagerPath: string, applicationInfoPath: string): Promise<void> {
|
||||
return this.startForkedWorker('directories_watcher_worker.js', {
|
||||
MEMORY_MANAGER_PATH: memoryManagerPath,
|
||||
APPLICATION_INFO_PATH: applicationInfoPath
|
||||
});
|
||||
}
|
||||
|
||||
async startServersWorker(host: string, udpPort: number, tcpPort: number): Promise<void> {
|
||||
return this.startForkedWorker('servers_worker.js', {
|
||||
HOST: host,
|
||||
USER_UDP_PORT: udpPort.toString(),
|
||||
USER_TCP_PORT: tcpPort.toString()
|
||||
});
|
||||
}
|
||||
|
||||
async startResourceCoordinatorWorker(usersConfigPath: string, applicationInfoPath: string, memoryManagerPath: string, queueManagerPath: string, tcpPort: number): Promise<void> {
|
||||
return this.startForkedWorker('resource_coordinator_worker.js', {
|
||||
USERS_CONFIG_PATH: usersConfigPath,
|
||||
APPLICATION_INFO_PATH: applicationInfoPath,
|
||||
MEMORY_MANAGER_PATH: memoryManagerPath,
|
||||
QUEUE_MANAGER_PATH: queueManagerPath,
|
||||
TCP_PORT: tcpPort.toString()
|
||||
});
|
||||
}
|
||||
|
||||
async startBackupRetrievalWorker(userConfigPath: string, applicationInfoPath: string, clientPort: number, destinationPath: string): Promise<void> {
|
||||
return this.startForkedWorker('backup_retrieval_worker.js', {
|
||||
USER_CONFIG_PATH: userConfigPath,
|
||||
APPLICATION_INFO_PATH: applicationInfoPath,
|
||||
CLIENT_PORT: clientPort.toString(),
|
||||
DESTINATION_PATH: destinationPath
|
||||
});
|
||||
}
|
||||
|
||||
private async startForkedWorker(scriptName: string, envData: { [key: string]: string }): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(path.join(this.pathToWorkerDir, 'network_scanner_worker.js', ), {
|
||||
execArgv: ['--max-old-space-size=4096'],
|
||||
workerData: { udpPort, tcpPort, okPage, errorPage, databaseResetPage, userConfigPath, applicationInfoPath }, // Pass necessary data to the worker
|
||||
const worker = fork(path.join(this.pathToWorkerDir, scriptName), {
|
||||
execArgv: ['--max-old-space-size=4096'], // Set memory limit for the forked process
|
||||
env: { ...process.env, ...envData } // Merge environment variables
|
||||
});
|
||||
|
||||
this.workers.push(worker); // Store the worker reference
|
||||
|
||||
worker.on('message', (data) => {
|
||||
if (data.type === 'changeContent') {
|
||||
this.windowManager.changeContent(data.page);
|
||||
worker.on('message', (data: unknown) => {
|
||||
const message = data as { type: string, page?: string, message?: string }; // Type casting for message
|
||||
|
||||
if (message.type === 'changeContent' && message.page) {
|
||||
this.windowManager.changeContent(message.page);
|
||||
} else if (message.type === 'showAlert' && message.message) {
|
||||
this.windowManager.showAlert(message.message);
|
||||
} else {
|
||||
console.log(`${scriptName} message:`, message);
|
||||
}
|
||||
});
|
||||
|
||||
worker.on('error', (err) => {
|
||||
console.error('Network Scanner Worker error:', err);
|
||||
worker.terminate();
|
||||
console.error(`${scriptName} error:`, err);
|
||||
worker.kill();
|
||||
this.removeWorker(worker);
|
||||
reject(err); // Reject the promise if there's an error
|
||||
reject(err);
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
console.log(`Network Scanner Worker exited with code ${code}`);
|
||||
this.removeWorker(worker); // Remove worker reference when it exits
|
||||
resolve(); // Resolve when the worker exits cleanly
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Start the Directories Watcher Worker
|
||||
async startDirectoriesWatchersWorker(memoryManagerPath: string, applicationInfoPath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(path.join(this.pathToWorkerDir, 'directories_watcher_worker.js'), {
|
||||
execArgv: ['--max-old-space-size=4096'],
|
||||
workerData: { memoryManagerPath, applicationInfoPath }, // Pass the port to the worker
|
||||
});
|
||||
|
||||
this.workers.push(worker); // Store the worker reference
|
||||
|
||||
worker.on('message', (data) => {
|
||||
console.log('DirectoriesWatcher message:', data);
|
||||
});
|
||||
|
||||
worker.on('error', (err) => {
|
||||
console.error('DirectoriesWatcher error:', err);
|
||||
worker.terminate();
|
||||
console.log(`${scriptName} exited with code ${code}`);
|
||||
this.removeWorker(worker);
|
||||
reject(err); // Reject the promise if there's an error
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
console.log(`DirectoriesWatcher exited with code ${code}`);
|
||||
this.removeWorker(worker); // Remove worker reference when it exits
|
||||
resolve(); // Resolve when the worker exits cleanly
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Start the Servers Worker (UDP and TCP servers)
|
||||
async startServersWorker(host: string, udpPort: number, tcpPort: number): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(path.join(this.pathToWorkerDir, 'servers_worker.js'), {
|
||||
execArgv: ['--max-old-space-size=4096'],
|
||||
workerData: { HOST: host, USER_UDP_PORT: udpPort, USER_TCP_PORT: tcpPort }, // Pass host and ports to the worker
|
||||
});
|
||||
|
||||
this.workers.push(worker); // Store the worker reference
|
||||
|
||||
worker.on('message', (data) => {
|
||||
console.log('Servers Worker message:', data);
|
||||
});
|
||||
|
||||
worker.on('error', (err) => {
|
||||
console.error('Servers Worker error:', err);
|
||||
worker.terminate();
|
||||
this.removeWorker(worker);
|
||||
reject(err); // Reject the promise if there's an error
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
console.log(`Servers Worker exited with code ${code}`);
|
||||
this.removeWorker(worker); // Remove worker reference when it exits
|
||||
resolve(); // Resolve when the worker exits cleanly
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Start the Users Info Worker
|
||||
async startResourceCoordinatorWorker(usersConfigPath: string, applicationInfoPath: string, memoryManagerPath: string, queueManagerPath: string, tcpPort: number): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(path.join(this.pathToWorkerDir, 'resource_coordinator_worker.js'), {
|
||||
execArgv: ['--max-old-space-size=4096'],
|
||||
workerData: {
|
||||
usersConfigPath,
|
||||
applicationInfoPath,
|
||||
memoryManagerPath,
|
||||
queueManagerPath,
|
||||
tcpPort
|
||||
}, // Pass necessary parameters to the worker
|
||||
});
|
||||
|
||||
this.workers.push(worker); // Store the worker reference
|
||||
|
||||
worker.on('message', (data) => {
|
||||
console.log('Users Info Worker message:', data);
|
||||
});
|
||||
|
||||
worker.on('error', (err) => {
|
||||
console.error('Users Info Worker error:', err);
|
||||
worker.terminate();
|
||||
this.removeWorker(worker);
|
||||
reject(err); // Reject the promise if there's an error
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
console.log(`Users Info Worker exited with code ${code}`);
|
||||
this.removeWorker(worker); // Remove worker reference when it exits
|
||||
resolve(); // Resolve when the worker exits cleanly
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Start the Backup Retrieval Worker
|
||||
async startBackupRetrievalWorker(
|
||||
userConfigPath: string,
|
||||
applicationInfoPath: string,
|
||||
clientPort: number,
|
||||
destinationPath: string
|
||||
): Promise<void> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const worker = new Worker(path.join(this.pathToWorkerDir, 'backup_retrieval_worker.js'), {
|
||||
execArgv: ['--max-old-space-size=4096'],
|
||||
workerData: { userConfigPath, applicationInfoPath, clientPort, destinationPath }, // Pass parameters to the worker
|
||||
});
|
||||
|
||||
this.workers.push(worker); // Store the worker reference
|
||||
|
||||
worker.on('message', async (data) => {
|
||||
console.log('Backup Retrieval Worker message:', data);
|
||||
await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait for 1 second
|
||||
await this.windowManager.changeContent('main_menu');
|
||||
await this.windowManager.showAlert(data.message);
|
||||
});
|
||||
|
||||
worker.on('error', (err) => {
|
||||
console.error('Backup Retrieval Worker error:', err);
|
||||
worker.terminate();
|
||||
this.removeWorker(worker);
|
||||
reject(err); // Reject the promise if there's an error
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
console.log(`Backup Retrieval Worker exited with code ${code}`);
|
||||
this.removeWorker(worker); // Remove worker reference when it exits
|
||||
resolve(); // Resolve when the worker exits cleanly
|
||||
if (code === 0) resolve();
|
||||
else reject(new Error(`${scriptName} exited with code ${code}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -176,15 +99,15 @@ export class WorkerManager {
|
||||
// Close all running workers
|
||||
closeAllWorkers(): void {
|
||||
console.log('Terminating all running workers...');
|
||||
this.workers.forEach(worker => worker.terminate()); // Terminate each worker
|
||||
this.workers = []; // Clear the array after terminating all workers
|
||||
this.workers.forEach(worker => worker.kill());
|
||||
this.workers = [];
|
||||
}
|
||||
|
||||
// Helper method to remove a worker from the workers array when it exits
|
||||
private removeWorker(worker: Worker): void {
|
||||
private removeWorker(worker: ChildProcess): void {
|
||||
const index = this.workers.indexOf(worker);
|
||||
if (index > -1) {
|
||||
this.workers.splice(index, 1); // Remove the worker from the array
|
||||
this.workers.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user