go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tree_status/internal/status/clear_users_cron.go (about)

     1  // Copyright 2024 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package status
    16  
    17  import (
    18  	"context"
    19  
    20  	"cloud.google.com/go/spanner"
    21  
    22  	"go.chromium.org/luci/common/logging"
    23  	"go.chromium.org/luci/server/span"
    24  )
    25  
    26  // ClearStatusUsers clears the column CreateUser in table Status.
    27  //
    28  // The retention policy is 63 days, but we delete it at 30 days
    29  // to allow time to fix issues with the deletion process, as
    30  // well as allow time for data to be wiped from the underlying
    31  // storage media.
    32  func ClearStatusUsers(ctx context.Context) error {
    33  	affectedRows, err := clearCreateUserColumn(ctx)
    34  	if err != nil {
    35  		logging.Errorf(ctx, "Failed to clear creation users: %w", err)
    36  		return err
    37  	}
    38  	logging.Infof(ctx, "Cleared %d create users", affectedRows)
    39  
    40  	return nil
    41  }
    42  
    43  // clearCreateUserColumn clears column CreateUser in the Status table which
    44  // have passed 30 days to keep inline with our retention policy.
    45  //
    46  // This function returns the number of affected rows.
    47  //
    48  // Column `CreateTime` is used to determine which rows to clear
    49  // the `CreateUser` column for.
    50  func clearCreateUserColumn(ctx context.Context) (int64, error) {
    51  	stmt := spanner.NewStatement(`
    52  	UPDATE Status
    53  	SET  CreateUser = ''
    54  	WHERE CreateTime <= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
    55  	AND CreateUser <> ''
    56  	`)
    57  	var rows int64
    58  	var err error
    59  	_, err = span.ReadWriteTransaction(ctx, func(ctx context.Context) error {
    60  		rows, err = span.Update(ctx, stmt)
    61  		return err
    62  	})
    63  	if err != nil {
    64  		return 0, err
    65  	}
    66  	return rows, nil
    67  }