1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-22 03:57:02 +00:00

Basic retention scanner w/ unit tests

Not wired into anything yet!
This commit is contained in:
James Hillyerd
2012-10-25 22:15:53 -07:00
parent b665612190
commit d8d0d1b4ff
3 changed files with 178 additions and 110 deletions

View File

@@ -1,12 +1,37 @@
package smtpd
import (
//"github.com/jhillyerd/inbucket/config"
//"github.com/jhillyerd/inbucket/log"
// "io/ioutil"
"github.com/jhillyerd/inbucket/log"
"time"
)
// retentionScan does a single pass of all mailboxes looking for messages that can be purged
func retentionScan(ds *DataStore) {
func retentionScan(ds DataStore, maxAge time.Duration, sleep time.Duration) error {
log.Trace("Starting retention scan")
cutoff := time.Now().Add(-1 * maxAge)
mboxes, err := ds.AllMailboxes()
if err != nil {
return err
}
for _, mb := range mboxes {
messages, err := mb.GetMessages()
if err != nil {
return err
}
for _, msg := range messages {
if msg.Date().Before(cutoff) {
log.Trace("Purging expired message %v", msg.Id())
err = msg.Delete()
if err != nil {
// Log but don't abort
log.Error("Failed to purge message %v: %v", msg.Id(), err)
}
}
}
// Sleep after completing a mailbox
time.Sleep(sleep)
}
return nil
}