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