From 156e4eee865e7b094c1ea0fee34d2b407fa4828e Mon Sep 17 00:00:00 2001 From: Prabhjot Singh Sethi Date: Fri, 18 Apr 2025 10:24:15 +0000 Subject: [PATCH] Enable test binary for running aging process for owner table Signed-off-by: Prabhjot Singh Sethi --- sync/owner.go | 13 ++++++++++++- sync/test/example.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 sync/test/example.go diff --git a/sync/owner.go b/sync/owner.go index 1af031c..9779604 100644 --- a/sync/owner.go +++ b/sync/owner.go @@ -167,6 +167,17 @@ var ( // definition for all the consuming processes to ensure synchronisation to work // in a seemless manner func InitializeLockOwner(ctx context.Context, store db.Store, name string) error { + return InitializeLockOwnerWithUpdateInterval(ctx, store, name, defaultOwnerUpdateInterval) +} + +// Initialize the Lock Owner management constructs, anyone while working with +// this library requires to use this function before actually start consuming +// any functionality from here. +// This also allows specifying the interval to ensuring configurability +// Also it is callers responsibility to ensure providing uniform store +// definition for all the consuming processes to ensure synchronisation to work +// in a seemless manner +func InitializeLockOwnerWithUpdateInterval(ctx context.Context, store db.Store, name string, interval time.Duration) error { ownerTableInit.Lock() defer ownerTableInit.Unlock() if ownerTable != nil { @@ -180,7 +191,7 @@ func InitializeLockOwner(ctx context.Context, store db.Store, name string) error store: store, col: col, name: name, - updateInterval: time.Duration(defaultOwnerUpdateInterval * time.Second), + updateInterval: time.Duration(interval * time.Second), } // allocate owner entry context diff --git a/sync/test/example.go b/sync/test/example.go new file mode 100644 index 0000000..715f436 --- /dev/null +++ b/sync/test/example.go @@ -0,0 +1,45 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/Prabhjot-Sethi/core/db" + "github.com/Prabhjot-Sethi/core/errors" + "github.com/Prabhjot-Sethi/core/sync" +) + +func main() { + ctx, cancelfn := context.WithCancel(context.Background()) + defer time.Sleep(2 * time.Second) + defer cancelfn() + config := &db.MongoConfig{ + Host: "localhost", + Port: "27017", + Username: "root", + Password: "password", + } + + client, err := db.NewMongoClient(config) + + if err != nil { + log.Panicf("failed to connect to mongo DB Error: %s", err) + return + } + + err = client.HealthCheck(context.Background()) + if err != nil { + log.Panicf("failed to perform Health check with DB Error: %s", err) + } + + s := client.GetDataStore("test-sync") + err = sync.InitializeLockOwnerWithUpdateInterval(ctx, s, "test-owner", 1) + if err != nil && !errors.IsAlreadyExists(err) { + log.Panicf("Got error while initializing lock owner %s", err) + } + for { + // loop endlessly to run aging process for owner table + time.Sleep(5 * time.Second) + } +}