Skip to content

Commit

Permalink
feat(sys-server): add runtime metrics to app;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 1, 2021
1 parent 543052b commit a69ed61
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
9 changes: 8 additions & 1 deletion packages/system-server/src/api/application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-28 22:00:45
* @LastEditTime: 2021-10-13 18:27:20
* @LastEditTime: 2021-11-01 12:19:58
* @Description: Application APIs
*/

Expand Down Expand Up @@ -34,6 +34,13 @@ export interface ApplicationStruct {
log_level?: string
enable_cloud_function_log?: string
}
runtime: {
image: string
metrics: {
cpu_shares?: number
memory?: number
}
}
collaborators: {
uid: string
roles: string[]
Expand Down
14 changes: 10 additions & 4 deletions packages/system-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ export default class Config {
}

/**
* The max old space size of node vm of application service, default is 256mb
* @see --max_old_space_size of node argv
* The max memory limit of application service, default is 256 in mb
*/
static get APP_SERVICE_NODE_MAX_OLD_SPACE_SIZE(): number {
return (process.env.APP_SERVICE_NODE_MAX_OLD_SPACE_SIZE ?? 256) as number
static get APP_SERVICE_MEMORY_LIMIT(): number {
return (process.env.APP_SERVICE_MEMORY_LIMIT ?? 256) as number
}

/**
* The cpu shares of application service, default is 256
*/
static get APP_SERVICE_CPU_SHARES(): number {
return (process.env.APP_SERVICE_MEMORY_LIMIT ?? 256) as number
}
}
15 changes: 11 additions & 4 deletions packages/system-server/src/lib/service-driver/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@ export class DockerContainerServiceDriver {
*/
async createService(app: ApplicationStruct) {
const uri = getApplicationDbUri(app)
const max_old_space_size = Config.APP_SERVICE_NODE_MAX_OLD_SPACE_SIZE
const memoryLimit = app.runtime?.metrics?.memory ?? Config.APP_SERVICE_MEMORY_LIMIT
const max_old_space_size = ~~(memoryLimit * 0.8)
// if no cpu-shares set, use memory limit as it
const cpuShares = app.runtime?.metrics?.cpu_shares ?? Config.APP_SERVICE_CPU_SHARES
const imageName = app.runtime?.image ?? Config.APP_SERVICE_IMAGE
const logLevel = Config.LOG_LEVEL
const container = await this.docker.createContainer({
Image: Config.APP_SERVICE_IMAGE,
Cmd: ['node', `--max_old_space_size=${max_old_space_size}`, './dist/start.js'],
Image: imageName,
Cmd: ['node', `--max_old_space_size=${max_old_space_size}`, './dist/index.js'],
name: `app_${app.appid}`,
Env: [
`DB=${app.config.db_name}`,
`DB_URI=${uri}`,
`LOG_LEVEL=debug`,
`LOG_LEVEL=${logLevel}`,
`ENABLE_CLOUD_FUNCTION_LOG=always`,
`SERVER_SECRET_SALT=${app.config.server_secret_salt}`
],
ExposedPorts: {
"8000/tcp": {}
},
HostConfig: {
Memory: memoryLimit * 1024 * 1024,
CpuShares: cpuShares
}
})

Expand Down
16 changes: 13 additions & 3 deletions packages/system-server/src/router/application/create.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-31 15:00:04
* @LastEditTime: 2021-10-08 01:25:43
* @LastEditTime: 2021-11-01 13:16:43
* @Description:
*/

import * as assert from 'assert'
import { Request, Response } from 'express'
import { getAccountById } from '../../api/account'
import { ApplicationStruct, createApplicationDb, generateAppid, getMyApplications } from '../../api/application'
import { ApplicationStruct, createApplicationDb, generateAppid, getApplicationByAppid, getMyApplications } from '../../api/application'
import Config from '../../config'
import { Constants } from '../../constants'
import { ErrorCodes } from '../../constants/error-code'
import { DatabaseAgent } from '../../lib/db-agent'
Expand Down Expand Up @@ -55,6 +56,13 @@ export async function handleCreateApplication(req: Request, res: Response) {
db_password: db_password,
server_secret_salt: generatePassword(64),
},
runtime: {
image: Config.APP_SERVICE_IMAGE,
metrics: {
cpu_shares: Config.APP_SERVICE_CPU_SHARES,
memory: Config.APP_SERVICE_MEMORY_LIMIT
}
},
created_at: now,
updated_at: now
}
Expand All @@ -71,7 +79,9 @@ export async function handleCreateApplication(req: Request, res: Response) {
const result = await createApplicationDb(data)
logger.debug(`create application db ${db_name}`, result)

const app = await getApplicationByAppid(appid)

return res.send({
data: ret
data: app
})
}

0 comments on commit a69ed61

Please sign in to comment.