You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
37 lines
1.3 KiB
package console
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"yunyan/comm"
|
|
natssys "yunyan/sys/nats"
|
|
)
|
|
|
|
// ConfigEvent 后台配置调整事件。console 改动某应用配置后,向该应用发此事件,
|
|
// 应用侧订阅约定的 subject 后据此热重载配置(应用侧订阅是后续工作)。
|
|
type ConfigEvent struct {
|
|
AppId uint32 `json:"app_id"` // 目标应用
|
|
Type string `json:"type"` // 配置类型,如 global_config / app_config / mcp
|
|
Action string `json:"action"` // add / update / delete / reload
|
|
Payload json.RawMessage `json:"payload,omitempty"` // 变更内容(可选)
|
|
Ts int64 `json:"ts"` // 发出时间戳(秒)
|
|
}
|
|
|
|
// eventSubject 取该应用的事件 subject;注册项未配置时回退到 "<前缀><appId>"。
|
|
func eventSubject(app *AppRegistry) string {
|
|
if app.NatsSubject != "" {
|
|
return app.NatsSubject
|
|
}
|
|
return fmt.Sprintf("%s%d", comm.Nats_ConfigEventSubjectPrefix, app.Id)
|
|
}
|
|
|
|
// publishConfigEvent 向目标应用发布配置调整事件(NATS JetStream 同步发布,等待 ack)。
|
|
func publishConfigEvent(app *AppRegistry, ev *ConfigEvent) error {
|
|
subject := eventSubject(app)
|
|
data, err := json.Marshal(ev)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return natssys.Publish(subject, data)
|
|
}
|
|
|