lifecycle
本文档介绍如何在应用中订阅生命周期事件。应用生命周期事件是在应用进行安装、卸载、升级等事件发生时,由系统自动进行触发,开发者可以提供自定义的事件处理函数,以实现自己的业务逻辑。
配置
manifest.yml 文件配置示例:
yaml
event:
triggers:
- key: life_trigger
type: lifecycle
events:
- pce:nexus:app:install
handler:
function: install-handler处理函数
当事件触发时,会被你定义的处理函数接收到。
示例
javascript
import type { LifecycleEventHandler } from "@pc-nexus/event";
export const handler: LifecycleEventHandler = async (context, event) => {
console.log(JSON.stringify(context));
console.log(JSON.stringify(event));
};参数
处理函数接收 context 和 event 两个参数。
context
context 为 NexusAppContext 类型,包含应用运行时的上下文信息,详情请参考 app ,获取触发的事件信息可以从 context 中获取 如下:
| 属性 | 描述 |
|---|---|
event.trigger.key | 触发器在 manifest 中定义的 key |
event.trigger.type | 触发器类型,值为 lifecycle |
event
event 是订阅事件所传递的数据,其类型定义如下:
javascript
export interface EventPayload {}
export interface LifecycleEventPayload extends EventPayload {
installation_id: string;
app: {
id: string;
version: string;
name: string;
publisher: {
name: string;
site: string;
phone: string;
email: string;
};
links: {
title?: string;
url?: string;
}[];
};
environment: {
id: string;
type: string;
name: string;
};
operated_by: string;
}
export interface HandlerFunctionEvent<T = unknown> {
event_type?: string;
payload: T;
}| 名称 | 描述 |
|---|---|
event_type | 订阅事件的类型,例如: pce:nexus:app:install |
payload | 订阅事件所传递的数据 |
payload 在应用生命周期事件中,所有事件通用属性如下:
| 名称 | 描述 |
|---|---|
installation_id | 当前应用安装时的唯一标识 |
app | 当前应用数据 |
environment | 当前应用所在环境数据 |
operated_by | 当前事件操作的用户 ID |
app 定义当前应用数据:
| 名称 | 描述 |
|---|---|
id | 应用 ID |
version | 应用版本号 |
name | 应用名 |
publisher | 应用开发商信息 |
publisher.name | 应用开发商的名称 |
publisher.site | 应用开发商的网站 |
publisher.phone | 应用开发商的电话 |
publisher.email | 应用开发商的邮箱 |
links | 应用的链接信息 |
links.title | 应用链接的标题 |
links.url | 应用链接的 URL 地址 |
environment 当前应用所在环境数据:
| 名称 | 描述 |
|---|---|
id | 应用部署环境 ID |
type | 当前应用所在的环境类型, development 还是 production |
name | 应用部署环境名称 |
事件
目前 Nexus 平台的应用支持以下几个事件:
| 事件名 | 描述 |
|---|---|
pce:nexus:app:install | 应用安装后触发 |
pce:nexus:app:uninstall | 应用卸载前触发 |
pce:nexus:app:upgrade | 应用升级时触发 |
install
当应用程序在站点中完成安装后,系统会发送名为 pce:nexus:app:install 的事件。
数据示例:
javascript
{
"event_type": "pce:nexus:app:install",
"payload": {
"installation_id": "d2b9c438-c0ee-4f4d-86dc-d6b263d83eff",
"app": {
"id": "33b0bb1b-429b-40eb-b4d5-5418584faa04",
"version": "1.0.0",
"name": "nexus-lifecycle-event",
"publisher": {
"name": "PingCode";
"site": "https://pingcode.com";
"phone": "12345678";
"email": "xxx@email.com";
},
"links": [
{
"title": "Demo",
"url": "https://demo.pingcode.com"
}
]
},
"environment": {
"id": "879b2014-6d56-4beb-9610-34a17dc7355b",
"type": "development",
"name": "development"
},
"operated_by": "631fec198a984680b564b2104ff79ce9"
}
}uninstall
通过用户界面触发卸载操作时,系统会发送名为 pce:nexus:app:uninstall 的事件。
数据示例:
javascript
{
"event_type": "pce:nexus:app:uninstall",
"payload": {
"installation_id": "d2b9c438-c0ee-4f4d-86dc-d6b263d83eff",
"app": {
"id": "33b0bb1b-429b-40eb-b4d5-5418584faa04",
"version": "1.0.0",
"name": "nexus-lifecycle-event",
"publisher": {
"name": "PingCode";
"site": "https://pingcode.com";
"phone": "12345678";
"email": "xxx@email.com";
},
"links": [
{
"title": "Demo",
"url": "https://demo.pingcode.com"
}
]
},
"environment": {
"id": "879b2014-6d56-4beb-9610-34a17dc7355b",
"type": "development",
"name": "development"
},
"operated_by": "631fec198a984680b564b2104ff79ce9"
}
}upgrade
对已安装应用升级至新版本时,会触发名为 pce:nexus:app:upgrade 的事件。
数据示例:
javascript
{
"event_type": "pce:nexus:app:upgrade",
"payload": {
"installation_id": "d2b9c438-c0ee-4f4d-86dc-d6b263d83eff",
"app": {
"id": "33b0bb1b-429b-40eb-b4d5-5418584faa04",
"version": "1.0.0",
"name": "nexus-lifecycle-event",
"publisher": {
"name": "PingCode";
"site": "https://pingcode.com";
"phone": "12345678";
"email": "xxx@email.com";
},
"links": [
{
"title": "Demo",
"url": "https://demo.pingcode.com"
}
]
},
"environment": {
"id": "879b2014-6d56-4beb-9610-34a17dc7355b",
"type": "development",
"name": "development"
},
"operated_by": "631fec198a984680b564b2104ff79ce9"
}
}