Skip to content

Commit

Permalink
fix(sys-server): fix remote deploy got duplicated error, dropped func…
Browse files Browse the repository at this point in the history
…/policy _id;
  • Loading branch information
maslow committed Sep 10, 2021
1 parent b188f8d commit 1da03a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 73 deletions.
56 changes: 15 additions & 41 deletions packages/system-server/src/api/function.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-09-10 11:43:37
* @LastEditTime: 2021-09-10 22:19:43
* @Description:
*/

import { Constants } from "../constants"
import { DatabaseAgent } from "../lib/db-agent"
import { compileTs2js } from 'cloud-function-engine/dist/utils'
import { CloudFunctionStruct } from "cloud-function-engine"
import { ClientSession, ObjectId } from 'mongodb'
import { ClientSession } from 'mongodb'
import * as assert from 'assert'
import { logger } from "../lib/logger"
import { ApplicationStruct, getApplicationDbAccessor } from "./application"
Expand Down Expand Up @@ -130,55 +130,29 @@ export async function deployFunctions(appid: string, functions: FunctionStruct[]
* @returns
*/
async function _deployOneFunction(func: FunctionStruct, session: ClientSession) {

await _processFunctionWithSameNameButNotId(func, session)

const db = DatabaseAgent.sys_accessor.db
const r = await db.collection(Constants.cn.functions).findOne({ _id: new ObjectId(func._id) }, { session })

const data = {
...func
...func,
}

// if exists function
if (r) {
delete data['_id']
const ret = await db.collection(Constants.cn.functions).updateOne({ _id: r._id }, {
delete data['_id']
const r = await db.collection(Constants.cn.functions)
.updateOne({
appid: func.appid,
name: func.name
}, {
$set: data
}, { session })

assert(ret.matchedCount, `deploy: update function ${func.name} occurred error`)
// return if exists & updated
if (r.matchedCount) {
logger.debug(`deploy function: found an exists function ${func.name} & updated it, matchedCount ${r.matchedCount}`)
return
}

// if new function
data._id = new ObjectId(data._id) as any
const ret = await db.collection(Constants.cn.functions)
.insertOne(data as any, { session })

const ret = await db.collection(Constants.cn.functions).insertOne(data as any, { session })
assert(ret.insertedId, `deploy: add function ${func.name} occurred error`)
}

/**
* Remove functions which have same name but different _id.
* @param func the function to be processing
* @param session the mongodb session for transaction operations
*/
async function _processFunctionWithSameNameButNotId(func: FunctionStruct, session: ClientSession) {
const db = DatabaseAgent.sys_accessor.db

// 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
}

logger.warn(`delete local func ${r?.value?._id} with same name with (${func._id}:${func.name}) but different id `)
logger.debug(`deploy function: inserted a function (${func.name}), insertedId ${ret.insertedId}`)
}
45 changes: 13 additions & 32 deletions packages/system-server/src/api/policy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-09-10 11:44:11
* @LastEditTime: 2021-09-10 22:09:19
* @Description:
*/

import * as assert from 'assert'
import { Constants } from '../constants'
import { DatabaseAgent } from "../lib/db-agent"
import { ClientSession, ObjectId } from 'mongodb'
import { ClientSession } from 'mongodb'
import { ApplicationStruct, getApplicationDbAccessor } from './application'
import { logger } from '../lib/logger'

export enum PolicyStatus {
DISABLED = 0,
Expand Down Expand Up @@ -105,47 +106,27 @@ export async function deployPolicies(appid: string, policies: PolicyStruct[]) {
* @returns
*/
async function _deployOnePolicy(policy: PolicyStruct, session: ClientSession) {

await _deletePolicyWithSameNameButNotId(policy, session)

const db = DatabaseAgent.sys_accessor.db
const r = await db.collection(Constants.cn.policies).findOne({ _id: new ObjectId(policy._id) }, { session })

const data = {
...policy
}
delete data['_id']


// if exists
if (r) {
delete data['_id']
const ret = await db.collection(Constants.cn.policies).updateOne({ _id: r._id }, {
const r = await db.collection(Constants.cn.policies)
.updateOne({
appid: policy.appid,
name: policy.name
}, {
$set: data
}, { session })

assert(ret.matchedCount, `deploy: update policy ${policy.name} occurred error`)
if (r.matchedCount) {
logger.debug(`deploy policy: found an exists policy (${policy.name}) & updated it, matchedCount ${r.matchedCount}`)
return
}

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

/**
* Remove policy which have same name but different _id.
* @param policy the policy to be processing
* @param session the mongodb session for transaction operations
* @see _deployOnePolicy()
* @private
*/
async function _deletePolicyWithSameNameButNotId(policy: PolicyStruct, session: ClientSession) {
const db = DatabaseAgent.sys_accessor.db
await db.collection(Constants.cn.policies).findOneAndDelete({
_id: {
$ne: new ObjectId(policy._id)
},
name: policy.name
}, { session })
assert(ret.insertedId, `deploy policy: add policy ${policy.name} occurred error`)
logger.debug(`deploy policy: inserted a policy (${policy.name}), insertedId ${ret.insertedId}`)
}

0 comments on commit 1da03a9

Please sign in to comment.