Skip to content

Commit

Permalink
fix: 修复获取触发器时未控制状态的问题;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 29, 2021
1 parent 23d359e commit 6dca01a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/cloud-function-engine/src/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FunctionEngine } from "."
import { CloudFunctionStruct, FunctionContext, FunctionResult, RequireFuncType } from "./types"
import * as ts from 'typescript'
import * as assert from "assert"

/**
* 云函数
Expand Down Expand Up @@ -76,6 +77,7 @@ export class CloudFunction {
}

constructor(data: CloudFunctionStruct) {
assert.ok(data)
this._data = data
}

Expand Down
17 changes: 10 additions & 7 deletions packages/cloud-function-engine/src/trigger-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TriggerScheduler {
public init(triggers: Trigger[]) {
this.log('init trigger scheduler')

this._triggers = triggers
this._triggers = triggers.filter(tri => tri.isEnabled)

this.scheduleTimer()

Expand All @@ -29,16 +29,14 @@ export class TriggerScheduler {
}

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

Expand All @@ -47,6 +45,11 @@ export class TriggerScheduler {
if (index < 0) {
this._triggers.push(trigger)
} else {
// 若为定时器,则保留其 `最近一次执行时间`
if(trigger.isTimer) {
trigger.last_exec_time = this._triggers[index].last_exec_time ?? 0
}

this._triggers[index] = trigger
}

Expand Down
9 changes: 8 additions & 1 deletion packages/cloud-function-engine/src/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ export class Trigger {
// 上次执行时间
public last_exec_time: number

// 状态
// 状态: 0 停用,1 启用
public status: number

/**
* 是否启用
*/
get isEnabled() {
return this.status === 1
}

/**
* 是否为事件触发器
*/
Expand Down

0 comments on commit 6dca01a

Please sign in to comment.