Skip to content

Commit

Permalink
fix: 修复应用远程部署时未清理被删函数的触发器导致的 app error;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 9, 2021
1 parent c81342e commit cd151c2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
13 changes: 12 additions & 1 deletion packages/devops-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/devops-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"body-parser": "^1.19.0",
"cloud-function-engine": "^0.4.3",
"dayjs": "^1.10.6",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
Expand Down
40 changes: 32 additions & 8 deletions packages/devops-server/src/api/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { compileTs2js } from 'cloud-function-engine/dist/utils'
import { CloudFunctionStruct } from "cloud-function-engine"
import { ClientSession, ObjectId } from 'mongodb'
import * as assert from 'assert'

const db = Globals.sys_db

/**
Expand Down Expand Up @@ -112,7 +113,7 @@ export async function deployFunctions(functions: CloudFunctionStruct[]) {

async function _deployOneFunction(func: CloudFunctionStruct, session: ClientSession) {

await _deleteFunctionWithSameNameButNotId(func, session)
await _processFunctionWithSameNameButNotId(func, session)

const db = Globals.sys_accessor.db
const r = await db.collection(Constants.cn.functions).findOne({ _id: new ObjectId(func._id) }, { session })
Expand Down Expand Up @@ -140,15 +141,38 @@ async function _deployOneFunction(func: CloudFunctionStruct, session: ClientSess
}

/**
* 删除本地 _id 不同,但 name 相同的云函数(若存在)
* 处理本地 _id 不同,但 name 相同的云函数(若存在)
* @param func
*/
async function _deleteFunctionWithSameNameButNotId(func: CloudFunctionStruct, session: ClientSession) {
async function _processFunctionWithSameNameButNotId(func: CloudFunctionStruct, session: ClientSession) {
const db = Globals.sys_accessor.db
await db.collection(Constants.cn.functions).findOneAndDelete({
_id: {
$ne: new ObjectId(func._id)

// remove functions
const r = await db.collection(Constants.cn.functions).findOneAndDelete(
{
_id: {
$ne: new ObjectId(func._id)
},
name: func.name
},
{ session })

if (!r.value) {
return
}

Globals.logger.warn(`delete local func ${r?.value?._id} with same name with (${func._id}:${func.name}) but different id `)

// remove its triggers if any
const r0 = await db.collection(Constants.cn.triggers).deleteMany(
{
func_id: r?.value?._id?.toString()
},
name: func.name
}, { session })
{ session })

if (!r0.deletedCount) {
return
}

Globals.logger.warn(`delete triggers of func ${func._id} which been deleted`, r0)
}
14 changes: 11 additions & 3 deletions packages/devops-server/src/api/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,24 @@ export async function deployTriggers(triggers: any[]) {
}

async function _deployOneTrigger(trigger: any, session: ClientSession) {

const db = Globals.sys_accessor.db

// skip trigger with invalid func_id
const func = await db.collection(Constants.cn.functions).findOne({ _id: new ObjectId(trigger.func_id) })
if (!func) {
logger.warn(`skip trigger with invalid func_id: ${trigger.func_id}`, trigger)
return
}

const r = await db.collection(Constants.cn.triggers).findOne({ _id: new ObjectId(trigger._id) }, { session })

const data = {
...trigger
}

logger.debug('deploy trigger: ', data, r)
// if exists function

// if exists trigger
if (r) {
delete data['_id']
const ret = await db.collection(Constants.cn.triggers).updateOne({ _id: r._id }, {
Expand All @@ -109,7 +117,7 @@ async function _deployOneTrigger(trigger: any, session: ClientSession) {
return
}

// if new function
// if new trigger
data._id = new ObjectId(data._id) as any
const ret = await db.collection(Constants.cn.triggers).insertOne(data as any, { session })
assert(ret.insertedId, `deploy: add trigger ${trigger.name} occurred error`)
Expand Down

0 comments on commit cd151c2

Please sign in to comment.