codis-slave-script
v2.3.1
Published
**CODIS** - A system for distributed computing using JS engines in web browsers.
Downloads
25
Readme
CODIS - CODIS-SLAVE-SCRIPT
CODIS - A system for distributed computing using JS engines in web browsers.
CODIS-SLAVE-SCRIPT - The system's website scripts part.
- Manages communication with the CODIS server.
- He is responsible for communication with other executive nodes.
- Creates executive threads.
- He is responsible for ensuring that too many threads are not created within the entire browser.
The server part of the system: CODIS-MASTER
Installation
$ yarn global add codis-slave-script
or
$ npm install -g codis-slave-script
Getting Started
After installation, call the command:
$ codis-init-slave
This command has two options:
- destination - the websocket address of our CODIS-MASTER.
- one-per-system - limit creation of execution nodes to one open tab in the browser for a given domain. In other words, with an open web page in two internet tabs, the system will only work on the first opened tab to reduce the system load. If the first card is closed, an executive node will be created on the second card.
Example:
$ codis-init-slave --destination ws://153.63.21.221:5299 --one-per-system true
The command will create 3 files in the selected directory:
- codis.js
- sharedInspector.js
- CODIS-slave.js
You must enable downloading of these files through your website.
You need to add codis.js
to your html file of your website: (preferably with the defer
option to make the script initialization in no way affect the website):
<script defer src="codis.js">
You can edit the destination and one-per-system options in the codis.js and codis-slave.js files
Modules
An important part of the CODIS system are modules. So the scripts that are downloaded as threads of executive nodes. The single module is a single script with the extension .mjs. In these files we define the logic of solving incoming tasks to the thread.
We create module files in the same directory where we called codis-init-slave.
If on the server we create a task in this form: CODIS.createTask ('exampleModule', task)
this means that in our directory we need to have the file exampleModule.mjs
so that it can be retrieved as a thread.
Receiving data and actions
The basis of the contents of the module file of our system should contain:
onmessage = (message) => {
switch (message.data.action) {
case 'doTask':
break;
}
};
Or if the module is intended for parents' task threads
onmessage = (message) => {
switch (message.data.action) {
case 'doTask':
console.log(message.data.task);
break;
case 'fromChild':
case 'fromParent':
case 'fromSibling':
console.log(message.data.data);
break;
}
};
message.data.action === 'doTask'
- 'doTask' is always the first / initial action that will come to our system thread. Along with this action, the content of the task will come, which we can read frommessage.data.task
.message.data.action === 'fromChild'
- 'fromChild' is an action that indicates the arrival of data from a sub-task thread. So this action can only come to the thread that performs the parent's tasks. Incoming data is stored inmessage.data.data
.message.data.action === 'fromParent'
- 'fromParent' is an action indicating the arrival of data from the parent thread. This action can only be reached from a thread that performs the parent's task. Incoming data is stored inmessage.data.data
.message.data.action === 'fromSibling'
- 'fromSibling' is an action indicating the arrival of data from a thread of the same level (from sibling) (ie from the thread of the task with the same parent). Incoming data is stored inmessage.data.data
.
Receiving information about sub-tasks
In order to receive information about sub-tasks currently being performed under selected task, call the action getWorkingChildrenThreads
which will return the information under the action childrenTasks
on the return. For example:
onmessage = (message) => {
switch (message.data.action) {
case 'doTask':
postMessage({action: 'getWorkingChildrenThreads'});
break;
case 'childrenTasks':
console.log(message.data.childrenTasks);
break;
}
};
The returned object is an array of currently operating sub-tasks. The element of the array is of the type:
interface Task {
nodeId?: string | string[];
// time in milliseconds
createdTime: number;
threadNumber?: number;
id: number;
module: string;
task: any;
options: TaskOptions;
// time in milliseconds
takeTime: number;
// ids of sub-tasks
subTasks: number[];
}
Sending data
To send a solution to the server, call postMessage with an object with the solution field::
postMessage({solution: result});
To send information that this task has been completed (no solution to save on the server side):
postMessage({action: 'terminate'});
To send data to the parent thread (parent), call postMessage with the fields: action: 'sendToParent'
and with the field data
where you save the message content:
data
- sent data can be of any type (any
)
postMessage ({action: 'sendToParent', data: 'some data'});
To send data to sub-tasks from a parent, we have three methods to use:
sendToSomeChild
- sends data to a randomly selected sub-task.sendToActiveChildren
- sends data to all currently running sub-tasks.sendToChild
- sends data to the selected sub-task with the given identifier.
postMessage ({action: 'sendToSomeChild', data: 'some data'});
postMessage ({action: 'sendToActiveChildren', data: 'some data'});
postMessage ({action: 'sendToChild', data: 'some data', childTaskId: 43});
To send data to working threads from the same level (to siblings):
postMessage ({action: 'sendToSiblings', data: 'some data'});
Control from the page
CODIS scripts are downloaded at the entrance to the site and are immediately run.
CODIS adds a function to the global window
object thanks to which we can control the operation of the CODIS application.
These are the functions:
CODIS_stop()
- stops the CODIS system on the machine.
CODIS_restart()
- resumes system after it has been stopped.
Example
For example, we can add two buttons to our site that will stop and restart the system.
<button onclick="CODIS_stop()">
stop
</button>
<button onclick="CODIS_restart()">
restart
</button>
Notices
The system / library was created by Filip Szcześniak.
For contact purposes, please write to email: [email protected]
The system is fully functional with the functions given above. The system is constantly evolving.
The code repository is private, it will be made available only to those who would like to support me in writing the system.
If you want to help in the development of the application, write to me!
If you notice any mistake, write to me!
If you want to offer some important functionality, write to me!