-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cron + graceful api shutdown (#43)
* add cron + graceful api shutdown * pass hours to delete as parameter * remove duplicate start * fix server shut down handling+other decoded types
- Loading branch information
Showing
6 changed files
with
109 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cron | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dappnode/validator-monitoring/listener/internal/logger" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
// RemoveOldSignatures deletes signatures older than a specified number of hours from the MongoDB collection | ||
func RemoveOldSignatures(collection *mongo.Collection, hours int) { | ||
logger.Debug(fmt.Sprintf("Removing signatures older than %d hours", hours)) | ||
targetTimeMillis := time.Now().Add(time.Duration(-hours) * time.Hour).UnixMilli() // Calculate time in the past based on hours | ||
filter := bson.M{ | ||
"entries.decodedPayload.timestamp": bson.M{ | ||
"$lt": fmt.Sprintf("%d", targetTimeMillis), // Compare timestamps as strings | ||
}, | ||
} | ||
// DeleteMany returns the number of documents deleted, it is useless for us since we're never | ||
// deleting a document, but an entry on its "entries" array | ||
_, err := collection.DeleteMany(context.Background(), filter) | ||
if err != nil { | ||
logger.Error("Failed to delete old signatures: " + err.Error()) | ||
} else { | ||
logger.Debug("Deleted old signatures") | ||
} | ||
} |