@@ -70,6 +70,12 @@ const commander = program
|
||||
// other
|
||||
.option('--color', 'force enable shell color')
|
||||
|
||||
// debug
|
||||
.option(
|
||||
'--debug_memory_dump',
|
||||
'enable memory dump for debug, send SIGUSR2 to dump memory',
|
||||
)
|
||||
|
||||
commander.parse()
|
||||
|
||||
const argv = commander.opts()
|
||||
@@ -160,6 +166,8 @@ export const DEBUG_MODE = {
|
||||
logging: isDebugMode,
|
||||
httpRequestVerbose:
|
||||
argv.httpRequestVerbose ?? argv.http_request_verbose ?? true,
|
||||
memoryDump:
|
||||
(argv.debug_memory_dump || process.env.MX_DEBUG_MEMORY_DUMP) ?? false,
|
||||
}
|
||||
export const THROTTLE_OPTIONS = {
|
||||
ttl: seconds(argv.throttle_ttl ?? 10),
|
||||
|
||||
20
apps/core/src/dump.ts
Normal file
20
apps/core/src/dump.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import v8 from 'node:v8'
|
||||
import fs from 'node:fs'
|
||||
import { TEMP_DIR } from './constants/path.constant'
|
||||
|
||||
export function registerForMemoryDump() {
|
||||
function createHeapSnapshot() {
|
||||
const snapshotStream = v8.getHeapSnapshot()
|
||||
const localeDate = new Date().toLocaleString()
|
||||
const fileName = `${TEMP_DIR}/HeapSnapshot-${localeDate}.heapsnapshot`
|
||||
const fileStream = fs.createWriteStream(fileName)
|
||||
snapshotStream.pipe(fileStream).on('finish', () => {
|
||||
console.log('Heap snapshot saved to', fileName)
|
||||
})
|
||||
}
|
||||
|
||||
process.on('SIGUSR2', () => {
|
||||
console.log('SIGUSR2 received, creating heap snapshot...')
|
||||
createHeapSnapshot()
|
||||
})
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import cluster from 'node:cluster'
|
||||
import { logger } from './global/consola.global'
|
||||
import { isMainCluster } from './global/env.global'
|
||||
import { register } from './global/index.global'
|
||||
import { registerForMemoryDump } from './dump'
|
||||
import { DEBUG_MODE } from './app.config'
|
||||
|
||||
process.title = `Mix Space (${cluster.isPrimary ? 'master' : 'worker'}) - ${
|
||||
process.env.NODE_ENV
|
||||
@@ -40,6 +42,7 @@ async function main() {
|
||||
)
|
||||
}
|
||||
|
||||
DEBUG_MODE.memoryDump && registerForMemoryDump()
|
||||
if (CLUSTER.enable) {
|
||||
Cluster.register(
|
||||
Number.parseInt(CLUSTER.workers) || os.cpus().length,
|
||||
|
||||
Reference in New Issue
Block a user