Skip to content

Commit

Permalink
feat(sys-server): add local dev/debug config for app-service;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 1, 2021
1 parent abc84d1 commit 83877a7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ services:
ACCOUNT_DEFAULT_APP_QUOTA: 5
APP_SERVICE_DEPLOY_HOST: local-dev.host:8080 # `*.local-dev.host` always resolved to 127.0.0.1, used to local development
APP_SERVICE_DEPLOY_URL_SCHEMA: 'http'
DEBUG_BIND_HOST_APP_PATH: '${PWD}/packages/app-service'
command: npx nodemon
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
Expand Down
17 changes: 15 additions & 2 deletions packages/system-server/src/api/function.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-10-08 00:46:05
* @LastEditTime: 2021-11-01 16:55:15
* @Description:
*/

import { Constants } from "../constants"
import { DatabaseAgent } from "../lib/db-agent"
import { CloudFunctionStruct } from "cloud-function-engine"
import { ClientSession } from 'mongodb'
import { ClientSession, ObjectId } from 'mongodb'
import * as assert from 'assert'
import { logger } from "../lib/logger"
import { ApplicationStruct, getApplicationDbAccessor } from "./application"
Expand Down Expand Up @@ -51,6 +51,19 @@ export async function getFunctionByName(appid: string, func_name: string) {
return doc
}

/**
* Load function data by id
* @param func_name
* @returns
*/
export async function getFunctionById(appid: string, func_id: ObjectId) {
const db = DatabaseAgent.db
const doc = await db.collection<FunctionStruct>(Constants.cn.functions)
.findOne({ _id: func_id, appid })

return doc
}


/**
* Publish functions
Expand Down
9 changes: 9 additions & 0 deletions packages/system-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,13 @@ export default class Config {
static get APP_SERVICE_CPU_SHARES(): number {
return (process.env.APP_SERVICE_MEMORY_LIMIT ?? 256) as number
}

/**
* DEBUG: the app-service path that bind to app-service container
* This env var should only be set while debugging app service,
* otherwise always keep this env var value be empty
*/
static get DEBUG_BIND_HOST_APP_PATH(): string | undefined {
return process.env.DEBUG_BIND_HOST_APP_PATH ?? undefined
}
}
11 changes: 9 additions & 2 deletions packages/system-server/src/lib/service-driver/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export class DockerContainerServiceDriver {
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

let binds = []
if (Config.DEBUG_BIND_HOST_APP_PATH) {
binds = [`${Config.DEBUG_BIND_HOST_APP_PATH}:/app`]
}

const container = await this.docker.createContainer({
Image: imageName,
Cmd: ['node', `--max_old_space_size=${max_old_space_size}`, './dist/index.js'],
Expand All @@ -40,8 +46,9 @@ export class DockerContainerServiceDriver {
},
HostConfig: {
Memory: memoryLimit * 1024 * 1024,
CpuShares: cpuShares
}
CpuShares: cpuShares,
Binds: binds
},
})

logger.debug(`create container ${container.id} of app ${app.appid}`)
Expand Down
12 changes: 3 additions & 9 deletions packages/system-server/src/router/function/get.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-10-08 00:39:05
* @LastEditTime: 2021-11-01 16:56:16
* @Description:
*/

import { CloudFunctionStruct } from 'cloud-function-engine'
import { Request, Response } from 'express'
import { ObjectId } from 'mongodb'
import { ApplicationStruct } from '../../api/application'
import { FunctionStruct } from '../../api/function'
import { FunctionStruct, getFunctionById } from '../../api/function'
import { checkPermission } from '../../api/permission'
import { Constants } from '../../constants'
import { permissions } from '../../constants/permissions'
Expand Down Expand Up @@ -93,13 +93,7 @@ export async function handleGetFunctionById(req: Request, res: Response) {
return res.status(code).send()
}

// do db query
const db = DatabaseAgent.db
const doc = await db.collection<CloudFunctionStruct>(Constants.cn.functions)
.findOne({
_id: new ObjectId(func_id),
appid: app.appid
})
const doc = await getFunctionById(app.appid, new ObjectId(func_id))

return res.send({ data: doc })
}
Expand Down
15 changes: 7 additions & 8 deletions packages/system-server/src/router/function/update.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-05 02:11:39
* @LastEditTime: 2021-10-08 01:44:47
* @LastEditTime: 2021-11-01 17:02:59
* @Description:
*/


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

const func = await db.collection(Constants.cn.functions)
.findOne({
_id: new ObjectId(func_id),
appid: app.appid
})
const func = await getFunctionById(app.appid, new ObjectId(func_id))

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

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

// update cloud function
const ret = await db.collection(Constants.cn.functions)
await db.collection(Constants.cn.functions)
.updateOne({
_id: new ObjectId(func_id),
appid: app.appid
}, {
$set: data
})

return res.send({ data: ret })
const doc = await getFunctionById(app.appid, new ObjectId(func_id))

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

0 comments on commit 83877a7

Please sign in to comment.