From b71b6d5da242ebd038f768cf2c6a5723553c73fb Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Mon, 24 Nov 2025 20:41:24 +0700 Subject: [PATCH] feat(schedule): add ticker --- couchbase/README.md | 2 +- couchbase/main.go | 65 ++++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/couchbase/README.md b/couchbase/README.md index a363e85..5bd6891 100644 --- a/couchbase/README.md +++ b/couchbase/README.md @@ -1,2 +1,2 @@ # couchbase-keepalive -A lightweight Go utility that performs periodic randomized get/set operations to prevent Couchbase Capella free clusters from entering inactive state. +A lightweight Go utility that performs periodic operations to prevent Couchbase Capella free clusters from entering inactive state. diff --git a/couchbase/main.go b/couchbase/main.go index ec9ffdc..8dd1be4 100644 --- a/couchbase/main.go +++ b/couchbase/main.go @@ -1,9 +1,11 @@ package main import ( - "fmt" + "context" "log" "os" + "os/signal" + "syscall" "time" "github.com/couchbase/gocb/v2" @@ -82,37 +84,46 @@ func main() { col := bucket.Scope(scopeName).Collection(collectionName) - // Create and store a Document - type User struct { - Name string `json:"name"` - Email string `json:"email"` - Interests []string `json:"interests"` - } + ctx, cancel := context.WithCancel(context.Background()) - upsertOptions := gocb.UpsertOptions{ - Expiry: 60 * time.Second, - } + go func() { + ticker := time.NewTicker(time.Minute) + defer ticker.Stop() - _, err = col.Upsert("u:jade", - User{ - Name: "Jade", - Email: "jade@test-email.com", - Interests: []string{"Swimming", "Rowing"}, - }, &upsertOptions) - if err != nil { - log.Fatal(err) - } + for { + select { + case <-ticker.C: + if err := incrementCounter(col); err != nil { + log.Printf("Keepalive increment error: %v", err) + } + case <-ctx.Done(): + return + } + } + }() - // Get the document back - getResult, err := col.Get("u:jade", nil) - if err != nil { - log.Fatal(err) - } + defer func() { + cancel() + if err := cluster.Close(nil); err != nil { + log.Printf("Error closing cluster: %v", err) + } + }() - var inUser User - err = getResult.Content(&inUser) + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) + <-sigCh +} + +func incrementCounter(col *gocb.Collection) error { + counterDocId := "counter" + // Increment by 1, creating doc if needed. + // By using `Initial: 1` we set the starting count(non-negative) to 1 if the document needs to be created. + // If it already exists, the count will increase by the amount provided in the Delta option(i.e 1). + increment, err := col.Binary().Increment(counterDocId, &gocb.IncrementOptions{Initial: 1, Delta: 1}) if err != nil { log.Fatal(err) + return err } - fmt.Printf("User: %v\n", inUser) + log.Printf("Counter : %d\n", increment.Content()) + return nil }