Skip to content

Commit

Permalink
支持自行进程守护
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Nov 2, 2021
1 parent 59cef40 commit 9f33944
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ public/
.env
/cache
dist
pkg
1 change: 1 addition & 0 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Request = express.Request
import Response = express.Response
import Timeout = NodeJS.Timeout
import Socket = SocketIOClient.Socket
import {cwd} from 'process'

interface IFileList {
files: {path: string; hash: string; size: number}[]
Expand Down
26 changes: 21 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import 'source-map-support'
import {fork, isMaster} from 'cluster'
import {config} from 'dotenv'
import {bootstrap} from './bootstrap'
import * as fs from 'fs'
import * as path from 'path'
import ms = require('ms')

const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json'), 'utf8'))

config()
bootstrap(packageJson.version)
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
if (process.env.NO_DAEMON || !isMaster) {
bootstrap(packageJson.version)
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})
}

if (!process.env.NO_DEMAON && isMaster) {
forkWorker()
}

function forkWorker(): void {
const cluster = fork()
cluster.on('exit', () => {
console.log(`工作进程 ${cluster.id} 异常退出,60秒后重启`)
setTimeout(() => forkWorker(), ms('60s'))
})
}

0 comments on commit 9f33944

Please sign in to comment.