-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (102 loc) · 4.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*_______ ____ _ _ _____
|__ __/ __ \| \ | |/ ____|
| | | | | | \| | (_____ ____ _ _ __
| | | | | | . ` |\___ \ \ /\ / / _` | '_ \
| | | |__| | |\ |____) \ V V / (_| | |_) |
|_| \____/|_| \_|_____/ \_/\_/ \__,_| .__/
| |
|_| */
/**
* @name TONSwap project - tonswap.com
* @copyright SVOI.dev Labs - https://svoi.dev
* @license Apache-2.0
*/
//let WORKERS = 1;
let WORKERS = (require('os').cpus().length) * 2;
const {FavoritoApp} = require('favorito');
const fs = require('fs');
const bodyParser = require('body-parser');
const cluster = require('cluster');
const config = require(__dirname + '/' + process.argv[2] ?? __dirname + '/config.js');
if(config.maxWorkers) {
WORKERS = config.maxWorkers;
}
if(cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
console.log('Starting', WORKERS, 'workers');
/**
* Получает сообщения от воркеров и распространяет их по другим воркерам
* @param msg
*/
function messageHandler(msg) {
if(msg.cmd) {
//console.log('WORKER MSG', msg);
//Broadcast message to all workers
for (const id in cluster.workers) {
try {
cluster.workers[id].send(msg);
} catch (e) {
console.log('CLUSTER SEND ERROR', e);
}
}
}
}
// Fork workers.
for (let i = 0; i < WORKERS; i++) {
let worker = cluster.fork();
worker.on('message', messageHandler);
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died. Spawn new.`);
console.log(code, signal);
let newWorker = cluster.fork();
newWorker.on('message', messageHandler);
});
} else {
(async () => {
const express = require('express');
const App = new FavoritoApp(__dirname + '/' + process.argv[2] ?? __dirname + '/config.js', 'conFederation ' + process.pid);
await App.init();
App.expressApp.use(bodyParser.json({limit: '50mb'}));
App.expressApp.disable('view cache');
App.expressApp.set('cache', false);
App.expressApp.set('etag', false);
App.expressApp.use('/modules/freeton', express.static('node_modules/freeton/src'));
App.expressApp.use('/modules/ton-client-web-js', express.static('node_modules/ton-client-web-js/'));
App.expressApp.use('/ton', express.static('dist'));
App.expressApp.use('/tonclient.wasm', express.static('dist/tonclient.wasm'));
const Twig = require('twig');
const Utils = require('./modules/utils/utils')
Twig.extendFilter("shortenPubkey", (text) => Utils.shortenPubkey(text));
Twig.extendFilter("unsignedNumberToSigned", (text, args) => Utils.unsignedNumberToSigned(text, args[0]));
Twig.extendFilter("numberToUnsignedNumber", (text, args) => Utils.numberToUnsignedNumber(text, args[0]));
Twig.extendFilter("numberToPercent", (text, args) => Utils.numberToPercent(text));
Twig.extendFilter('numberToPretty', (text, args) => Utils.numberToPretty(text, args[0]));
await App.start();
App.on('error', (error) => {
console.log('ERR', error);
})
//App.db.newdb.db.options.logging = false;
//let config = App.config;
//let db = App.db.default;
})()
process.on('unhandledRejection', error => {
console.error(error);
});
process.on('uncaughtException', error => {
console.error(error);
});
/*setInterval(() => {
process.send({cmd: 'test', data: {a: 1, b: 2}});
}, 5000)
process.on('message', (msg) => {
console.log('WORKER NEW MESSAGE', process.pid, msg)
});*/
//console.log(`Worker ${process.pid} started`);
}
process.on('unhandledRejection', error => {
console.error(error);
});
process.on('uncaughtException', error => {
console.error(error);
});