Skip to content

Commit

Permalink
feat: 新增实现指定一个触发器的调度更新(接口和功能);
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jun 8, 2021
1 parent f3b5ada commit 8e3dbcf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 14 deletions.
5 changes: 0 additions & 5 deletions http/admin.http
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,3 @@ Authorization: Bearer {{token}}
"name": "Maslow",
"avatar": "https://work.zhuo-zhuo.com/file/data/23ttprpxmavkkuf6nttc/PHID-FILE-vzv6dyqo3ev2tmngx7mu/logoL)"
}

### 应用触发器配置

POST {{base_url}}/admin/apply/triggers HTTP/1.1
Authorization: Bearer {{token}}
23 changes: 23 additions & 0 deletions http/trigger.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

@token={{login.response.body.$.data.access_token}}

### 管理员登陆
# @name login

POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "less",
"password": "kissme"
}

### 应用所有触发器配置

POST {{base_url}}/admin/apply/triggers HTTP/1.1
Authorization: Bearer {{token}}


### 应用指定触发器配置
POST {{base_url}}/admin/apply/triggers?tid=60bf1fe10bd2797aacc70e2e HTTP/1.1
Authorization: Bearer {{token}}
43 changes: 43 additions & 0 deletions src/lib/faas/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class Trigger {
// 上次执行时间
public last_exec_time: number

// 状态
public status: number

get isEvent() {
return this.type === TriggerType.TRIGGER_EVENT
}
Expand All @@ -65,6 +68,7 @@ export class Trigger {
tri.func_id = data.func_id
tri.name = data.name
tri.last_exec_time = data.last_exec_time ?? 0
tri.status = data.status

if (tri.isEvent) {
tri.event = data.event
Expand Down Expand Up @@ -102,6 +106,30 @@ export class TriggerScheduler {
this.cancelTimer()
}

/**
* 更新指定 trigger:
* 1. 从库中获取最新 trigger 数据
* 2. 若 status 为停用,则从当前调度表中删除
* 3. 若 status 为启用,则将其最新数据更新或添加到当前调度列表中
* @param triggerId
*/
public async updateTrigger(trigger: Trigger): Promise<boolean> {
// 停用状态,移除调度
if (trigger.status === 0) {
return this.removeTrigger(trigger.id)
}

// 启用状态,更新或添加
const index = this._triggers.findIndex(tri => tri.id === trigger.id)
if (index < 0) {
this._triggers.push(trigger)
} else {
this._triggers[index] = trigger
}

return true
}

/**
* 触发事件
* @param event 事件名
Expand Down Expand Up @@ -202,6 +230,21 @@ export class TriggerScheduler {
return this._triggers.filter(tri => tri.isTimer)
}

/**
* 从当前调度列表中移除触发器
* @param triggerId
* @returns
*/
private removeTrigger(triggerId: string): boolean {
const index = this._triggers.findIndex(t => t.id === triggerId)
if (index === -1) {
return false
}

this._triggers.splice(index, 1)
return true
}

/**
* 定时器触发器调度
*/
Expand Down
37 changes: 28 additions & 9 deletions src/router/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { entry as adminEntry } from '../../entry/admin'
import { entry as appEntry } from '../../entry/app'
import { getAccessRules } from '../../lib/rules'
import { Ruler } from 'less-api'
import { scheduler } from '../../lib/faas'
import { scheduler, Trigger } from '../../lib/faas'

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


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

// apply triggers
const triggerId = req.query.tid ?? null

try {
logger.debug(`[${requestId}] apply trigger rule`)
await scheduler.init()

// 若未提供 triggerId, 则更新所有触发器调度
if (!triggerId) {
await scheduler.init()
return res.send({ code: 0, data: 'ok:applied' })
}

// get trigger by id
const r = await db.collection('triggers').where({ _id: triggerId }).getOne()
if (!r.ok) return res.status(404).send('trigger not found')

// 更新指定触发器
const trigger = Trigger.fromJson(r.data)
const result = await scheduler.updateTrigger(trigger)

return res.send({
code: 0,
data: result ? 'ok:applied' : 'ok:unchanged'
})
} catch (error) {
logger.error(`[${requestId}] apply trigger rule error: `, error)
return res.send({
code: 1,
data: error
})
}

return res.send({
code: 0,
data: 'applied'
})
})

0 comments on commit 8e3dbcf

Please sign in to comment.