Skip to content

Commit

Permalink
feat(app-server): impl start.ts to support cluster process manage;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 17, 2021
1 parent 7121988 commit 64b2a74
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/app-server/ecosystem.config.js.tpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
apps: {
script: 'dist/index.js',
name: 'less',
interpreter_args: '--experimental-vm-modules',
name: 'app_server',
interpreter_args: '--max_old_space_size=256',
watch: './dist',
// max_memory_restart: '256M'
}
Expand Down
28 changes: 14 additions & 14 deletions packages/app-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/app-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"typings": "./dist/index.d.ts",
"private": "true",
"scripts": {
"start": "node ./dist/index.js",
"start": "node ./dist/start.js",
"build": "npx tsc -p tsconfig.json && npm run create-internal-pkg",
"watch": "npx tsc -p tsconfig.json -w",
"create-internal-pkg": "node ./scripts/create-internal-package.js",
Expand Down
58 changes: 58 additions & 0 deletions packages/app-server/src/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-16 15:55:37
* @LastEditTime: 2021-08-17 14:41:01
* @Description:
* The start script entry: manage service process by cluster process mode, restart service process while error occurred.
* You can also run service directly by launching `index.ts`.
*/

import * as cluster from 'cluster'
import { Worker } from 'cluster'
import { createLogger } from './lib/logger'
import * as fs from 'fs'
import { join } from 'path'
import { debounce } from 'lodash'
import { execSync } from 'child_process'


if (cluster.isMaster) {
const logger = createLogger('primary')
logger.info(`Primary ${process.pid} is running`)
let worker: Worker = null

const forkServer = debounce(() => {
worker = cluster.fork()
}, 1000)

forkServer()

/**
* start service process while ones dead
*/
cluster.on('exit', (worker: Worker, code: number, signal: number) => {
logger.info(`worker ${worker.process.pid} died with code ${code}, signal ${signal}`)
forkServer()
})

/**
* watch package.json, restart service process when new package installed
*/
fs.watch(join(__dirname, '../package.json'))
.on('change', (type, filename) => {
logger.info(type, filename)

// create internal package since npm had cleaned the internal package `@/cloud-sdk` after `npm install` running
const ret = execSync("npm run create-internal-pkg")
logger.info(ret.toString())

// kill worker to reload packages
if (worker) {
logger.info(`worker ${worker.process.pid} would be killed`)
worker.kill()
}
})
} else {
// run the app server
require('./index')
}

0 comments on commit 64b2a74

Please sign in to comment.