molly-db 1.0.16 → 1.0.17

Sign up to get free protection for your applications and to get access to all the features.
package/main.js CHANGED
@@ -15,30 +15,12 @@ function config( _config ) {
15
15
 
16
16
  /* --------------------------------------------------------------------------------------- */
17
17
 
18
- function startWorker(_path,_self){
19
- return new Promise((response,reject)=>{
20
- const wrk = new worker.Worker(
21
- _path,{ workerData:_self,
22
- env: worker.SHARE_ENV,
23
- }); wrk.on('exit',(err)=>{ console.log(err);
24
- response(startWorker(_path,_self));
25
- }); wrk.on('message',(msg)=>{
26
- console.log(msg); response(msg)
27
- });
28
- });
29
- }
30
-
31
- /* --------------------------------------------------------------------------------------- */
32
-
33
18
  class molly_db{
34
19
 
35
- constructor( opt ){
36
- if( opt.pass )
37
- this.pass = opt.pass;
38
- this.port = opt.port || 27017;
39
- this.path = opt.path.replace(/^\./,process.cwd());
40
- this.workerPath = `${__dirname}/module/_worker_.js`;
41
- return startWorker( this.workerPath,this );
20
+ constructor( opt ){ if( opt.pass )
21
+ this.pass = opt.pass; this.port = opt.port || 27017;
22
+ this.path = opt.path.replace( /^\./,process.cwd() );
23
+ return require(`${__dirname}/module/_worker_.js`)(this);
42
24
  }
43
25
 
44
26
  }
@@ -2,13 +2,17 @@ const worker = require('worker_threads');
2
2
  const readline = require('readline');
3
3
  const crypto = require('./_crypto_');
4
4
  const { Buffer } = require('buffer');
5
+ const cluster = require('cluster');
5
6
  const fetch = require('axios');
6
7
  const http = require('http');
7
8
  const url = require('url');
8
9
  const fs = require('fs');
9
10
 
10
- const query = worker.workerData;
11
+ /* --------------------------------------------------------------------------------------- */
12
+
13
+ let query;
11
14
  const db = new Object();
15
+
12
16
  /* --------------------------------------------------------------------------------------- */
13
17
 
14
18
  function _init_(){
@@ -25,11 +29,22 @@ function app(req,res){
25
29
 
26
30
  /* --------------------------------------------------------------------------------------- */
27
31
 
28
- (()=>{ http.createServer( app ).listen( query.port,()=>{
29
- _init_().then(()=>{ worker.parentPort.postMessage({
30
- workerID: process.pid, port: query.port,
31
- protocol: 'HTTP', status: 'started',
32
- }); }).catch(e=>{ process.exit(1); });
33
- }); })();
32
+ module.exports = (args)=>{ query = args;
33
+ return new Promise((response,reject)=>{
34
+ if (cluster.isPrimary) { const worker = cluster.fork();
35
+ cluster.on('exit', (worker, code, signal) => { cluster.fork();
36
+ console.log(`worker ${worker.process.pid} died`);
37
+ }); worker.on('message', (msg)=>{ console.log(msg); response(); });
38
+ worker.on('exit', (msg)=>{ reject(msg); });
39
+ } else {
40
+ http.createServer( app ).listen( query.port,()=>{
41
+ _init_().then(()=>{ process.send({
42
+ workerID: process.pid, port: query.port,
43
+ protocol: 'HTTP', status: 'started',
44
+ })}).catch(e=>{ console.log(e); });
45
+ });
46
+ }
47
+ });
48
+ }
34
49
 
35
50
  /* --------------------------------------------------------------------------------------- */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "license": "MIT",
3
3
  "main": "main.js",
4
- "version": "1.0.16",
4
+ "version": "1.0.17",
5
5
  "name": "molly-db",
6
6
  "author": "bececrazy",
7
7
  "scripts": {