Skip to content

Commit

Permalink
Save pump version in redis (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu authored and buger committed Aug 25, 2019
1 parent 3bb755d commit d302667
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
21 changes: 17 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
43 changes: 43 additions & 0 deletions storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
}
}

0 comments on commit d302667

Please sign in to comment.