Skip to content

Latest commit

 

History

History
289 lines (233 loc) · 7.66 KB

定时任务.md

File metadata and controls

289 lines (233 loc) · 7.66 KB

环境准备

  • release-3.3分支源码

描述

增加定时任务,用于定时触发某些机制,比如消息、控制指令等。

分析

官方介绍如下:

ThingsBoard 允许您通过灵活的时间表配置来安排各种类型的事件。ThingsBoard 调度器根据它们的调度触发配置的调度器事件。当调度程序事件被触发时,规则引擎消息从具有与规则引擎消息相似结构的事件配置中生成。然后将生成的消息转发到规则引擎并从根规则链开始处理。

任务处理流程如下:

scheduler

因此可以得到功能性需求:

  • 任务调度器(调度任务,任务交给规则引擎处理)
  • 可视化界面(列表以及日历)

从可用性角度出发,还需考虑以下非功能性需求:

  • 性能
  • 可靠性(尽可能不丢失任务,以及补偿)
  • 可扩展性(支持集群)

官方调度事件结构:

{
    "id": { //事件编号
        "entityType": "SCHEDULER_EVENT",
        "id": "8b47f8d0-6a15-11ec-89c2-2f343e6c262d" 
    },
    "createdTime": 1640940130525,
    "additionalInfo": null,
    "tenantId": {//租户
        "entityType": "TENANT",
        "id": "827a2e50-6333-11ec-a919-556e8dbef35c"
    },
    "customerId": {//客户
        "entityType": "CUSTOMER",
        "id": "13814000-1dd2-11b2-8080-808080808080"
    },
    "name": "test-update-attribute",//事件名称
    "type": "updateAttributes", //事件类型
    "schedule": { //调度时间配置
        "timezone": "Asia/Shanghai",//时区
        "startTime": 1640880000000, //开始时间
        "repeat": {
            "type": "TIMER", //重复类型
            "endsOn": 1641312000000, //结束时间
            "repeatInterval": 5,
            "timeUnit": "SECONDS" //时间单位
        }
    },
    "configuration": { //调度消息配置
        "originatorId": {
            "entityType": "DEVICE",
            "id": "65195820-6a15-11ec-89c2-2f343e6c262d"
        },
        "msgType": "POST_ATTRIBUTES_REQUEST", //消息类型
        "msgBody": { //消息内容
            "test": "123"
        },
        "metadata": { //元数据
            "scope": "SERVER_SCOPE"
        }
    },
    "ownerId": { //事件归属
        "entityType": "TENANT",
        "id": "827a2e50-6333-11ec-a919-556e8dbef35c"
    }
}

调度时间配置支持多时区和重复调度,重复调度类型包含:Daily(天)、Weekly(星期)、Monthly(月)、Yearly(年)以及Time-Based(自定义时间),Weekly支持选择周一到周日,Time-Based支持选择间隔时间粒度:秒、分钟、小时。

调度消息配置支持以下几种消息类型:General Report(报表生成)、Update Attribute(属性更新)、Send RPC Request to Device(发送RPC请求给设备)、Update Firmware(更新固件)、Update Software(更新软件)。

Update Firmware的配置信息为:

"configuration": {
        "originatorId": {
            "entityType": "DEVICE",
            "id": "4e07e0e0-8c0f-11ec-98f9-ff45c37940c6"
        },
        "msgType": "FIRMWARE_UPDATED",
        "msgBody": {
            "entityType": "OTA_PACKAGE",
            "id": "5dc11080-8c0d-11ec-a344-c767c1ab1bb8"
        },
        "metadata": {}
    },

Update Software的配置信息为:

"configuration": {
        "originatorId": {
            "entityType": "DEVICE",
            "id": "4e07e0e0-8c0f-11ec-98f9-ff45c37940c6"
        },
        "msgType": "SOFTWARE_UPDATED",
        "msgBody": {
            "entityType": "OTA_PACKAGE",
            "id": "a9ea1090-8c0f-11ec-83d0-83ba2015b874"
        },
        "metadata": {}
    },

