address j4n review comments:

- don't double count in dry run
- write to an operator specified $CHATMAIL_EXPIRE_PROMPATH but don't
  document anything in official docs about it yet
- rename age_bytes into message/msg_age_bytes
This commit is contained in:
holger krekel
2026-11-24 18:18:24 +01:00
parent 64c8bde5de
commit e57fe77e8e
5 changed files with 37 additions and 20 deletions
+13 -9
View File
@@ -99,8 +99,9 @@ def scan_mailbox_messages(mbox):
return messages
def expire_to_target(mbox, target_bytes, dry=False):
def expire_to_target(mbox, target_bytes, dry=False, skip=()):
messages = scan_mailbox_messages(mbox)
messages = [m for m in messages if str(mbox / m.path) not in skip]
total_size = sum(m.quota_size for m in messages)
# Keep recent 24 hours of messages protected from expiry because
# likely something is wrong with interactions on that address
@@ -128,6 +129,7 @@ PROM_HELP = {
"storage_bytes": "Mailbox storage in bytes.",
"messages_bytes": "Sum of msg bytes >= threshold.",
"messages_count": "Number of msgs >= size threshold.",
"messages_age_bytes": "Sum of msg bytes older than N days.",
"accounts": "Number of accounts.",
"accounts_active": "Non-CI accounts active within N days.",
"expire_removed_mailboxes": "Mailboxes removed by the last run.",
@@ -157,6 +159,7 @@ class Expiry:
self.login_buckets = dict.fromkeys(self.login_days, 0)
self.msg_bytes = dict.fromkeys(self.size_thresholds, 0)
self.msg_counts = dict.fromkeys(self.size_thresholds, 0)
self.msg_age_bytes = dict.fromkeys((1, 3, 7, 14), 0)
def remove_mailbox(self, mboxdir):
if self.verbose:
@@ -227,7 +230,9 @@ class Expiry:
target_bytes = (
self.config.max_mailbox_size_mb * 1024 * 1024 * QUOTA_CLEANUP_FACTOR
)
removed = expire_to_target(Path(mbox.basedir), target_bytes, dry=self.dry)
removed = expire_to_target(
Path(mbox.basedir), target_bytes, dry=self.dry, skip=self.removed_paths
)
if removed:
changed = True
self.del_files += len(removed)
@@ -253,6 +258,9 @@ class Expiry:
if msg.size >= size:
self.msg_bytes[size] += msg.size
self.msg_counts[size] += 1
for days in self.msg_age_bytes:
cutoff = self.now - days * 86400
self.msg_age_bytes[days] += sum(m.size for m in kept if m.mtime < cutoff)
self.size_messages += sum(m.size for m in kept)
self.size_extra += sum(entry.size for entry in mbox.extrafiles)
@@ -282,6 +290,7 @@ class Expiry:
gauge("storage_bytes", storage, "kind")
gauge("messages_bytes", self.msg_bytes, "min_size")
gauge("messages_count", self.msg_counts, "min_size")
gauge("messages_age_bytes", self.msg_age_bytes, "min_days")
gauge("accounts", accounts, "kind")
gauge("accounts_active", self.login_buckets, "days")
gauge("expire_removed_mailboxes", self.del_mboxes)
@@ -325,11 +334,6 @@ def daily_expire_main(args=None):
action="store_true",
help="actually remove all expired files and dirs",
)
parser.add_argument(
"--prom",
metavar="PATH",
help="write storage and expiry statistics as Prometheus metrics to PATH",
)
args = parser.parse_args(args)
config = read_config(args.chatmail_ini)
@@ -342,8 +346,8 @@ def daily_expire_main(args=None):
for mailbox in iter_mailboxes(str(config.mailboxes_dir), maxnum=maxnum):
exp.process_mailbox_stat(mailbox)
print(exp.get_summary())
if args.prom:
exp.write_prom(args.prom)
if prompath := os.environ.get("CHATMAIL_EXPIRE_PROMPATH"):
exp.write_prom(prompath)
def quota_expire_main(args=None):
+23 -6
View File
@@ -128,21 +128,38 @@ def test_mbox_without_password(mbox1, example_config, capsys):
assert not os.path.isdir(mbox_rescan.basedir)
def test_expiry_cli_prom(example_config, mbox1, tmp_path):
def test_expiry_cli_prom(example_config, mbox1, tmp_path, monkeypatch):
days = int(example_config.delete_mails_after) + 1
create_new_messages(mbox1.basedir, ["cur/msg_old"], size=1000, days=days)
path = tmp_path / "expire.prom"
expiry_main([str(example_config._inipath), "--remove", "--prom", str(path)])
create_new_messages(mbox1.basedir, ["cur/msg_4days"], size=100, days=4)
path = tmp_path / "chatmail-expire.prom"
monkeypatch.setenv("CHATMAIL_EXPIRE_PROMPATH", str(path))
expiry_main([str(example_config._inipath), "--remove"])
text = path.read_text()
# fill_mbox leaves 500 + 600 bytes of messages after msg_old expired
assert 'chatmail_storage_bytes{kind="messages"} 1100\n' in text
assert 'chatmail_messages_count{min_size="0"} 2\n' in text
# fill_mbox 500 + 600 bytes and msg_4days remain after msg_old expired
assert 'chatmail_storage_bytes{kind="messages"} 1200\n' in text
assert 'chatmail_messages_count{min_size="0"} 3\n' in text
assert 'chatmail_messages_age_bytes{min_days="3"} 100\n' in text
assert 'chatmail_messages_age_bytes{min_days="7"} 0\n' in text
assert 'chatmail_accounts{kind="all"} 1\n' in text
# msg_old and maildirsize
assert "chatmail_expire_removed_files 2\n" in text
assert "chatmail_expire_last_run_timestamp_seconds " in text
@pytest.mark.parametrize("dry", [True, False])
def test_expiry_dry_counts_like_real_run(example_config, mbox1, dry):
mboxdir = Path(mbox1.basedir)
days = int(example_config.delete_mails_after) + 1
_create_message(mboxdir, "cur", 400 * MB, days_old=days, disk_size=10)
_create_message(mboxdir, "cur", 400 * MB, days_old=2, disk_size=10)
exp = Expiry(example_config, dry=dry, now=time.time(), verbose=False)
exp.process_mailbox_stat(MailboxStat(mboxdir))
# age pass removes the old one, quota pass the other, plus maildirsize
assert exp.del_files == 3
assert exp.size_messages == 1100
def test_expiry_cli_basic(example_config, mbox1):
mboxdir = Path(mbox1.basedir)
p1 = _create_message(mboxdir, "cur", 400 * MB, days_old=2, disk_size=10)
-2
View File
@@ -441,8 +441,6 @@ class ChatmailDeployer(Deployer):
owner="vmail",
mode="700",
)
# chatmail-expire writes its statistics there
self.ensure_directory("/var/lib/chatmail", owner="vmail")
# This file is used by auth proxy.
# https://wiki.debian.org/EtcMailName
@@ -5,5 +5,5 @@ After=network.target
[Service]
Type=oneshot
User=vmail
ExecStart={execpath} {config_path} -v --remove --prom /var/lib/chatmail/expire.prom
ExecStart={execpath} {config_path} -v --remove
-2
View File
@@ -104,8 +104,6 @@ short overview of ``chatmaild`` services:
deletes old messages, large messages, and entire mailboxes
of users who have not logged in for longer than
``delete_inactive_users_after`` days.
After each daily run it writes storage, account and expiry statistics
as Prometheus metrics to ``/var/lib/chatmail/expire.prom``.
- ``chatmail-quota-expire`` is called by Dovecot's ``quota_warning`` mechanism
and will automatically remove oldest messages to keep mailboxes well under ``max_mailbox_size``.