4
4
APPLICATION_SECRET_KEY ,
5
5
CN_FUNCTION_LOGS ,
6
6
CN_PUBLISHED_FUNCTIONS ,
7
+ TASK_LOCK_INIT_TIME ,
7
8
} from '../constants'
8
9
import { CreateFunctionDto } from './dto/create-function.dto'
9
10
import { UpdateFunctionDto } from './dto/update-function.dto'
@@ -18,6 +19,8 @@ import { CloudFunction } from './entities/cloud-function'
18
19
import { ApplicationConfiguration } from 'src/application/entities/application-configuration'
19
20
import { FunctionLog } from 'src/log/entities/function-log'
20
21
import { CloudFunctionHistory } from './entities/cloud-function-history'
22
+ import { TriggerService } from 'src/trigger/trigger.service'
23
+ import { TriggerPhase } from 'src/trigger/entities/cron-trigger'
21
24
import { UpdateFunctionDebugDto } from './dto/update-function-debug.dto'
22
25
23
26
@Injectable ( )
@@ -28,6 +31,7 @@ export class FunctionService {
28
31
constructor (
29
32
private readonly databaseService : DatabaseService ,
30
33
private readonly jwtService : JwtService ,
34
+ private readonly triggerService : TriggerService ,
31
35
) { }
32
36
async create ( appid : string , userid : ObjectId , dto : CreateFunctionDto ) {
33
37
await this . db . collection < CloudFunction > ( 'CloudFunction' ) . insertOne ( {
@@ -79,6 +83,73 @@ export class FunctionService {
79
83
}
80
84
81
85
async updateOne ( func : CloudFunction , dto : UpdateFunctionDto ) {
86
+ // update function name
87
+ if ( dto . newName ) {
88
+ const client = SystemDatabase . client
89
+ const session = client . startSession ( )
90
+
91
+ const found = await this . findOne ( func . appid , dto . newName )
92
+ if ( found ) {
93
+ return new Error ( `Function name ${ found . name } already exists` )
94
+ }
95
+
96
+ try {
97
+ session . startTransaction ( )
98
+
99
+ const fn = await this . db
100
+ . collection < CloudFunction > ( 'CloudFunction' )
101
+ . findOneAndUpdate (
102
+ { appid : func . appid , name : func . name } ,
103
+ {
104
+ $set : {
105
+ name : dto . newName ,
106
+ desc : dto . description ,
107
+ methods : dto . methods ,
108
+ tags : dto . tags || [ ] ,
109
+ updatedAt : new Date ( ) ,
110
+ } ,
111
+ } ,
112
+ { session, returnDocument : 'after' } ,
113
+ )
114
+
115
+ // publish
116
+ await this . publish ( fn . value , func . name )
117
+
118
+ // trigger
119
+ const triggers = await this . triggerService . findAllByTarget (
120
+ func . appid ,
121
+ func . name ,
122
+ )
123
+ if ( triggers . length !== 0 ) {
124
+ const triggersToInsert = triggers . map ( ( doc ) => ( {
125
+ appid : doc . appid ,
126
+ desc : doc . desc ,
127
+ cron : doc . cron ,
128
+ target : dto . newName , // set to new function name
129
+ state : doc . state ,
130
+ phase : TriggerPhase . Creating ,
131
+ lockedAt : TASK_LOCK_INIT_TIME ,
132
+ createdAt : new Date ( doc . createdAt ) ,
133
+ updatedAt : new Date ( ) ,
134
+ } ) )
135
+ await this . triggerService . removeAllByTarget (
136
+ func . appid ,
137
+ func . name ,
138
+ session ,
139
+ )
140
+ await this . triggerService . createMany ( triggersToInsert , session )
141
+ }
142
+ await session . commitTransaction ( )
143
+ return fn . value
144
+ } catch ( error ) {
145
+ await session . abortTransaction ( )
146
+ this . logger . error ( error )
147
+ throw error
148
+ } finally {
149
+ await session . endSession ( )
150
+ }
151
+ }
152
+
82
153
await this . db . collection < CloudFunction > ( 'CloudFunction' ) . updateOne (
83
154
{ appid : func . appid , name : func . name } ,
84
155
{
@@ -138,13 +209,16 @@ export class FunctionService {
138
209
return res
139
210
}
140
211
141
- async publish ( func : CloudFunction ) {
212
+ async publish ( func : CloudFunction , oldFuncName ?: string ) {
142
213
const { db, client } = await this . databaseService . findAndConnect ( func . appid )
143
214
const session = client . startSession ( )
144
215
try {
145
216
await session . withTransaction ( async ( ) => {
146
217
const coll = db . collection ( CN_PUBLISHED_FUNCTIONS )
147
- await coll . deleteOne ( { name : func . name } , { session } )
218
+ await coll . deleteOne (
219
+ { name : oldFuncName ? oldFuncName : func . name } ,
220
+ { session } ,
221
+ )
148
222
await coll . insertOne ( func , { session } )
149
223
} )
150
224
} finally {
0 commit comments