Skip to content

Commit

Permalink
Update ops to run planpreview cleaner at a fixed time (#2368)
Browse files Browse the repository at this point in the history
**What this PR does / why we need it**:

- Fix the time to run the job to make it becomes more predictable.
- This also ensures that the job will be executed once per day even if the ops was restarted or deployed. 

**Which issue(s) this PR fixes**:

Fixes #

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
-->
```release-note
NONE
```

This PR was merged by Kapetanios.
  • Loading branch information
nghialv committed Aug 18, 2021
1 parent 44d741d commit 71ee2e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions pkg/app/ops/planpreviewoutputcleaner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/filestore:go_default_library",
"@com_github_robfig_cron_v3//:go_default_library",
"@org_uber_go_zap//:go_default_library",
],
)
26 changes: 14 additions & 12 deletions pkg/app/ops/planpreviewoutputcleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import (
"fmt"
"time"

"github.com/robfig/cron/v3"
"go.uber.org/zap"

"github.com/pipe-cd/pipe/pkg/filestore"
)

const (
outputTTL = 48 * time.Hour
interval = 24 * time.Hour
prefix = "command-output/"
outputTTL = 48 * time.Hour
cronSchedule = "0 9 * * *" // Run at 09:00 every day.
interval = 24 * time.Hour
prefix = "command-output/"
)

type store interface {
Expand All @@ -50,22 +52,22 @@ func NewCleaner(s store, logger *zap.Logger) *Cleaner {
func (c *Cleaner) Run(ctx context.Context) error {
c.logger.Info("start running planpreview ouput cleaner")

t := time.NewTicker(interval)
for {
select {
case <-ctx.Done():
break

case <-t.C:
c.clean(ctx)
}
cr := cron.New()
if _, err := cr.AddFunc(cronSchedule, func() { c.clean(ctx) }); err != nil {
return err
}

cr.Start()
<-ctx.Done()
cr.Stop()

c.logger.Info("planpreview output cleaner has been stopped")
return nil
}

func (c *Cleaner) clean(ctx context.Context) error {
c.logger.Info("will find stale planpreview outputs to delete")

objects, err := c.store.List(ctx, prefix)
if err != nil {
c.logger.Error("failed to list planpreview output objects",
Expand Down

0 comments on commit 71ee2e6

Please sign in to comment.