Skip to content

Commit b1c350a

Browse files
committed
fix: 修复发布、部署资源时事务使用错误;
1 parent 5c4fb54 commit b1c350a

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Constants } from "../constants"
33
import { Globals } from "../lib/globals"
44
import { compileTs2js } from 'cloud-function-engine/dist/utils'
55
import { CloudFunctionStruct } from "cloud-function-engine"
6-
import { ObjectId } from 'mongodb'
6+
import { ClientSession, ObjectId } from 'mongodb'
77
import * as assert from 'assert'
88
const db = Globals.sys_db
99

@@ -63,8 +63,8 @@ export async function publishFunctions() {
6363
await session.withTransaction(async () => {
6464
const _db = app_accessor.db
6565
const app_coll = _db.collection(Constants.function_collection)
66-
await app_coll.deleteMany({})
67-
await app_coll.insertMany(data)
66+
await app_coll.deleteMany({}, { session })
67+
await app_coll.insertMany(data, { session })
6868
})
6969
} catch (error) {
7070
logger.error(error)
@@ -99,7 +99,7 @@ export async function deployFunctions(functions: CloudFunctionStruct[]) {
9999
try {
100100
await session.withTransaction(async () => {
101101
for (const func of data) {
102-
await _deployOneFunction(func)
102+
await _deployOneFunction(func, session)
103103
}
104104
})
105105
} catch (error) {
@@ -110,12 +110,12 @@ export async function deployFunctions(functions: CloudFunctionStruct[]) {
110110
}
111111
}
112112

113-
async function _deployOneFunction(func: CloudFunctionStruct) {
113+
async function _deployOneFunction(func: CloudFunctionStruct, session: ClientSession) {
114114

115-
await _deleteFunctionWithSameNameButNotId(func)
115+
await _deleteFunctionWithSameNameButNotId(func, session)
116116

117117
const db = Globals.sys_accessor.db
118-
const r = await db.collection('__functions').findOne({ _id: new ObjectId(func._id) })
118+
const r = await db.collection('__functions').findOne({ _id: new ObjectId(func._id) }, { session })
119119

120120
const data = {
121121
...func
@@ -126,7 +126,7 @@ async function _deployOneFunction(func: CloudFunctionStruct) {
126126
delete data['_id']
127127
const ret = await db.collection('__functions').updateOne({ _id: r._id }, {
128128
$set: data
129-
})
129+
}, { session })
130130

131131
assert(ret.matchedCount, `deploy: update function ${func.name} occurred error`)
132132
return
@@ -135,20 +135,20 @@ async function _deployOneFunction(func: CloudFunctionStruct) {
135135
// if new function
136136
data._id = new ObjectId(data._id) as any
137137

138-
const ret = await db.collection('__functions').insertOne(data as any)
138+
const ret = await db.collection('__functions').insertOne(data as any, { session })
139139
assert(ret.insertedId, `deploy: add function ${func.name} occurred error`)
140140
}
141141

142142
/**
143143
* 删除本地 _id 不同,但 name 相同的云函数(若存在)
144144
* @param func
145145
*/
146-
async function _deleteFunctionWithSameNameButNotId(func: CloudFunctionStruct) {
146+
async function _deleteFunctionWithSameNameButNotId(func: CloudFunctionStruct, session: ClientSession) {
147147
const db = Globals.sys_accessor.db
148148
await db.collection('__functions').findOneAndDelete({
149149
_id: {
150150
$ne: new ObjectId(func._id)
151151
},
152152
name: func.name
153-
})
153+
}, { session })
154154
}

packages/devops-server/src/api/rules.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as assert from 'assert'
22
import { Constants } from '../constants'
33
import { Globals } from "../lib/globals"
4-
import { ObjectId } from 'mongodb'
4+
import { ClientSession, ObjectId } from 'mongodb'
55

66
const db = Globals.sys_db
77
export interface RuleDocument {
@@ -48,8 +48,8 @@ export async function publishAccessPolicy() {
4848
await session.withTransaction(async () => {
4949
const _db = app_accessor.db
5050
const app_coll = _db.collection(Constants.policy_collection)
51-
await app_coll.deleteMany({})
52-
await app_coll.insertMany(ret)
51+
await app_coll.deleteMany({}, { session })
52+
await app_coll.insertMany(ret, { session })
5353
})
5454
} catch (error) {
5555
logger.error(error)
@@ -76,7 +76,7 @@ export async function deployPolicies(policies) {
7676
try {
7777
await session.withTransaction(async () => {
7878
for (const item of data) {
79-
await _deployOnePolicy(item)
79+
await _deployOnePolicy(item, session)
8080
}
8181
})
8282
} catch (error) {
@@ -87,12 +87,12 @@ export async function deployPolicies(policies) {
8787
}
8888
}
8989

90-
async function _deployOnePolicy(policy: any) {
90+
async function _deployOnePolicy(policy: any, session: ClientSession) {
9191

92-
await _deletePolicyWithSameNameButNotId(policy)
92+
await _deletePolicyWithSameNameButNotId(policy, session)
9393

9494
const db = Globals.sys_accessor.db
95-
const r = await db.collection('__policies').findOne({ _id: new ObjectId(policy._id) })
95+
const r = await db.collection('__policies').findOne({ _id: new ObjectId(policy._id) }, { session })
9696

9797
const data = {
9898
...policy
@@ -104,28 +104,28 @@ async function _deployOnePolicy(policy: any) {
104104
delete data['_id']
105105
const ret = await db.collection('__policies').updateOne({ _id: r._id }, {
106106
$set: data
107-
})
107+
}, { session })
108108

109109
assert(ret.matchedCount, `deploy: update policy ${policy.name} occurred error`)
110110
return
111111
}
112112

113113
// if new
114114
data._id = new ObjectId(data._id) as any
115-
const ret = await db.collection('__policies').insertOne(data as any)
115+
const ret = await db.collection('__policies').insertOne(data as any, { session })
116116
assert(ret.insertedId, `deploy: add policy ${policy.name} occurred error`)
117117
}
118118

119119
/**
120120
* 删除本地 _id 不同,但 name 相同的策略(若存在)
121121
* @param func
122122
*/
123-
async function _deletePolicyWithSameNameButNotId(policy: any) {
123+
async function _deletePolicyWithSameNameButNotId(policy: any, session: ClientSession) {
124124
const db = Globals.sys_accessor.db
125125
await db.collection('__policies').findOneAndDelete({
126126
_id: {
127127
$ne: new ObjectId(policy._id)
128128
},
129129
name: policy.name
130-
})
130+
}, { session })
131131
}

packages/devops-server/src/api/trigger.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import { Constants } from "../constants"
33
import { Globals } from "../lib/globals"
4-
import { ObjectId } from 'mongodb'
4+
import { ClientSession, ObjectId } from 'mongodb'
55
import * as assert from 'assert'
66

77
const db = Globals.sys_db
@@ -49,8 +49,8 @@ export async function publishTriggers() {
4949
await session.withTransaction(async () => {
5050
const _db = app_accessor.db
5151
const app_coll = _db.collection(Constants.trigger_collection)
52-
await app_coll.deleteMany({})
53-
await app_coll.insertMany(ret)
52+
await app_coll.deleteMany({}, { session })
53+
await app_coll.insertMany(ret, { session })
5454
})
5555
} catch (error) {
5656
logger.error(error)
@@ -77,7 +77,7 @@ export async function deployTriggers(triggers: any[]) {
7777
try {
7878
await session.withTransaction(async () => {
7979
for (const func of data) {
80-
await _deployOneTrigger(func)
80+
await _deployOneTrigger(func, session)
8181
}
8282
})
8383
} catch (error) {
@@ -88,10 +88,10 @@ export async function deployTriggers(triggers: any[]) {
8888
}
8989
}
9090

91-
async function _deployOneTrigger(trigger: any) {
91+
async function _deployOneTrigger(trigger: any, session: ClientSession) {
9292

9393
const db = Globals.sys_accessor.db
94-
const r = await db.collection('__triggers').findOne({ _id: new ObjectId(trigger._id) })
94+
const r = await db.collection('__triggers').findOne({ _id: new ObjectId(trigger._id) }, { session })
9595

9696
const data = {
9797
...trigger
@@ -103,14 +103,14 @@ async function _deployOneTrigger(trigger: any) {
103103
delete data['_id']
104104
const ret = await db.collection('__triggers').updateOne({ _id: r._id }, {
105105
$set: data
106-
})
106+
}, { session })
107107

108108
assert(ret.matchedCount, `deploy: update trigger ${trigger.name} occurred error`)
109109
return
110110
}
111111

112112
// if new function
113113
data._id = new ObjectId(data._id) as any
114-
const ret = await db.collection('__triggers').insertOne(data as any)
114+
const ret = await db.collection('__triggers').insertOne(data as any, { session })
115115
assert(ret.insertedId, `deploy: add trigger ${trigger.name} occurred error`)
116116
}

0 commit comments

Comments
 (0)