github.com/Axway/agent-sdk@v1.1.101/pkg/agent/teamcache.go (about) 1 package agent 2 3 import ( 4 "fmt" 5 "os" 6 "time" 7 8 "github.com/Axway/agent-sdk/pkg/apic/definitions" 9 10 "github.com/Axway/agent-sdk/pkg/jobs" 11 "github.com/Axway/agent-sdk/pkg/util/log" 12 ) 13 14 // QA EnvVars 15 const qaTeamCacheInterval = "QA_CENTRAL_TEAMCACHE_INTERVAL" 16 17 type centralTeamsCache struct { 18 lastUpdateTime time.Time 19 jobs.Job 20 } 21 22 func (j *centralTeamsCache) Ready() bool { 23 return true 24 } 25 26 func (j *centralTeamsCache) Status() error { 27 return nil 28 } 29 30 func (j *centralTeamsCache) Execute() error { 31 platformTeams, err := agent.apicClient.GetTeam(map[string]string{}) 32 if err != nil { 33 return err 34 } 35 36 if len(platformTeams) == 0 { 37 return fmt.Errorf("error: no teams returned from central") 38 } 39 40 for _, team := range platformTeams { 41 agent.cacheManager.AddTeam(&team) 42 } 43 j.lastUpdateTime = time.Now() 44 return nil 45 } 46 47 // registerTeamMapCacheJob - 48 func registerTeamMapCacheJob() { 49 RefreshTeamCache() 50 jobs.RegisterIntervalJobWithName(agent.teamJob, getJobInterval(), "Team Cache") 51 } 52 53 func RefreshTeamCache() { 54 if agent.teamJob == nil { 55 agent.teamJob = ¢ralTeamsCache{} 56 } 57 58 if agent.teamJob.lastUpdateTime.IsZero() || agent.teamJob.lastUpdateTime.Before(time.Now().Add(12*time.Hour)) { 59 // execute the job on startup to populate the team cache 60 agent.teamJob.Execute() 61 } 62 } 63 64 func getJobInterval() time.Duration { 65 interval := 12 * time.Hour 66 // check for QA env vars 67 if val := os.Getenv(qaTeamCacheInterval); val != "" { 68 if duration, err := time.ParseDuration(val); err == nil { 69 log.Tracef("Using %s (%s) rather than the default (%s) for non-QA", qaTeamCacheInterval, val, time.Hour) 70 interval = duration 71 } else { 72 log.Tracef("Could not use %s (%s) it is not a proper duration", qaTeamCacheInterval, val) 73 } 74 } 75 76 return interval 77 } 78 79 // GetTeamByName - Returns the PlatformTeam associated with the name 80 func GetTeamByName(name string) *definitions.PlatformTeam { 81 if agent.cacheManager != nil { 82 return agent.cacheManager.GetTeamByName(name) 83 } 84 return nil 85 } 86 87 // GetTeamByID - Returns the PlatformTeam associated with the id 88 func GetTeamByID(id string) *definitions.PlatformTeam { 89 if agent.cacheManager != nil { 90 return agent.cacheManager.GetTeamByID(id) 91 } 92 return nil 93 }