Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Commit

Permalink
fix: 修改多线程模式可以自定义
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Jun 4, 2020
1 parent 174902f commit ff50771
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .env
Expand Up @@ -2,6 +2,8 @@
PORT=4365
# 根路由
ROOT_URL=''
# 启用多线程 true/false
ENABLE_CLUSTER=false
# 超时时间(毫秒)
TIMEOUT_MAX_AGE=15000

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Expand Up @@ -9,6 +9,7 @@ services:
environment:
PORT: 4365
NODE_ENV: production
ENABLE_CLUSTER: "true"
CACHE_TYPE: redis
REDIS_URL: "redis://redis:6379/"
REDIS_HOST: "redis"
Expand Down
7 changes: 4 additions & 3 deletions src/bin/www.ts
Expand Up @@ -8,12 +8,12 @@ import colors = require('colors')
import moduleAlias from 'module-alias'
moduleAlias.addAlias('@', path.join(__dirname, '../'))
import { app } from '../app'
import { PORT, IS_DEBUG } from '@/config'
import { PORT, IS_DEBUG, ENABLE_CLUSTER } from '@/config'
import { Log } from '@/utils'
import { errorLogger } from '@/middleware'
const httpPort = normalizePort(PORT)
const numCPUs = os.cpus().length
if (cluster.isMaster && !IS_DEBUG) {
if (ENABLE_CLUSTER && cluster.isMaster && !IS_DEBUG) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork()
}
Expand Down Expand Up @@ -64,7 +64,8 @@ function onError(error: any): void {
*/
function onListening(): void {
console.log('################################################')
Log.info(`worker ${cluster.worker.id} 运行地址为 http://127.0.0.1:${httpPort}`)
const workerId = cluster?.worker?.id || ''
Log.info(`worker ${workerId} 运行地址为 http://127.0.0.1:${httpPort}`)
console.log('################################################')
}

Expand Down
2 changes: 2 additions & 0 deletions src/config/index.ts
Expand Up @@ -34,6 +34,8 @@ if (IS_DEBUG) {
export const PORT = Number(env.PORT || 4365)
// 路由根路径
export const ROOT_URL = env.ROOT_URL || ''
// 启用多线程
export const ENABLE_CLUSTER = env.ENABLE_CLUSTER === 'true'
// 超时时间
export const TIMEOUT_MAX_AGE = Number(env.TIMEOUT_MAX_AGE || 15000)

Expand Down
2 changes: 2 additions & 0 deletions src/controllers/status.ts
@@ -1,6 +1,7 @@
import os = require('os')
import path = require('path')
import fs = require('fs-extra')
import cluster = require('cluster')
import Koa = require('koa')
import pidusage from 'pidusage'
import git from 'git-rev-sync'
Expand Down Expand Up @@ -28,6 +29,7 @@ export async function status(ctx: Koa.Context, next: Koa.Next) {
nodeVersion: process.versions.node,
gitHash,
ip: ctx.ip,
workerId: cluster?.worker?.id,
stat: {
memory: dataFormat(stat.memory),
cpu: `${stat.cpu} %`,
Expand Down

0 comments on commit ff50771

Please sign in to comment.