+ Implemented the WorkerClass

+ Implemented a typescript debugging tool
* Configuration Updates
This commit is contained in:
Daniel Legt 2021-08-21 17:50:54 +03:00
parent 54f12a164c
commit 7296410b12
5 changed files with 139 additions and 8 deletions

21
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/app.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}

View File

@ -1,22 +1,48 @@
// Import the configuration file
import { getConfig } from "./helper/config";
import { getConfig, NodeConnection } from "./helper/config";
// Import SystemControl
import { SystemControl } from "./models/SystemController";
// Import the worker processes
import { DataWorker } from "./models/WorkerProcess";
// Global system control singleton
(global as any).logger = new SystemControl();
(global as any).config = null;
((global as any).logger as SystemControl) = new SystemControl();
/**
* The main function to run the program with
*/
async function main() {
// Get the configuration file
(global as any).config = getConfig();
const config = getConfig();
console.log(
(global as any).config
);
// Initialize the Worker Nodes array
let workerNodes : Array<DataWorker> = [];
// Spawn in the DataWorkers
for ( let i: number = 0; i < config.options.parallel_nodes; i++ ) {
// Create a new data worker
workerNodes.push(new DataWorker(config.options.parallel_node_tasks));
}
/**
* Keep track of the worker that is assigned this node
*/
let workerIndex = 0;
// Start assigning nodes to the DataWorkers
for ( let node of config.nodes ) {
console.log(`Adding workerIndex[${workerIndex}]`);
// Add the task to the node
workerNodes[workerIndex].addNodeTask(node);
// Increment the worker index
workerIndex++;
if ( workerIndex >= workerNodes.length ) workerIndex = 0;
}
console.log(`Succesfully added ${config.nodes.length} Tasks to ${workerNodes.length} Workers`);
for( let w of workerNodes ) {
await w.startProcessing();
}
}

10
src/helper/mainHelper.ts Normal file
View File

@ -0,0 +1,10 @@
/**
* Get an unique UUID V4
* @returns An unique UUID
*/
export function uuid(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}

View File

@ -0,0 +1,74 @@
import { NodeConnection } from "../helper/config";
import { uuid } from "../helper/mainHelper";
export class DataWorker {
/**
* An array of tasks that should be taken care of
*/
private nodeTasks : Array<NodeConnection> = [];
/**
* The amount of parallel tasks to run at once
*/
private parallelTasksCount: number;
private id: string;
/**
* @param parallelTasks The amount of parallel tasks that this Worker can run at once
*/
constructor(parallelTasks: number) {
// Initialize the amount of paralel tasks to run at the same time
this.parallelTasksCount = parallelTasks;
// Generate an ID for the worker
this.id = uuid();
}
public addNodeTask(node: NodeConnection) {
// Add the task to the list
this.nodeTasks.push(node);
}
/**
* Start processing the node tasks.
*/
public async startProcessing() : Promise<void> {
// Go through all of the tasks to run
for ( let i = 0; i < this.nodeTasks.length; i++ ) {
// Spawn in the task queue
let taskQueue : Array<Promise<any>> = [];
// Start running the parallel tasks
for ( let x = 0; x < this.parallelTasksCount; x++ ) {
// Check if we have stept out of bounds
if ( i >= this.nodeTasks.length ) // Ignore out-of-bounds
break;
// Add the process to the queue
taskQueue.push(this.processTask(this.nodeTasks[i]))
// Add to the index
i += x;
}
// Get the results of the running
const runResults = await Promise.all(taskQueue);
console.log(runResults);
}
}
/**
* Process a task
* @param task The task to process
*/
private async processTask(task: NodeConnection) {
return `[${this.id}]: Succesfully ran task ${task.name}`;
}
}

View File

@ -58,7 +58,7 @@
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
"inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */