feat: add generic queue and subscription retirement
This commit is contained in:
77
sdk/go/examples/resource-lifecycle/main.go
Normal file
77
sdk/go/examples/resource-lifecycle/main.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// resource-lifecycle 创建并退役本次独占的空队列与订阅,不处理任何现有资源。
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
orbit "git.yuntuo.hk/k2safe/orbit/sdk/go"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
func run() error {
|
||||
token := os.Getenv("ORBIT_TOKEN")
|
||||
if path := os.Getenv("ORBIT_CREDENTIALS_FILE"); token == "" && path != "" {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var c struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err = json.Unmarshal(raw, &c); err != nil {
|
||||
return err
|
||||
}
|
||||
token = c.Token
|
||||
}
|
||||
client, err := orbit.NewClient(os.Getenv("ORBIT_URL"), orbit.Options{Token: token})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
var random [16]byte
|
||||
if _, err = rand.Read(random[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
runID := hex.EncodeToString(random[:])
|
||||
q, err := client.CreateQueue(ctx, runID+"-queue", orbit.CreateQueueRequest{Name: "lifecycle-" + runID, MaxConcurrency: 1, RatePerSecond: 10})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("queue=%s\n", q.Queue.ID)
|
||||
sub, err := client.CreateSubscription(ctx, runID+"-sub", orbit.CreateSubscriptionRequest{Name: "lifecycle-" + runID, QueueID: q.Queue.ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("subscription=%s\n", sub.Subscription.ID)
|
||||
in := orbit.RetireResourceRequest{ExpectedRevision: sub.Subscription.Revision, Reason: "isolated lifecycle example completed"}
|
||||
if _, err = client.RetireSubscription(ctx, sub.Subscription.ID, runID+"-retire-sub", in); err != nil {
|
||||
return err
|
||||
}
|
||||
replay, err := client.RetireSubscription(ctx, sub.Subscription.ID, runID+"-retire-sub", in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !replay.Deduplicated {
|
||||
return fmt.Errorf("retirement did not preserve operation identity")
|
||||
}
|
||||
retired, err := client.RetireQueue(ctx, q.Queue.ID, runID+"-retire-queue", orbit.RetireResourceRequest{ExpectedRevision: q.Queue.Revision, Reason: "isolated lifecycle example completed"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if retired.Queue.RetiredAt == nil {
|
||||
return fmt.Errorf("queue retirement missing")
|
||||
}
|
||||
fmt.Println("retired=true history_preserved=true subscription_replay=true")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user