From d30266709ec0467f13ec5061f7893ee67895a9e5 Mon Sep 17 00:00:00 2001 From: Furkan Senharputlu Date: Sun, 25 Aug 2019 17:21:50 +0300 Subject: [PATCH] Save pump version in redis (#133) Related to TykTechnologies/product#42 --- main.go | 21 +++++++++++++++++---- storage/redis.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index d37d84148..13a2c822e 100644 --- a/main.go +++ b/main.go @@ -6,17 +6,17 @@ import ( "os" "github.com/gocraft/health" - "gopkg.in/vmihailenco/msgpack.v2" + msgpack "gopkg.in/vmihailenco/msgpack.v2" "github.com/TykTechnologies/logrus" - "github.com/TykTechnologies/logrus-prefixed-formatter" + prefixed "github.com/TykTechnologies/logrus-prefixed-formatter" "github.com/TykTechnologies/tyk-pump/analytics" "github.com/TykTechnologies/tyk-pump/analytics/demo" "github.com/TykTechnologies/tyk-pump/pumps" "github.com/TykTechnologies/tyk-pump/storage" - "github.com/TykTechnologies/tykcommon-logger" + logger "github.com/TykTechnologies/tykcommon-logger" - "gopkg.in/alecthomas/kingpin.v2" + kingpin "gopkg.in/alecthomas/kingpin.v2" ) var SystemConfig TykPumpConfiguration @@ -78,6 +78,15 @@ func setupAnalyticsStore() { UptimeStorage.Init(uptimeConf) } +func storeVersion() { + var versionStore = &storage.RedisClusterStorageManager{} + versionConf := SystemConfig.AnalyticsStorageConfig + versionStore.KeyPrefix = "version-check-" + versionStore.Config = versionConf + versionStore.Connect() + versionStore.SetKey("pump", VERSION, 0) +} + func initialisePumps() { Pumps = make([]pumps.Pump, len(SystemConfig.Pumps)) i := 0 @@ -182,6 +191,10 @@ func writeToPumps(keys []interface{}, job *health.Job, startTime time.Time) { func main() { SetupInstrumentation() + // Store version which will be read by dashboard and sent to + // vclu(version check and licecnse utilisation) service + storeVersion() + // Create the store setupAnalyticsStore() diff --git a/storage/redis.go b/storage/redis.go index 72a220cf8..850e9f04c 100644 --- a/storage/redis.go +++ b/storage/redis.go @@ -248,3 +248,46 @@ func (r *RedisClusterStorageManager) GetAndDeleteSet(keyName string) []interface return vals } + +// SetKey will create (or update) a key value in the store +func (r *RedisClusterStorageManager) SetKey(keyName, session string, timeout int64) error { + log.Debug("[STORE] SET Raw key is: ", keyName) + log.Debug("[STORE] Setting key: ", r.fixKey(keyName)) + + r.ensureConnection() + _, err := r.db.Do("SET", r.fixKey(keyName), session) + if timeout > 0 { + if err := r.SetExp(keyName, timeout); err != nil { + return err + } + } + if err != nil { + log.Error("Error trying to set value: ", err) + return err + } + return nil +} + +func (r *RedisClusterStorageManager) SetExp(keyName string, timeout int64) error { + _, err := r.db.Do("EXPIRE", r.fixKey(keyName), timeout) + if err != nil { + log.Error("Could not EXPIRE key: ", err) + } + return err +} + +func (r *RedisClusterStorageManager) ensureConnection() { + if r.db != nil { + // already connected + return + } + log.Info("Connection dropped, reconnecting...") + for { + r.Connect() + if r.db != nil { + // reconnection worked + return + } + log.Info("Reconnecting again...") + } +}