From 6dca01a39df3154886b6d67a52d9b2e7c8af933f Mon Sep 17 00:00:00 2001 From: maslow Date: Fri, 30 Jul 2021 01:53:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E5=99=A8=E6=97=B6=E6=9C=AA=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cloud-function-engine/src/function.ts | 2 ++ .../src/trigger-scheduler.ts | 17 ++++++++++------- packages/cloud-function-engine/src/trigger.ts | 9 ++++++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/cloud-function-engine/src/function.ts b/packages/cloud-function-engine/src/function.ts index 6fc4351b84..7823b4c92f 100644 --- a/packages/cloud-function-engine/src/function.ts +++ b/packages/cloud-function-engine/src/function.ts @@ -1,6 +1,7 @@ import { FunctionEngine } from "." import { CloudFunctionStruct, FunctionContext, FunctionResult, RequireFuncType } from "./types" import * as ts from 'typescript' +import * as assert from "assert" /** * 云函数 @@ -76,6 +77,7 @@ export class CloudFunction { } constructor(data: CloudFunctionStruct) { + assert.ok(data) this._data = data } diff --git a/packages/cloud-function-engine/src/trigger-scheduler.ts b/packages/cloud-function-engine/src/trigger-scheduler.ts index 28801f6136..57d502acc9 100644 --- a/packages/cloud-function-engine/src/trigger-scheduler.ts +++ b/packages/cloud-function-engine/src/trigger-scheduler.ts @@ -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() @@ -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) } @@ -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 } diff --git a/packages/cloud-function-engine/src/trigger.ts b/packages/cloud-function-engine/src/trigger.ts index 09828873c6..1eb782f104 100644 --- a/packages/cloud-function-engine/src/trigger.ts +++ b/packages/cloud-function-engine/src/trigger.ts @@ -35,9 +35,16 @@ export class Trigger { // 上次执行时间 public last_exec_time: number - // 状态 + // 状态: 0 停用,1 启用 public status: number + /** + * 是否启用 + */ + get isEnabled() { + return this.status === 1 + } + /** * 是否为事件触发器 */