-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtrigger.ts
97 lines (87 loc) · 3.07 KB
/
trigger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { CloudApiService } from '../utils'
import { successLog } from '../logger'
import { IFunctionTriggerOptions, IFunctionBatchOptions } from '../types'
import { CloudBaseError } from '../error'
const scfService = CloudApiService.getInstance('scf')
// 创建函数触发器
export async function createFunctionTriggers(options: IFunctionTriggerOptions): Promise<void> {
const { functionName, triggers = [], envId } = options
const parsedTriggers = triggers.map(item => {
if (item.type !== 'timer') {
throw new CloudBaseError(
`不支持的触发器类型 [${item.type}],目前仅支持定时触发器(timer)!`
)
}
return {
TriggerName: item.name,
Type: item.type,
TriggerDesc: item.config
}
})
try {
await scfService.request('BatchCreateTrigger', {
FunctionName: functionName,
Namespace: envId,
Triggers: JSON.stringify(parsedTriggers),
Count: parsedTriggers.length
})
} catch (e) {
throw new CloudBaseError(`[${functionName}] 创建触发器失败:${e.message}`, {
action: e.action,
code: e.code
})
}
}
// 批量部署函数触发器
export async function batchCreateTriggers(options: IFunctionBatchOptions): Promise<void> {
const { functions, envId } = options
const promises = functions.map(func =>
(async () => {
try {
await createFunctionTriggers({
functionName: func.name,
triggers: func.triggers,
envId
})
successLog(`[${func.name}] 创建云函数触发器成功!`)
} catch (e) {
throw new CloudBaseError(e.message)
}
})()
)
await Promise.all(promises)
}
// 删除函数触发器
export async function deleteFunctionTrigger(options: IFunctionTriggerOptions): Promise<void> {
const { functionName, triggerName, envId } = options
try {
await scfService.request('DeleteTrigger', {
FunctionName: functionName,
Namespace: envId,
TriggerName: triggerName,
Type: 'timer'
})
successLog(`[${functionName}] 删除云函数触发器 ${triggerName} 成功!`)
} catch (e) {
throw new CloudBaseError(`[${functionName}] 删除触发器失败:${e.message}`)
}
}
export async function batchDeleteTriggers(options: IFunctionBatchOptions): Promise<void> {
const { functions, envId } = options
const promises = functions.map(func =>
(async () => {
try {
func.triggers.forEach(async trigger => {
await deleteFunctionTrigger({
functionName: func.name,
triggerName: trigger.name,
envId
})
})
} catch (e) {
throw new CloudBaseError(e.message)
}
})()
)
await Promise.all(promises)
}