Skip to content

Commit 83877a7

Browse files
committed
feat(sys-server): add local dev/debug config for app-service;
1 parent abc84d1 commit 83877a7

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ services:
5858
ACCOUNT_DEFAULT_APP_QUOTA: 5
5959
APP_SERVICE_DEPLOY_HOST: local-dev.host:8080 # `*.local-dev.host` always resolved to 127.0.0.1, used to local development
6060
APP_SERVICE_DEPLOY_URL_SCHEMA: 'http'
61+
DEBUG_BIND_HOST_APP_PATH: '${PWD}/packages/app-service'
6162
command: npx nodemon
6263
volumes:
6364
- /var/run/docker.sock:/var/run/docker.sock:ro

packages/system-server/src/api/function.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-10-08 00:46:05
4+
* @LastEditTime: 2021-11-01 16:55:15
55
* @Description:
66
*/
77

88
import { Constants } from "../constants"
99
import { DatabaseAgent } from "../lib/db-agent"
1010
import { CloudFunctionStruct } from "cloud-function-engine"
11-
import { ClientSession } from 'mongodb'
11+
import { ClientSession, ObjectId } from 'mongodb'
1212
import * as assert from 'assert'
1313
import { logger } from "../lib/logger"
1414
import { ApplicationStruct, getApplicationDbAccessor } from "./application"
@@ -51,6 +51,19 @@ export async function getFunctionByName(appid: string, func_name: string) {
5151
return doc
5252
}
5353

54+
/**
55+
* Load function data by id
56+
* @param func_name
57+
* @returns
58+
*/
59+
export async function getFunctionById(appid: string, func_id: ObjectId) {
60+
const db = DatabaseAgent.db
61+
const doc = await db.collection<FunctionStruct>(Constants.cn.functions)
62+
.findOne({ _id: func_id, appid })
63+
64+
return doc
65+
}
66+
5467

5568
/**
5669
* Publish functions

packages/system-server/src/config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,13 @@ export default class Config {
129129
static get APP_SERVICE_CPU_SHARES(): number {
130130
return (process.env.APP_SERVICE_MEMORY_LIMIT ?? 256) as number
131131
}
132+
133+
/**
134+
* DEBUG: the app-service path that bind to app-service container
135+
* This env var should only be set while debugging app service,
136+
* otherwise always keep this env var value be empty
137+
*/
138+
static get DEBUG_BIND_HOST_APP_PATH(): string | undefined {
139+
return process.env.DEBUG_BIND_HOST_APP_PATH ?? undefined
140+
}
132141
}

packages/system-server/src/lib/service-driver/container.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export class DockerContainerServiceDriver {
2424
const cpuShares = app.runtime?.metrics?.cpu_shares ?? Config.APP_SERVICE_CPU_SHARES
2525
const imageName = app.runtime?.image ?? Config.APP_SERVICE_IMAGE
2626
const logLevel = Config.LOG_LEVEL
27+
28+
let binds = []
29+
if (Config.DEBUG_BIND_HOST_APP_PATH) {
30+
binds = [`${Config.DEBUG_BIND_HOST_APP_PATH}:/app`]
31+
}
32+
2733
const container = await this.docker.createContainer({
2834
Image: imageName,
2935
Cmd: ['node', `--max_old_space_size=${max_old_space_size}`, './dist/index.js'],
@@ -40,8 +46,9 @@ export class DockerContainerServiceDriver {
4046
},
4147
HostConfig: {
4248
Memory: memoryLimit * 1024 * 1024,
43-
CpuShares: cpuShares
44-
}
49+
CpuShares: cpuShares,
50+
Binds: binds
51+
},
4552
})
4653

4754
logger.debug(`create container ${container.id} of app ${app.appid}`)

packages/system-server/src/router/function/get.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-08-30 16:51:19
4-
* @LastEditTime: 2021-10-08 00:39:05
4+
* @LastEditTime: 2021-11-01 16:56:16
55
* @Description:
66
*/
77

88
import { CloudFunctionStruct } from 'cloud-function-engine'
99
import { Request, Response } from 'express'
1010
import { ObjectId } from 'mongodb'
1111
import { ApplicationStruct } from '../../api/application'
12-
import { FunctionStruct } from '../../api/function'
12+
import { FunctionStruct, getFunctionById } from '../../api/function'
1313
import { checkPermission } from '../../api/permission'
1414
import { Constants } from '../../constants'
1515
import { permissions } from '../../constants/permissions'
@@ -93,13 +93,7 @@ export async function handleGetFunctionById(req: Request, res: Response) {
9393
return res.status(code).send()
9494
}
9595

96-
// do db query
97-
const db = DatabaseAgent.db
98-
const doc = await db.collection<CloudFunctionStruct>(Constants.cn.functions)
99-
.findOne({
100-
_id: new ObjectId(func_id),
101-
appid: app.appid
102-
})
96+
const doc = await getFunctionById(app.appid, new ObjectId(func_id))
10397

10498
return res.send({ data: doc })
10599
}

packages/system-server/src/router/function/update.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-09-05 02:11:39
4-
* @LastEditTime: 2021-10-08 01:44:47
4+
* @LastEditTime: 2021-11-01 17:02:59
55
* @Description:
66
*/
77

88

99
import { Request, Response } from 'express'
1010
import { ObjectId } from 'mongodb'
1111
import { ApplicationStruct } from '../../api/application'
12+
import { getFunctionById } from '../../api/function'
1213
import { checkPermission } from '../../api/permission'
1314
import { Constants } from '../../constants'
1415
import { permissions } from '../../constants/permissions'
@@ -99,11 +100,7 @@ export async function handleUpdateFunctionCode(req: Request, res: Response) {
99100
const body = req.body
100101
if (!body.code) return res.status(422).send('code cannot be empty')
101102

102-
const func = await db.collection(Constants.cn.functions)
103-
.findOne({
104-
_id: new ObjectId(func_id),
105-
appid: app.appid
106-
})
103+
const func = await getFunctionById(app.appid, new ObjectId(func_id))
107104

108105
if (!func) return res.status(422).send('function not found')
109106

@@ -129,13 +126,15 @@ export async function handleUpdateFunctionCode(req: Request, res: Response) {
129126
}
130127

131128
// update cloud function
132-
const ret = await db.collection(Constants.cn.functions)
129+
await db.collection(Constants.cn.functions)
133130
.updateOne({
134131
_id: new ObjectId(func_id),
135132
appid: app.appid
136133
}, {
137134
$set: data
138135
})
139136

140-
return res.send({ data: ret })
137+
const doc = await getFunctionById(app.appid, new ObjectId(func_id))
138+
139+
return res.send({ data: doc })
141140
}

0 commit comments

Comments
 (0)