From 7296410b1252aaa5996d6f7a998f3c772b9c0f1c Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sat, 21 Aug 2021 17:50:54 +0300 Subject: [PATCH] + Implemented the WorkerClass + Implemented a typescript debugging tool * Configuration Updates --- .vscode/launch.json | 21 +++++++++++ src/app.ts | 40 ++++++++++++++++---- src/helper/mainHelper.ts | 10 +++++ src/models/WorkerProcess.ts | 74 +++++++++++++++++++++++++++++++++++++ tsconfig.json | 2 +- 5 files changed, 139 insertions(+), 8 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 src/helper/mainHelper.ts create mode 100644 src/models/WorkerProcess.ts diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..fe986c5 --- /dev/null +++ b/.vscode/launch.json @@ -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": [ + "/**" + ], + "program": "${workspaceFolder}/src/app.ts", + "preLaunchTask": "tsc: build - tsconfig.json", + "outFiles": [ + "${workspaceFolder}/dist/**/*.js" + ] + } + ] +} \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index a592abf..1023627 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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 = []; + + // 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(); + } } diff --git a/src/helper/mainHelper.ts b/src/helper/mainHelper.ts new file mode 100644 index 0000000..2bd4988 --- /dev/null +++ b/src/helper/mainHelper.ts @@ -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); + }); +} \ No newline at end of file diff --git a/src/models/WorkerProcess.ts b/src/models/WorkerProcess.ts new file mode 100644 index 0000000..e795bab --- /dev/null +++ b/src/models/WorkerProcess.ts @@ -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 = []; + + /** + * 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 { + + // 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> = []; + + // 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}`; + } + +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index ec3b2f3..aaafe09 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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 */