Update Attribute的配置信息为:

"configuration": {
        "originatorId": {
            "entityType": "DEVICE",
            "id": "4e07e0e0-8c0f-11ec-98f9-ff45c37940c6"
        },
        "msgType": "POST_ATTRIBUTES_REQUEST",
        "msgBody": {
            "123": "456"
        },
        "metadata": {
            "scope": "SERVER_SCOPE"
        }
    },

Send Rpc Request To Device的配置信息为:

"configuration": {
        "originatorId": {
            "entityType": "DEVICE",
            "id": "7ae4a550-9ad1-11ec-9260-a327b4f24eda"
        },
        "msgType": "RPC_CALL_FROM_SERVER_TO_DEVICE",
        "msgBody": {
            "method": "setTemperture",
            "params": {
                "temp": 123
            }
        },
        "metadata": {
            "oneway": true
        }
    },

开发

调研现有技术,决定使用以下方案:

  • 定时任务框架: 基于ScheduledThreadPool进行调度
  • 日历控件:Angular-calendar

准备使用以下:

  • 调度
    • 展示方式:
      • 列表
      • 视图(暂不支持)
    • 类型
      • 星期
      • 定时(时、分、秒)
  • 配置:
    • 固件更新
    • 软件更新
    • 属性更新
    • RPC请求(暂不支持)

实现内容

后端开发:

  • 调度框架实现
  • 任务的创建、修改、删除逻辑实现

前端开发:

  • 菜单增加
  • 路由增加
  • 列表组件增加以及数据源关联
    • 调度组件
    • 配置组件
  • 详情页增加
  • 国际化
  • 表单检查
文件列表

前端部分

scheduler-job.service.ts
menu.service.ts
attribute-key-value-table.component.html
attribute-key-value-table.component.scss
attribute-key-value-table.component.ts
scheduler-job.component.html
scheduler-job.component.ts
scheduler-job.module.ts
scheduler-job-attribute-update.component.ts
scheduler-job-attribute-update-component.html
scheduler-job-configuration.component.html
scheduler-job-configuration.component.ts
scheduler-job-ota-update.component.html
scheduler-job-ota-update.component.ts
scheduler-job-routing.module.ts
scheduler-job-schedule.component.html
scheduler-job-schedule.component.ts
scheduler-job-table-config.resolve.ts
target-select.component.html
target-select.component.ts
home-pages.module.ts
ota-package-autocomplete.component.ts
scheduer-job-id.ts
entity-type.models.ts
scheduler-job.models.ts
locale.constant-en_US.json
locale.constant-zh_CN.json

后端部分

queue.proto
SchedulerJobEntity.java
SchedulerJobInfoEntity.java
ModelConstants.java
SchedulerJobDao.java
SchedulerJobInfoDao.java
SchedulerJobServiceImpl.java
JpaSchedulerJobDao.java
JpaSchedulerJobInfoDao.java
SchedulerJobInfoRepository.java
SchedulerJobRepository.java
schema-entities.sql
schema-entities-hsql.sql
SchedulerJobService.java
src/main/java/org/thingsboard/server/common/data
EntityIdFactory.java
SchedulerJobId.java
SchedulerJob.java
SchedulerJobInfo.java
DefaultTenantProfileConfiguration.java
EntityType.java
TbMsgAttributesNode.java
pom.xml(thingsboard)

实现细节较多,具体可参考我的3.3扩展分支:release-3.3-x

验证

任务增加

scheduler_job_add_configuration scheduler_job_add_schedule

任务详情

scheduler_job_detail

任务验证

scheduler_job_update_attributes

TIPS

  • 因Thingsboard支持分布式部署,所以定时任务也需要支持分布式。
  • 开发完成的内容还不支持Rpc请求(Rpc Request),属性更新暂时只支持服务端属性(Server Attributes).