-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (118 loc) · 4.84 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
________ ________ _____ _____ _____ _ ______
| ____\ \ / / ____| __ \ / ____|/ ____| /\ | | | ____|
| |__ \ \ / /| |__ | |__) | (___ | | / \ | | | |__
| __| \ \/ / | __| | _ / \___ \| | / /\ \ | | | __|
| |____ \ / | |____| | \ \ ____) | |____ / ____ \| |____| |____
|______| \/ |______|_| \_\_____/ \_____/_/ \_\______|______|
/ ____| | |
| | ___ _ __ _ __ ___ ___| |_
| | / _ \| '_ \| '_ \ / _ \/ __| __|
| |___| (_) | | | | | | | __/ (__| |_
\_____\___/|_| |_|_| |_|\___|\___|\__|
*/
/**
* @name Everscale connection provider
* @copyright SVOI.dev Labs - https://svoi.dev
* @license Apache-2.0
* @version 1.0
*/
//let WORKERS = 1;
let WORKERS = (require('os').cpus().length) * 2;
const WORKER_SUICIDE_TIME = 5 * 60 * 1000;//5 min
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', 'EverscaleConnect ' + 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('/ever', express.static('dist'));
App.expressApp.use('/tonclient.wasm', express.static('dist/tonclient.wasm'));
App.expressApp.use('/eversdk.wasm', express.static('dist/eversdk.wasm'));
App.expressApp.use('/examples', express.static('examples'));
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`);
if(WORKER_SUICIDE_TIME && WORKER_SUICIDE_TIME !== 0) {
setTimeout(() => {
console.log(`Worker ${process.pid} suicide`);
process.exit();
}, WORKER_SUICIDE_TIME + (Math.round(Math.random() * 10000)))
}
}
process.on('unhandledRejection', error => {
console.error(error);
});
process.on('uncaughtException', error => {
console.error(error);
});