-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into cloud_billing
- Loading branch information
Showing
141 changed files
with
3,934 additions
and
8,215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package worker | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"cloud.google.com/go/storage" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
const _unusedAssetsPageSize = 100 | ||
|
||
func (w *Worker) deleteUnusedAssets(ctx context.Context) error { | ||
for { | ||
// 1. Fetch unused assets | ||
assets, err := w.admin.DB.FindUnusedAssets(ctx, _unusedAssetsPageSize) | ||
if err != nil { | ||
return err | ||
} | ||
if len(assets) == 0 { | ||
return nil | ||
} | ||
|
||
// 2. Delete objects from cloud storage | ||
// Limit the number of concurrent deletes to 8 | ||
// TODO: Use batch API once google-cloud-go supports it | ||
group, cctx := errgroup.WithContext(ctx) | ||
group.SetLimit(8) | ||
var ids []string | ||
for _, asset := range assets { | ||
ids = append(ids, asset.ID) | ||
group.Go(func() error { | ||
parsed, err := url.Parse(asset.Path) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse asset path %q: %w", asset.Path, err) | ||
} | ||
err = w.admin.Assets.Object(strings.TrimPrefix(parsed.Path, "/")).Delete(cctx) | ||
if err != nil && !errors.Is(err, storage.ErrObjectNotExist) { | ||
return fmt.Errorf("failed to delete asset %q: %w", asset.Path, err) | ||
} | ||
return nil | ||
}) | ||
} | ||
err = group.Wait() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 3. Delete the assets in the DB | ||
err = w.admin.DB.DeleteAssets(ctx, ids) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(assets) < _unusedAssetsPageSize { | ||
// no more assets to delete | ||
return nil | ||
} | ||
// fetch again could be more unused assets | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.