-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: change config retention to a job
- Loading branch information
1 parent
9319631
commit f6709cd
Showing
3 changed files
with
49 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package jobs | ||
|
||
import ( | ||
gocontext "context" | ||
"encoding/json" | ||
|
||
"github.com/flanksource/commons/logger" | ||
"github.com/flanksource/config-db/api/v1" | ||
"github.com/flanksource/config-db/db" | ||
"github.com/flanksource/config-db/scrapers" | ||
"github.com/flanksource/duty/context" | ||
"github.com/flanksource/duty/models" | ||
) | ||
|
||
func ProcessChangeRetentionRules() { | ||
ctx := context.NewContext(gocontext.Background()).WithDB(db.DefaultDB(), db.Pool) | ||
jobHistory := models.NewJobHistory("ProcessChangeRetentionRules", "", "").Start() | ||
_ = db.PersistJobHistory(jobHistory) | ||
defer func() { | ||
_ = db.PersistJobHistory(jobHistory.End()) | ||
}() | ||
|
||
var activeScrapers []models.ConfigScraper | ||
if err := ctx.DB().Where("deleted_at IS NULL").Find(&activeScrapers).Error; err != nil { | ||
logger.Errorf("Error querying config scrapers from db: %v", err) | ||
jobHistory.AddError(err.Error()) | ||
return | ||
} | ||
|
||
for _, s := range activeScrapers { | ||
var spec v1.ScraperSpec | ||
if err := json.Unmarshal([]byte(s.Spec), &spec); err != nil { | ||
logger.Errorf("Error unmarshaling config scraper[%s] into json: %v", s.ID, err) | ||
jobHistory.AddError(err.Error()) | ||
continue | ||
} | ||
|
||
for _, changeSpec := range spec.Retention.Changes { | ||
err := scrapers.ProcessChangeRetention(ctx, s.ID, changeSpec) | ||
if err != nil { | ||
logger.Errorf("Error processing change retention for scraper[%s] config analysis: %v", s.ID, err) | ||
jobHistory.AddError(err.Error()) | ||
} else { | ||
jobHistory.IncrSuccess() | ||
} | ||
} | ||
} | ||
} |
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