aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamal Carvalho <jamal@golang.org>2022-01-06 21:13:10 +0000
committerJamal Carvalho <jamal@golang.org>2022-01-06 23:49:05 +0000
commitcf9e9d0dbb309851faec02cf2ca1fc9b28d0f694 (patch)
tree77a6ac87459bedfb820c05738a05f92b2805161b
parent46c69fbd4081c98528db2f73027e4c2c6b530f18 (diff)
downloadgo-x-pkgsite-cf9e9d0dbb309851faec02cf2ca1fc9b28d0f694.tar.xz
cmd,internal: add worker endpoint to clear beta cache
Added an endpoint to clear the cache during cloudbuild deployments of the beta frontend. Change-Id: I3fb19c3f1eef6b1b15eda2601b27a403b39b0f8d Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/375956 Trust: Jamal Carvalho <jamal@golang.org> Run-TryBot: Jamal Carvalho <jamal@golang.org> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Julie Qiu <julie@golang.org>
-rw-r--r--cmd/worker/main.go24
-rw-r--r--internal/config/config.go3
-rw-r--r--internal/worker/server.go50
3 files changed, 48 insertions, 29 deletions
diff --git a/cmd/worker/main.go b/cmd/worker/main.go
index 6633a97e..64124961 100644
--- a/cmd/worker/main.go
+++ b/cmd/worker/main.go
@@ -92,17 +92,19 @@ func main() {
reportingClient := cmdconfig.ReportingClient(ctx, cfg)
redisCacheClient := getCacheRedis(ctx, cfg)
+ redisBetaCacheClient := getBetaCacheRedis(ctx, cfg)
experimenter := cmdconfig.Experimenter(ctx, cfg, expg, reportingClient)
server, err := worker.NewServer(cfg, worker.ServerConfig{
- DB: db,
- IndexClient: indexClient,
- ProxyClient: proxyClient,
- SourceClient: sourceClient,
- RedisCacheClient: redisCacheClient,
- Queue: fetchQueue,
- ReportingClient: reportingClient,
- StaticPath: template.TrustedSourceFromFlag(flag.Lookup("static").Value),
- GetExperiments: experimenter.Experiments,
+ DB: db,
+ IndexClient: indexClient,
+ ProxyClient: proxyClient,
+ SourceClient: sourceClient,
+ RedisCacheClient: redisCacheClient,
+ RedisBetaCacheClient: redisBetaCacheClient,
+ Queue: fetchQueue,
+ ReportingClient: reportingClient,
+ StaticPath: template.TrustedSourceFromFlag(flag.Lookup("static").Value),
+ GetExperiments: experimenter.Experiments,
})
if err != nil {
log.Fatal(ctx, err)
@@ -157,6 +159,10 @@ func getCacheRedis(ctx context.Context, cfg *config.Config) *redis.Client {
return getRedis(ctx, cfg.RedisCacheHost, cfg.RedisCachePort, 0, 6*time.Second)
}
+func getBetaCacheRedis(ctx context.Context, cfg *config.Config) *redis.Client {
+ return getRedis(ctx, cfg.RedisBetaCacheHost, cfg.RedisCachePort, 0, 6*time.Second)
+}
+
func getRedis(ctx context.Context, host, port string, writeTimeout, readTimeout time.Duration) *redis.Client {
if host == "" {
return nil
diff --git a/internal/config/config.go b/internal/config/config.go
index 8287688e..739f5d9f 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -164,7 +164,7 @@ type Config struct {
DBPassword string `json:"-"`
// Configuration for redis page cache.
- RedisCacheHost, RedisCachePort string
+ RedisCacheHost, RedisBetaCacheHost, RedisCachePort string
// UseProfiler specifies whether to enable Stackdriver Profiler.
UseProfiler bool
@@ -386,6 +386,7 @@ func Init(ctx context.Context) (_ *Config, err error) {
DBName: GetEnv("GO_DISCOVERY_DATABASE_NAME", "discovery-db"),
DBSecret: os.Getenv("GO_DISCOVERY_DATABASE_SECRET"),
RedisCacheHost: os.Getenv("GO_DISCOVERY_REDIS_HOST"),
+ RedisBetaCacheHost: os.Getenv("GO_DISCOVERY_REDIS_BETA_HOST"),
RedisCachePort: GetEnv("GO_DISCOVERY_REDIS_PORT", "6379"),
Quota: QuotaSettings{
Enable: os.Getenv("GO_DISCOVERY_ENABLE_QUOTA") == "true",
diff --git a/internal/worker/server.go b/internal/worker/server.go
index aa069a21..a7ef8582 100644
--- a/internal/worker/server.go
+++ b/internal/worker/server.go
@@ -48,6 +48,7 @@ type Server struct {
proxyClient *proxy.Client
sourceClient *source.Client
cache *cache.Cache
+ betaCache *cache.Cache
db *postgres.DB
queue queue.Queue
reportingClient *errorreporting.Client
@@ -60,15 +61,16 @@ type Server struct {
// ServerConfig contains everything needed by a Server.
type ServerConfig struct {
- DB *postgres.DB
- IndexClient *index.Client
- ProxyClient *proxy.Client
- SourceClient *source.Client
- RedisCacheClient *redis.Client
- Queue queue.Queue
- ReportingClient *errorreporting.Client
- StaticPath template.TrustedSource
- GetExperiments func() []*internal.Experiment
+ DB *postgres.DB
+ IndexClient *index.Client
+ ProxyClient *proxy.Client
+ SourceClient *source.Client
+ RedisCacheClient *redis.Client
+ RedisBetaCacheClient *redis.Client
+ Queue queue.Queue
+ ReportingClient *errorreporting.Client
+ StaticPath template.TrustedSource
+ GetExperiments func() []*internal.Experiment
}
const (
@@ -98,6 +100,10 @@ func NewServer(cfg *config.Config, scfg ServerConfig) (_ *Server, err error) {
if scfg.RedisCacheClient != nil {
c = cache.New(scfg.RedisCacheClient)
}
+ var bc *cache.Cache
+ if scfg.RedisBetaCacheClient != nil {
+ bc = cache.New(scfg.RedisBetaCacheClient)
+ }
// Update information about DB locks, etc. every few seconds.
p := poller.New(&postgres.UserInfo{}, func(ctx context.Context) (interface{}, error) {
@@ -112,6 +118,7 @@ func NewServer(cfg *config.Config, scfg ServerConfig) (_ *Server, err error) {
proxyClient: scfg.ProxyClient,
sourceClient: scfg.SourceClient,
cache: c,
+ betaCache: bc,
queue: scfg.Queue,
reportingClient: scfg.ReportingClient,
templates: templates,
@@ -213,7 +220,10 @@ func (s *Server) Install(handle func(string, http.Handler)) {
handle("/repopulate-search-documents", rmw(s.errorHandler(s.handleRepopulateSearchDocuments)))
// manual: clear-cache clears the redis cache.
- handle("/clear-cache", rmw(s.errorHandler(s.clearCache)))
+ handle("/clear-cache", rmw(s.clearCache(s.cache)))
+
+ // manual: clear-beta-cache clears the redis beta cache.
+ handle("/clear-beta-cache", rmw(s.clearCache(s.betaCache)))
// manual: delete the specified module version.
handle("/delete/", http.StripPrefix("/delete", rmw(s.errorHandler(s.handleDelete))))
@@ -620,15 +630,17 @@ func (s *Server) handleReprocess(w http.ResponseWriter, r *http.Request) error {
return nil
}
-func (s *Server) clearCache(w http.ResponseWriter, r *http.Request) error {
- if s.cache == nil {
- return errors.New("redis cache client is not configured")
- }
- if err := s.cache.Clear(r.Context()); err != nil {
- return err
- }
- fmt.Fprint(w, "Cache cleared.")
- return nil
+func (s *Server) clearCache(cache *cache.Cache) http.HandlerFunc {
+ return s.errorHandler(func(w http.ResponseWriter, r *http.Request) error {
+ if cache == nil {
+ return errors.New("redis cache client is not configured")
+ }
+ if err := cache.Clear(r.Context()); err != nil {
+ return err
+ }
+ fmt.Fprint(w, "Cache cleared.")
+ return nil
+ })
}
// handleDelete deletes the specified module version.