|
1 | | - |
2 | | - |
3 | 1 | # Master |
4 | 2 |
|
5 | | -Master connects to a peer2peer "room" data channel and shares with their peers (Workers) |
6 | | -data about the jobs. Among them is job data( each job/task may have its own data to solve/work on) , |
7 | | -job code to executed in workers and code dependencies which workers have to install before stat processing tasks. |
8 | | -In other words Master defines the problem , defines the way to solve it, provides all the tools and code needed and adds data (if) needed to solve the problem.. |
| 3 | +Master connects to a peer2peer "room" data channel and shares with their peers (Workers) data about the jobs. Among them is job data( each job/task may have its own data to solve/work on) , job code to executed in workers and code dependencies which workers have to install before stat processing tasks. In other words Master defines the problem , defines the way to solve it, provides all the tools and code needed and adds data (if) needed to solve the problem.. |
9 | 4 |
|
10 | | -Master and Workers can find each other in any network condition , as long they are connected online. |
11 | | -This is possible from nature of peer to peer networks and [bugout](https://github.com/chr15m/bugout) ! |
12 | | -Bugout offers message transfer encryption, but we encrypt all data i/o transfers on top of that , here as well. |
| 5 | +Master and Workers can find each other in any network condition , as long they are connected online. This is possible from nature of peer to peer networks and [bugout](https://github.com/chr15m/bugout) ! Bugout offers message transfer encryption, but we encrypt all data i/o transfers on top of that , here as well. |
13 | 6 |
|
14 | 7 | ## Installation |
15 | 8 |
|
16 | 9 | ```bash |
17 | 10 | git clone https://github.com/queue-xec/master |
18 | 11 | cd master |
19 | | - yarn # or npm install |
| 12 | + yarn # or npm install |
20 | 13 | ``` |
21 | 14 |
|
22 | | -```bash |
| 15 | +```bash |
23 | 16 | node cli.js --setup |
24 | | - ``` |
| 17 | +``` |
| 18 | + |
25 | 19 | Will prompt user to enter following info: |
26 | | -- `transferEncryptToken` Enter a 32 length alphanumeric string Or prompt to generate one on the fly |
27 | | -- `token` Enter at least 20 length alphanumeric string Or prompt to generate one on the fly |
| 20 | + |
| 21 | +- `transferEncryptToken` Enter a 32 length alphanumeric string Or prompt to generate one on the fly |
| 22 | +- `token` Enter at least 20 length alphanumeric string Or prompt to generate one on the fly |
28 | 23 |
|
29 | 24 | These info will saved and loaded in .env file at root Masters folder.Should be the same on all workers , to be able to communicate. |
30 | 25 |
|
31 | 26 | ## How to set job code to run in workers |
32 | 27 |
|
33 | | -Edit [taskFile](https://github.com/queue-xec/master/blob/devel/src/task.js) inner `run()` method |
34 | | -⚠️ Beware , this file should never modified in a remote Worker instance because will be overwritten by Master next time Worker instance initiated. |
| 28 | +Edit [taskFile](https://github.com/queue-xec/master/blob/devel/src/task.js) inner `run()` method ⚠️ Beware , this file should never modified in a remote Worker instance because will be overwritten by Master next time Worker instance initiated. |
| 29 | + |
35 | 30 | ```javascript |
36 | 31 | // Require here libs you passed from master as dependencies |
37 | 32 | // if needed here. |
38 | 33 | const { Big } = require('big.js'); |
39 | 34 | const Helper = require('./Helper.js'); |
40 | | -class Task { // task.js file will placed by default in WORKER_DIR /workplace/task.js |
41 | | - constructor() { |
42 | | - this.data = null; |
43 | | - // this.helper = new Helper(); |
44 | | - } |
45 | | - |
46 | | - /** |
47 | | - * Description: This method is the job procces. Receives job data (job) |
48 | | - * Must return job results (Object) , to delivered in master. |
49 | | - * @param {Object} job data from Master about job {id: Number , data: String (needs JSON.parse)} |
50 | | - * @returns {Object} result |
51 | | - */ |
52 | | - async run(job) { |
53 | | - console.dir(job); |
54 | | - const data = JSON.parse(job.data); |
55 | | - // code to solve the problem , in remote Workers |
56 | | - return {}; |
57 | | - } |
| 35 | +class Task { |
| 36 | + // task.js file will placed by default in WORKER_DIR /workplace/task.js |
| 37 | + constructor() { |
| 38 | + this.data = null; |
| 39 | + // this.helper = new Helper(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Description: This method is the job procces. Receives job data (job) |
| 44 | + * Must return job results (Object) , to delivered in master. |
| 45 | + * @param {Object} job data from Master about job {id: Number , data: String (needs JSON.parse)} |
| 46 | + * @returns {Object} result |
| 47 | + */ |
| 48 | + async run(job) { |
| 49 | + console.dir(job); |
| 50 | + const data = JSON.parse(job.data); |
| 51 | + // code to solve the problem , in remote Workers |
| 52 | + return {}; |
| 53 | + } |
58 | 54 | } |
59 | 55 |
|
60 | 56 | module.exports = Task; |
61 | | - |
62 | 57 | ``` |
63 | 58 |
|
64 | | - |
65 | 59 | ## Example: |
| 60 | + |
66 | 61 | ```js |
67 | | - const Master = require('queue-xec-master') |
68 | | - function resultCollect(result){ // handle here incoming results from workers.. |
69 | | - console.dir(result) |
70 | | - } |
71 | | -async function run(){ |
72 | | - const mm = new Master({ |
73 | | - onResults: resultCollect, |
74 | | - execAssets: { |
75 | | - dependencies: ['big.js' ,'moment'], // pass Worker dependencies |
76 | | - files: [ |
77 | | - { masterPath: '/src/task.js', name: 'task.js', workerPath: '/workplace/task.js' }, // if task.js file not passed to workers , Master will use default one located in /src/task.js , here you can ovverride it by changing above details |
78 | | - { masterPath: '/src/Logger.js', name: 'Logger.js', workerPath: '/workplace/Logger.js' }, |
79 | | - { masterPath: '/src/Helper.js', name: 'Helper.js', workerPath: '/workplace/Helper.js' }, |
80 | | - ], |
81 | | - /* masterPath and workerPath are not absolute , NEVER access files out of their process.cwd() path. |
| 62 | +const Master = require('queue-xec-master'); |
| 63 | +function resultCollect(result) { |
| 64 | + // handle here incoming results from workers.. |
| 65 | + console.dir(result); |
| 66 | +} |
| 67 | +async function run() { |
| 68 | + const mm = new Master({ |
| 69 | + onResults: resultCollect, |
| 70 | + execAssets: { |
| 71 | + dependencies: ['big.js', 'moment'], // pass Worker dependencies |
| 72 | + files: [ |
| 73 | + { |
| 74 | + masterPath: '/src/task.js', |
| 75 | + name: 'task.js', |
| 76 | + workerPath: '/workplace/task.js', |
| 77 | + }, // if task.js file not passed to workers , Master will use default one located in /src/task.js , here you can ovverride it by changing above details |
| 78 | + { |
| 79 | + masterPath: '/src/Logger.js', |
| 80 | + name: 'Logger.js', |
| 81 | + workerPath: '/workplace/Logger.js', |
| 82 | + }, |
| 83 | + { |
| 84 | + masterPath: '/src/Helper.js', |
| 85 | + name: 'Helper.js', |
| 86 | + workerPath: '/workplace/Helper.js', |
| 87 | + }, |
| 88 | + ], |
| 89 | + /* masterPath and workerPath are not absolute , NEVER access files out of their process.cwd() path. |
82 | 90 | Are relative to their process current location , respectively */ |
83 | | - }, |
84 | | - }); |
85 | | - const dummy = { |
86 | | - x: 1 , |
87 | | - y: 2 |
88 | | - }; |
89 | | - |
90 | | - let cnt = 0; |
91 | | - for (let i = 0; i < 5; i++) { |
92 | | - const payload = { |
93 | | - id: cnt, |
94 | | - data: JSON.stringify(dummy), |
95 | | - |
| 91 | + }, |
| 92 | + }); |
| 93 | + const dummy = { |
| 94 | + x: 1, |
| 95 | + y: 2, |
96 | 96 | }; |
97 | | - await mm.pushNewJob(payload).catch((e) => console.log(e)); |
98 | | - cnt += 1; |
99 | | - } |
| 97 | + |
| 98 | + let cnt = 0; |
| 99 | + for (let i = 0; i < 5; i++) { |
| 100 | + const payload = { |
| 101 | + id: cnt, |
| 102 | + data: JSON.stringify(dummy), |
| 103 | + }; |
| 104 | + await mm.pushNewJob(payload).catch((e) => console.log(e)); |
| 105 | + cnt += 1; |
| 106 | + } |
100 | 107 | } |
101 | 108 | ``` |
102 | 109 |
|
103 | 110 | ### ⚠️ Under development ⚠️ |
104 | 111 |
|
105 | 112 | [](https://github.com/queue-xec/master/blob/devel/LICENSE) |
106 | 113 |
|
107 | | - |
108 | 114 | ## > Contributors < |
| 115 | + |
109 | 116 | <a href="https://github.com/queue-xec/master/graphs/contributors"> |
110 | 117 | <img src="https://contrib.rocks/image?repo=queue-xec/master" /> |
111 | | -</a> |
| 118 | +</a> |
0 commit comments