-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
130 lines (121 loc) · 3.85 KB
/
start.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
import process from 'node:process';
import { isMainThread, parentPort } from 'node:worker_threads';
import 'dotenv/config';
import vine from '@vinejs/vine';
import logger from '#logger';
import { init, stop } from '#vendor/start/server.js';
import configApp from '#config/app.js';
import db from '#database/db.js';
import redis from '#database/redis.js';
import schemas from '#app/validate/schemas/schemas.js';
import validators from '#vendor/start/validators.js';
// import '#app/routes/httpRoutes.js';
// import '#app/routes/wsRoutes.js';
import { getListRoutes, getWsRoutes, routesHandler } from "#vendor/start/router.js";
import httpRoutes from '#app/routes/httpRoutes.js';
import wsRoutes from '#app/routes/wsRoutes.js';
// import { getWsRoutes } from '#vendor/start/router.js';
logger.info(configApp);
// console.log({ configApp })
const migrationDB = async () => {
await db.migrate.up({ directory: './database/migrations' });
};
const testRedis = async () => {
await redis.set('test', Date.now().toString());
};
const compileValidateSchema = () => {
const schemaKeys = Object.keys(schemas);
schemaKeys.forEach((key) => {
validators[key] = vine.compile(schemas[key]);
});
};
const start = async () => {
try {
/* eslint-disable no-undef */
process.title = configApp.appName;
logger.info(
'use module: uws_' +
process.platform +
'_' +
process.arch +
'_' +
process.versions.modules +
'.node',
);
compileValidateSchema();
// const wsRoutes = getWsRoutes();
// logger.info(wsRoutes);
if (configApp.startMigration) {
await migrationDB();
logger.info('migrate success');
}
await testRedis();
logger.info('test redis success');
routesHandler(httpRoutes, false);
console.log(getListRoutes());
routesHandler(wsRoutes, true);
// console.log(getListRoutes());
await init();
process.on('SIGINT', stopSIGINT);
process.on('SIGHUP', stopSIGHUP);
process.on('SIGTERM', stopSIGTERM);
process.on('uncaughtException', stopUncaughtException);
} catch (err) {
/* eslint-disable no-undef */
console.error(err);
process.exit(1);
}
};
const removeListeners = () => {
process.removeListener('SIGINT', stopSIGINT);
process.removeListener('SIGHUP', stopSIGHUP);
process.removeListener('SIGTERM', stopSIGTERM);
process.removeListener('uncaughtException', stopUncaughtException);
};
const stopHandler = (type) => {
stop(type);
removeListeners();
};
const stopSIGINT = () => {
logger.info('stop SIGINT');
stopHandler('SIGINT');
process.exit(1);
};
const stopSIGHUP = () => {
logger.info('stop SIGHUP');
stopHandler('SIGHUP');
process.exit(1);
};
const stopSIGTERM = () => {
logger.info('stop SIGTERM');
stopHandler('SIGTERM');
process.exit(1);
};
const stopUncaughtException = (err, origin) => {
logger.error('event uncaughtException');
logger.error(err);
logger.error(origin);
// console.error(err);
// console.error(origin);
stopHandler('uncaughtException');
process.exit(1);
};
start().then(() => {
logger.info('start success');
if (!isMainThread) {
parentPort.postMessage('start success');
parentPort.on('message', (message) => {
if (message.command === 'shutdown') {
logger.info('message.command === shutdown');
stop('MainThread');
removeListeners();
setTimeout(() => {
parentPort.close().then(() => {
logger.info('parentPort.close');
//process.exit(0);
});
}, 100);
}
});
}
});