Skip to content

Commit

Permalink
fix: 恢复函数调试调用、增加触发器调用编译功能;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 29, 2021
1 parent 1d8dbe5 commit 0eb8163
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/app-server/src/api/function-log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Constants } from "../constants"
import { Globals } from "../lib/globals"

const db = Globals.db
Expand All @@ -10,7 +11,7 @@ const db = Globals.db
export async function addFunctionLog(data: any) {
if(!data) return null

const r = await db.collection('function_logs')
const r = await db.collection(Constants.function_log_collection)
.add(data)

return r.id
Expand Down
5 changes: 3 additions & 2 deletions packages/app-server/src/api/trigger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Constants } from "../constants"
import { Globals } from "../lib/globals"

const db = Globals.db
Expand All @@ -8,7 +9,7 @@ const db = Globals.db
* @returns
*/
export async function getTriggers(status = 1) {
const r = await db.collection('triggers')
const r = await db.collection(Constants.trigger_collection)
.where({ status: status })
.get()

Expand All @@ -21,7 +22,7 @@ export async function getTriggers(status = 1) {
* @returns
*/
export async function getTriggerById(id: string) {
const r = await db.collection('triggers')
const r = await db.collection(Constants.trigger_collection)
.where({ _id: id })
.getOne()

Expand Down
10 changes: 10 additions & 0 deletions packages/app-server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ export const Constants = {
*/
function_collection: '__deployed__functions',

/**
* 云函数触发器集合名
*/
trigger_collection: '__deployed__triggers',

/**
* 访问策略集合名
*/
policy_collection: '__deployed__rules',

/**
* 函数日志的集合名
*/
function_log_collection: "__function_logs"
}

deepFreeze(Constants)
10 changes: 9 additions & 1 deletion packages/app-server/src/lib/scheduler/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getFunctionById } from "../../api/function"
import { addFunctionLog } from "../../api/function-log"
import { CloudFunction, TriggerScheduler } from "cloud-function-engine"
import { createLogger } from "../logger"
import assert = require("assert")


const logger = createLogger('scheduler')
Expand All @@ -19,8 +20,15 @@ export class FrameworkScheduler extends TriggerScheduler {
* @returns
*/
async getFunctionById(func_id: string): Promise<CloudFunction>{
assert(func_id)
const funcData = await getFunctionById(func_id)
return new CloudFunction(funcData)
assert.ok(funcData)

const func = new CloudFunction(funcData)
if(!func.compiledCode) {
func.compile2js()
}
return func
}

/**
Expand Down
22 changes: 10 additions & 12 deletions packages/app-server/src/router/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as path from 'path'
import * as uuid from 'uuid'
import { getFunctionByName } from '../../api/function'
import { Globals } from '../../lib/globals'
import { Constants } from '../../constants'

// 设置云函数中的加载函数
CloudFunction.require_func = Globals.require_func
Expand Down Expand Up @@ -36,9 +37,6 @@ FunctionRouter.all('/:name', handleInvokeFunction) // alias for /invoke/

/**
* 调用云函数
* @param req
* @param res
* @returns
*/
async function handleInvokeFunction(req: Request, res: Response) {
const requestId = req['requestId']
Expand All @@ -52,13 +50,13 @@ async function handleInvokeFunction(req: Request, res: Response) {

const debug = req.query?.debug ?? false

// 调试权限验证: @TODO 暂时先注释掉,需要通过令牌来控制调试权限
// if (debug) {
// const code = await checkPermission(req['auth']?.uid, 'function.debug')
// if (code) {
// return res.status(code).send('permission denied')
// }
// }
// 调试权限验证: @TODO 需要通过令牌来控制调试权限
if (debug) {
const auth = req['auth']
if(!auth || auth.type !== 'admin') {
return res.status(403).send('permission denied')
}
}

const funcData = await getFunctionByName(func_name)
if (!funcData) {
Expand All @@ -81,7 +79,7 @@ async function handleInvokeFunction(req: Request, res: Response) {
if(debug || !func.compiledCode) {
func.compile2js()

await db.collection('functions')
await db.collection(Constants.function_collection)
.doc(func.id)
.update({ compiledCode: func.compiledCode, updated_at: Date.now()})
}
Expand All @@ -100,7 +98,7 @@ async function handleInvokeFunction(req: Request, res: Response) {

// 将云函数调用日志存储到数据库
if(debug) {
await db.collection('function_logs')
await db.collection(Constants.function_log_collection)
.add({
requestId: requestId,
func_id: func.id,
Expand Down

0 comments on commit 0eb8163

Please sign in to comment.