Skip to content

Commit

Permalink
feat: add components clean up job
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Jan 2, 2024
1 parent 566b467 commit b22e2bd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
16 changes: 16 additions & 0 deletions job/components.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package job

import (
"time"

"github.com/flanksource/duty/context"
)

func CleanupSoftDeletedComponents(ctx context.Context, olderThan time.Duration) (int, error) {
tx := ctx.DB().Exec("DELETE FROM components WHERE deleted_at < NOW() - interval '1 SECONDS' * ?", int64(olderThan.Seconds()))
if tx.Error != nil {
return 0, tx.Error
}

return int(tx.RowsAffected), nil
}
5 changes: 5 additions & 0 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
ResourceTypeComponent = "components"
ResourceTypeCheckStatuses = "check_statuses"
)

var RetentionHour = Retention{
Success: 1,
Failed: 3,
Expand Down
17 changes: 0 additions & 17 deletions notification.go

This file was deleted.

47 changes: 47 additions & 0 deletions tests/job_components_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tests

import (
"time"

"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/tests/fixtures/dummy"
"github.com/google/uuid"
ginkgo "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/samber/lo"
)

var _ = ginkgo.Describe("Soft deleted components", ginkgo.Ordered, func() {
var softDeletedComponents []models.Component

ginkgo.It("should populated dummy deleted components", func() {
data := dummy.GenerateDynamicDummyData(DefaultContext.DB())
for i := range data.Components {
data.Components[i].AgentID = uuid.Nil

if i == 0 {
data.Components[i].DeletedAt = lo.ToPtr(dummy.CurrentTime.Add(-10 * time.Minute))
continue
}

data.Components[i].DeletedAt = lo.ToPtr(dummy.CurrentTime.Add(-time.Hour * 24 * 7))
}

softDeletedComponents = data.Components
err := DefaultContext.DB().Create(&softDeletedComponents).Error
Expect(err).ToNot(HaveOccurred())
})

ginkgo.It("should delete soft deleted components", func() {
count, err := job.CleanupSoftDeletedComponents(DefaultContext, time.Hour*24)
Expect(err).ToNot(HaveOccurred())

Expect(count).To(Equal(len(softDeletedComponents) - 1))
})

ginkgo.It("should cleanup the newly added soft deleted components", func() {
err := DefaultContext.DB().Delete(&softDeletedComponents).Error
Expect(err).ToNot(HaveOccurred())
})
})

0 comments on commit b22e2bd

Please sign in to comment.