This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
cleanup.go
132 lines (114 loc) · 3.86 KB
/
cleanup.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package cleanup implements periodic data deletion.
package cleanup
import (
"context"
"fmt"
"net/http"
"time"
"github.com/google/exposure-notifications-verification-server/pkg/config"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/render"
"github.com/google/exposure-notifications-server/pkg/cache"
"github.com/google/exposure-notifications-server/pkg/logging"
"go.uber.org/zap"
)
// Controller is a controller for the cleanup service.
type Controller struct {
config *config.CleanupConfig
cache *cache.Cache
db *database.Database
h *render.Renderer
logger *zap.SugaredLogger
}
// New creates a new cleanup controller.
func New(ctx context.Context, config *config.CleanupConfig, cache *cache.Cache, db *database.Database, h *render.Renderer) *Controller {
logger := logging.FromContext(ctx)
return &Controller{
config: config,
cache: cache,
db: db,
h: h,
logger: logger,
}
}
func (c *Controller) shouldCleanup() error {
cStatCache, err := c.cache.WriteThruLookup(database.CleanupName,
func() (interface{}, error) {
cState, err := c.db.FindCleanupStatus(database.CleanupName)
if err != nil {
return nil, err
}
return cState, err
})
if err != nil {
// in case this was not found, create a new record.
cStatCache, err = c.db.CreateCleanup(database.CleanupName)
if err != nil {
return fmt.Errorf("error attempting to backfill cleanup config: %v", err)
}
}
cStat, ok := cStatCache.(*database.CleanupStatus)
if !ok {
return fmt.Errorf("cleanup cache is typed incorrectly")
}
if cStat.NotBefore.After(time.Now().UTC()) {
return fmt.Errorf("skipping cleanup. no cleanup before %v", cStat.NotBefore)
}
// Attempt to advance the generation.
cStat, err = c.db.ClaimCleanup(cStat, c.config.CleanupPeriod)
if err != nil {
return fmt.Errorf("unable to lock cleanup: %v", err)
}
if err := c.cache.Set(database.CleanupName, cStat); err != nil {
return fmt.Errorf("failed to set cache: %w", err)
}
return nil
}
func (c *Controller) HandleCleanup() http.Handler {
type CleanupResult struct {
Cleanup bool `json:"cleanup"`
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := c.shouldCleanup(); err != nil {
c.logger.Errorf("shouldCleanUp: %v", err)
c.h.RenderJSON(w, http.StatusOK, &CleanupResult{false})
return
}
// Cleanup tasks
if count, err := c.db.PurgeVerificationCodes(c.config.VerificationCodeMaxAge); err != nil {
c.logger.Errorf("db.PurgeVerificationCodes: %v", err)
} else {
c.logger.Infof("purged %v verification codes", count)
}
if count, err := c.db.PurgeTokens(c.config.VerificationTokenMaxAge); err != nil {
c.logger.Errorf("db.PurgeTokens: %v", err)
} else {
c.logger.Infof("purged %v verification tokens", count)
}
// Gather usage statistics
if err := c.db.UpdateUserStats(); err != nil {
c.logger.Errorf("db.UpdateUserStats: %v", err)
} else {
c.logger.Info("updated statistics for user records")
}
if err := c.db.UpdateAuthorizedAppStats(); err != nil {
c.logger.Errorf("db.UpdateAuthorizedAppStats: %v", err)
} else {
c.logger.Info("updated statistics for authorized app records")
}
c.h.RenderJSON(w, http.StatusOK, &CleanupResult{true})
})
}