Skip to content

Commit 8e3dbcf

Browse files
committed
feat: 新增实现指定一个触发器的调度更新(接口和功能);
1 parent f3b5ada commit 8e3dbcf

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

http/admin.http

-5
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,3 @@ Authorization: Bearer {{token}}
4343
"name": "Maslow",
4444
"avatar": "https://work.zhuo-zhuo.com/file/data/23ttprpxmavkkuf6nttc/PHID-FILE-vzv6dyqo3ev2tmngx7mu/logoL)"
4545
}
46-
47-
### 应用触发器配置
48-
49-
POST {{base_url}}/admin/apply/triggers HTTP/1.1
50-
Authorization: Bearer {{token}}

http/trigger.http

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
@token={{login.response.body.$.data.access_token}}
3+
4+
### 管理员登陆
5+
# @name login
6+
7+
POST {{base_url}}/admin/login HTTP/1.1
8+
Content-Type: application/json
9+
10+
{
11+
"username": "less",
12+
"password": "kissme"
13+
}
14+
15+
### 应用所有触发器配置
16+
17+
POST {{base_url}}/admin/apply/triggers HTTP/1.1
18+
Authorization: Bearer {{token}}
19+
20+
21+
### 应用指定触发器配置
22+
POST {{base_url}}/admin/apply/triggers?tid=60bf1fe10bd2797aacc70e2e HTTP/1.1
23+
Authorization: Bearer {{token}}

src/lib/faas/trigger.ts

+43
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class Trigger {
4444
// 上次执行时间
4545
public last_exec_time: number
4646

47+
// 状态
48+
public status: number
49+
4750
get isEvent() {
4851
return this.type === TriggerType.TRIGGER_EVENT
4952
}
@@ -65,6 +68,7 @@ export class Trigger {
6568
tri.func_id = data.func_id
6669
tri.name = data.name
6770
tri.last_exec_time = data.last_exec_time ?? 0
71+
tri.status = data.status
6872

6973
if (tri.isEvent) {
7074
tri.event = data.event
@@ -102,6 +106,30 @@ export class TriggerScheduler {
102106
this.cancelTimer()
103107
}
104108

109+
/**
110+
* 更新指定 trigger:
111+
* 1. 从库中获取最新 trigger 数据
112+
* 2. 若 status 为停用,则从当前调度表中删除
113+
* 3. 若 status 为启用,则将其最新数据更新或添加到当前调度列表中
114+
* @param triggerId
115+
*/
116+
public async updateTrigger(trigger: Trigger): Promise<boolean> {
117+
// 停用状态,移除调度
118+
if (trigger.status === 0) {
119+
return this.removeTrigger(trigger.id)
120+
}
121+
122+
// 启用状态,更新或添加
123+
const index = this._triggers.findIndex(tri => tri.id === trigger.id)
124+
if (index < 0) {
125+
this._triggers.push(trigger)
126+
} else {
127+
this._triggers[index] = trigger
128+
}
129+
130+
return true
131+
}
132+
105133
/**
106134
* 触发事件
107135
* @param event 事件名
@@ -202,6 +230,21 @@ export class TriggerScheduler {
202230
return this._triggers.filter(tri => tri.isTimer)
203231
}
204232

233+
/**
234+
* 从当前调度列表中移除触发器
235+
* @param triggerId
236+
* @returns
237+
*/
238+
private removeTrigger(triggerId: string): boolean {
239+
const index = this._triggers.findIndex(t => t.id === triggerId)
240+
if (index === -1) {
241+
return false
242+
}
243+
244+
this._triggers.splice(index, 1)
245+
return true
246+
}
247+
205248
/**
206249
* 定时器触发器调度
207250
*/

src/router/admin/index.ts

+28-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { entry as adminEntry } from '../../entry/admin'
77
import { entry as appEntry } from '../../entry/app'
88
import { getAccessRules } from '../../lib/rules'
99
import { Ruler } from 'less-api'
10-
import { scheduler } from '../../lib/faas'
10+
import { scheduler, Trigger } from '../../lib/faas'
1111

1212
export const AdminRouter = Router()
1313
const logger = getLogger('admin:api')
@@ -325,7 +325,8 @@ AdminRouter.post('/apply/rules', async (req, res) => {
325325

326326

327327
/**
328-
* 重新调度最新的触发器配置
328+
* 应用触发器配置
329+
* @query tid? 触发器ID,若缺省则重新应用所有触发器配置
329330
*/
330331
AdminRouter.post('/apply/triggers', async (req, res) => {
331332
const requestId = req['requestId']
@@ -337,16 +338,34 @@ AdminRouter.post('/apply/triggers', async (req, res) => {
337338
return res.status(code).send()
338339
}
339340

340-
// apply triggers
341+
const triggerId = req.query.tid ?? null
342+
341343
try {
342344
logger.debug(`[${requestId}] apply trigger rule`)
343-
await scheduler.init()
345+
346+
// 若未提供 triggerId, 则更新所有触发器调度
347+
if (!triggerId) {
348+
await scheduler.init()
349+
return res.send({ code: 0, data: 'ok:applied' })
350+
}
351+
352+
// get trigger by id
353+
const r = await db.collection('triggers').where({ _id: triggerId }).getOne()
354+
if (!r.ok) return res.status(404).send('trigger not found')
355+
356+
// 更新指定触发器
357+
const trigger = Trigger.fromJson(r.data)
358+
const result = await scheduler.updateTrigger(trigger)
359+
360+
return res.send({
361+
code: 0,
362+
data: result ? 'ok:applied' : 'ok:unchanged'
363+
})
344364
} catch (error) {
345365
logger.error(`[${requestId}] apply trigger rule error: `, error)
366+
return res.send({
367+
code: 1,
368+
data: error
369+
})
346370
}
347-
348-
return res.send({
349-
code: 0,
350-
data: 'applied'
351-
})
352371
})

0 commit comments

Comments
 (0)