Skip to content

Commit 1da03a9

Browse files
committed
fix(sys-server): fix remote deploy got duplicated error, dropped func/policy _id;
1 parent b188f8d commit 1da03a9

File tree

2 files changed

+28
-73
lines changed

2 files changed

+28
-73
lines changed

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

+15-41
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-09-10 11:43:37
4+
* @LastEditTime: 2021-09-10 22:19:43
55
* @Description:
66
*/
77

88
import { Constants } from "../constants"
99
import { DatabaseAgent } from "../lib/db-agent"
1010
import { compileTs2js } from 'cloud-function-engine/dist/utils'
1111
import { CloudFunctionStruct } from "cloud-function-engine"
12-
import { ClientSession, ObjectId } from 'mongodb'
12+
import { ClientSession } from 'mongodb'
1313
import * as assert from 'assert'
1414
import { logger } from "../lib/logger"
1515
import { ApplicationStruct, getApplicationDbAccessor } from "./application"
@@ -130,55 +130,29 @@ export async function deployFunctions(appid: string, functions: FunctionStruct[]
130130
* @returns
131131
*/
132132
async function _deployOneFunction(func: FunctionStruct, session: ClientSession) {
133-
134-
await _processFunctionWithSameNameButNotId(func, session)
135-
136133
const db = DatabaseAgent.sys_accessor.db
137-
const r = await db.collection(Constants.cn.functions).findOne({ _id: new ObjectId(func._id) }, { session })
138-
139134
const data = {
140-
...func
135+
...func,
141136
}
142-
143-
// if exists function
144-
if (r) {
145-
delete data['_id']
146-
const ret = await db.collection(Constants.cn.functions).updateOne({ _id: r._id }, {
137+
delete data['_id']
138+
const r = await db.collection(Constants.cn.functions)
139+
.updateOne({
140+
appid: func.appid,
141+
name: func.name
142+
}, {
147143
$set: data
148144
}, { session })
149145

150-
assert(ret.matchedCount, `deploy: update function ${func.name} occurred error`)
146+
// return if exists & updated
147+
if (r.matchedCount) {
148+
logger.debug(`deploy function: found an exists function ${func.name} & updated it, matchedCount ${r.matchedCount}`)
151149
return
152150
}
153151

154152
// if new function
155-
data._id = new ObjectId(data._id) as any
153+
const ret = await db.collection(Constants.cn.functions)
154+
.insertOne(data as any, { session })
156155

157-
const ret = await db.collection(Constants.cn.functions).insertOne(data as any, { session })
158156
assert(ret.insertedId, `deploy: add function ${func.name} occurred error`)
159-
}
160-
161-
/**
162-
* Remove functions which have same name but different _id.
163-
* @param func the function to be processing
164-
* @param session the mongodb session for transaction operations
165-
*/
166-
async function _processFunctionWithSameNameButNotId(func: FunctionStruct, session: ClientSession) {
167-
const db = DatabaseAgent.sys_accessor.db
168-
169-
// remove functions
170-
const r = await db.collection(Constants.cn.functions).findOneAndDelete(
171-
{
172-
_id: {
173-
$ne: new ObjectId(func._id)
174-
},
175-
name: func.name
176-
},
177-
{ session })
178-
179-
if (!r.value) {
180-
return
181-
}
182-
183-
logger.warn(`delete local func ${r?.value?._id} with same name with (${func._id}:${func.name}) but different id `)
157+
logger.debug(`deploy function: inserted a function (${func.name}), insertedId ${ret.insertedId}`)
184158
}

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

+13-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-09-10 11:44:11
4+
* @LastEditTime: 2021-09-10 22:09:19
55
* @Description:
66
*/
77

88
import * as assert from 'assert'
99
import { Constants } from '../constants'
1010
import { DatabaseAgent } from "../lib/db-agent"
11-
import { ClientSession, ObjectId } from 'mongodb'
11+
import { ClientSession } from 'mongodb'
1212
import { ApplicationStruct, getApplicationDbAccessor } from './application'
13+
import { logger } from '../lib/logger'
1314

1415
export enum PolicyStatus {
1516
DISABLED = 0,
@@ -105,47 +106,27 @@ export async function deployPolicies(appid: string, policies: PolicyStruct[]) {
105106
* @returns
106107
*/
107108
async function _deployOnePolicy(policy: PolicyStruct, session: ClientSession) {
108-
109-
await _deletePolicyWithSameNameButNotId(policy, session)
110-
111109
const db = DatabaseAgent.sys_accessor.db
112-
const r = await db.collection(Constants.cn.policies).findOne({ _id: new ObjectId(policy._id) }, { session })
113-
114110
const data = {
115111
...policy
116112
}
113+
delete data['_id']
117114

118-
119-
// if exists
120-
if (r) {
121-
delete data['_id']
122-
const ret = await db.collection(Constants.cn.policies).updateOne({ _id: r._id }, {
115+
const r = await db.collection(Constants.cn.policies)
116+
.updateOne({
117+
appid: policy.appid,
118+
name: policy.name
119+
}, {
123120
$set: data
124121
}, { session })
125122

126-
assert(ret.matchedCount, `deploy: update policy ${policy.name} occurred error`)
123+
if (r.matchedCount) {
124+
logger.debug(`deploy policy: found an exists policy (${policy.name}) & updated it, matchedCount ${r.matchedCount}`)
127125
return
128126
}
129127

130128
// if new
131-
data._id = new ObjectId(data._id) as any
132129
const ret = await db.collection(Constants.cn.policies).insertOne(data as any, { session })
133-
assert(ret.insertedId, `deploy: add policy ${policy.name} occurred error`)
134-
}
135-
136-
/**
137-
* Remove policy which have same name but different _id.
138-
* @param policy the policy to be processing
139-
* @param session the mongodb session for transaction operations
140-
* @see _deployOnePolicy()
141-
* @private
142-
*/
143-
async function _deletePolicyWithSameNameButNotId(policy: PolicyStruct, session: ClientSession) {
144-
const db = DatabaseAgent.sys_accessor.db
145-
await db.collection(Constants.cn.policies).findOneAndDelete({
146-
_id: {
147-
$ne: new ObjectId(policy._id)
148-
},
149-
name: policy.name
150-
}, { session })
130+
assert(ret.insertedId, `deploy policy: add policy ${policy.name} occurred error`)
131+
logger.debug(`deploy policy: inserted a policy (${policy.name}), insertedId ${ret.insertedId}`)
151132
}

0 commit comments

Comments
 (0)