in queue.Drop, to drop a message from the outgoing queue, not only remove it from the database, but also its contents from the file system

This commit is contained in:
Mechiel Lukkien 2023-05-22 15:03:23 +02:00
parent 88fd775ec4
commit b0623e6038
No known key found for this signature in database
2 changed files with 12 additions and 1 deletions

View file

@ -206,7 +206,7 @@ func Add(ctx context.Context, log *mlog.Log, senderAccount string, mailFrom, rcp
return err return err
} }
dst := mox.DataDirPath(filepath.Join("queue", store.MessagePath(qm.ID))) dst := qm.MessagePath()
defer func() { defer func() {
if dst != "" { if dst != "" {
err := os.Remove(dst) err := os.Remove(dst)
@ -324,10 +324,18 @@ func Drop(ctx context.Context, ID int64, toDomain string, recipient string) (int
return qm.Recipient().XString(true) == recipient return qm.Recipient().XString(true) == recipient
}) })
} }
var msgs []Msg
q.Gather(&msgs)
n, err := q.Delete() n, err := q.Delete()
if err != nil { if err != nil {
return 0, fmt.Errorf("selecting and deleting messages from queue: %v", err) return 0, fmt.Errorf("selecting and deleting messages from queue: %v", err)
} }
for _, m := range msgs {
p := m.MessagePath()
if err := os.Remove(p); err != nil {
xlog.WithContext(ctx).Errorx("removing queue message from file system", err, mlog.Field("queuemsgid", m.ID), mlog.Field("path", p))
}
}
return n, nil return n, nil
} }

View file

@ -102,6 +102,9 @@ func TestQueue(t *testing.T) {
if n != 1 { if n != 1 {
t.Fatalf("dropped %d, expected 1", n) t.Fatalf("dropped %d, expected 1", n)
} }
if _, err := os.Stat(msgs[1].MessagePath()); err == nil || !os.IsNotExist(err) {
t.Fatalf("dropped message not removed from file system")
}
next := nextWork(ctxbg, nil) next := nextWork(ctxbg, nil)
if next > 0 { if next > 0 {