forked from labring/laf
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-server): impl start.ts to support cluster process manage;
- Loading branch information
Showing
4 changed files
with
75 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |