docs: consolidate runnable examples and publish v0.1.1 guides
This commit is contained in:
32
examples/subscriptions/README.md
Normal file
32
examples/subscriptions/README.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# 结果订阅管理
|
||||
|
||||
创建订阅、同键核对、暂停、恢复及查询,最后暂停本次订阅。
|
||||
|
||||
## 准备
|
||||
|
||||
- Go 1.27.1、当前版本 Orbit;服务启动和初始化见[仓库 README](../../README.md#快速开始)。
|
||||
- 初始化时增加 --event-access。
|
||||
- [示例总索引](../README.md)提供统一初始化和权限说明;完整字段、失败处理见[详细手册](../../docs/examples/subscriptions.md)。
|
||||
|
||||
## 运行
|
||||
|
||||
以下命令从仓库根目录执行;凭据路径替换为实际初始化输出。
|
||||
|
||||
```sh
|
||||
cd examples
|
||||
export ORBIT_URL=http://127.0.0.1:8080
|
||||
export ORBIT_CREDENTIALS_FILE="$PWD/../.local/development.credentials.json"
|
||||
go run ./subscriptions
|
||||
```
|
||||
|
||||
## 结果核对
|
||||
|
||||
duplicate=true resumed_revision=3;final_enabled=false revision=4 pending=0。具体 ID 和统计值以本次运行为准。程序会核对上述关键条件,失败时打印错误并以非零状态退出。
|
||||
|
||||
## 接入自己的项目
|
||||
|
||||
阅读[main.go](main.go)中的客户端创建、请求及错误处理。业务项目通过 `go get git.yuntuo.hk/k2safe/orbit/sdk/go@v0.1.1` 引入 SDK,无需导入本示例包。
|
||||
|
||||
示例运行会写入真实平台数据,应使用专用测试应用/队列。正常结束保留历史和审计;重复演示通常创建新身份,不能当作上次失败操作的恢复。真实业务应在第一次请求前持久化操作身份,网络结果不明时沿原身份核对。
|
||||
|
||||
每次演示保留一个暂停订阅,命名空间最多 64 个订阅;不要将重复运行示例作为持续消费者。
|
||||
109
examples/subscriptions/main.go
Normal file
109
examples/subscriptions/main.go
Normal file
@@ -0,0 +1,109 @@
|
||||
// subscriptions 演示订阅管理和幂等回执;事件消费及Inbox完整示例随后独立提供。
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
orbit "git.yuntuo.hk/k2safe/orbit/sdk/go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
func run() error {
|
||||
token, queue := os.Getenv("ORBIT_TOKEN"), os.Getenv("ORBIT_QUEUE_ID")
|
||||
if path := os.Getenv("ORBIT_CREDENTIALS_FILE"); path != "" {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var credential struct {
|
||||
Token string `json:"token"`
|
||||
QueueID string `json:"queue_id"`
|
||||
}
|
||||
if err = json.Unmarshal(raw, &credential); err != nil {
|
||||
return err
|
||||
}
|
||||
if token == "" {
|
||||
token = credential.Token
|
||||
}
|
||||
if queue == "" {
|
||||
queue = credential.QueueID
|
||||
}
|
||||
}
|
||||
if token == "" || queue == "" {
|
||||
return errors.New("provide an event-enabled credential and queue via ORBIT_CREDENTIALS_FILE or environment")
|
||||
}
|
||||
client, err := orbit.NewClient(os.Getenv("ORBIT_URL"), orbit.Options{Token: token})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
var random [16]byte
|
||||
if _, err = rand.Read(random[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
operation := hex.EncodeToString(random[:])
|
||||
input := orbit.CreateSubscriptionRequest{Name: "results-" + operation, QueueID: queue, Types: []string{"task.succeeded", "task.failed", "task.cancelled", "task.needs_review"}, MaxPending: 100, MaxAttempts: 20}
|
||||
created, err := client.CreateSubscription(ctx, "create-"+operation, input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
duplicate, err := client.CreateSubscription(ctx, "create-"+operation, input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !duplicate.Deduplicated || duplicate.Subscription.ID != created.Subscription.ID {
|
||||
return errors.New("subscription identity was not preserved")
|
||||
}
|
||||
sub := created.Subscription
|
||||
configure := func(enabled bool, key string) error {
|
||||
out, err := client.ConfigureSubscription(ctx, sub.ID, key, orbit.ConfigureSubscriptionRequest{ExpectedRevision: sub.Revision, Enabled: enabled, Reason: "subscription management example"})
|
||||
if err == nil {
|
||||
sub = out.Subscription
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err = configure(false, "pause-"+operation); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = configure(true, "resume-"+operation); err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := client.GetSubscription(ctx, sub.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !current.Enabled || current.Revision != 3 {
|
||||
return errors.New("subscription control state mismatch")
|
||||
}
|
||||
page, err := client.ListSubscriptions(ctx, queue, orbit.SubscriptionPageRequest{Limit: 100})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
found := false
|
||||
for _, item := range page.Items {
|
||||
found = found || item.ID == sub.ID
|
||||
}
|
||||
if !found {
|
||||
return errors.New("subscription absent from scoped listing")
|
||||
}
|
||||
// 示例不启动消费者。结束时暂停本次订阅,保留配置和审计以便核查。
|
||||
if err = configure(false, "finish-"+operation); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("subscription=%s duplicate=true resumed_revision=3\n", sub.ID)
|
||||
fmt.Printf("final_enabled=%t revision=%d pending=%d\n", sub.Enabled, sub.Revision, sub.PendingCount)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user