neuer cron

This commit is contained in:
Andreas Knuth 2026-01-27 21:05:16 -06:00
parent 90e294de82
commit 1d5e24f541
5 changed files with 80 additions and 6 deletions

View File

@ -1,13 +1,28 @@
FROM docker.io/mailserver/docker-mailserver:latest FROM docker.io/mailserver/docker-mailserver:latest
LABEL maintainer="andreas.knuth@bayarea-cc.com" LABEL maintainer="andreas.knuth@bayarea-cc.com"
LABEL description="Custom DMS with Python3 support" LABEL description="Custom DMS with Python3 support and Sieve Sync"
# Install Python and boto3 # 1. Python, pip und dependencies installieren
# croniter hinzufügen!
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
python3 \ python3 \
python3-pip \ python3-pip \
&& pip3 install --break-system-packages --no-cache-dir boto3 \ && pip3 install --break-system-packages --no-cache-dir boto3 croniter \
&& apt-get clean \ && apt-get clean \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# 2. Verzeichnis erstellen
WORKDIR /scripts
# 3. Script kopieren
COPY sync_dynamodb_to_sieve.py /scripts/sync.py
RUN chmod +x /scripts/sync.py
# 4. Schedule Konfiguration kopieren (Der Cron-String)
COPY sieve-schedule /etc/sieve-schedule
# 5. Supervisor Konfiguration kopieren
# DMS scannt diesen Ordner beim Start
COPY sieve-supervisor.conf /etc/supervisor/conf.d/sieve-sync.conf

View File

@ -1 +0,0 @@
*/5 * * * * root python3 /scripts/sync.py >> /var/log/sieve-sync.log 2>&1

1
DMS/sieve-schedule Normal file
View File

@ -0,0 +1 @@
*/5 * * * *

12
DMS/sieve-supervisor.conf Normal file
View File

@ -0,0 +1,12 @@
[program:sieve-sync]
command=/usr/bin/python3 /scripts/sync.py
# Wichtig: stdout_logfile schickt die Logs an den Docker Log Stream
# Damit sehen Sie die Logs via "docker logs <container>"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autostart=true
autorestart=true
startsecs=5
# Hier werden die ENV Vars des Containers automatisch an das Script vererbt!

View File

@ -6,6 +6,13 @@ import boto3
import os import os
from pathlib import Path from pathlib import Path
import json import json
import time
from datetime import datetime
try:
from croniter import croniter
except ImportError:
print("Bitte 'croniter' via pip installieren!")
exit(1)
# Config # Config
REGION = 'us-east-2' REGION = 'us-east-2'
@ -102,4 +109,44 @@ def sync():
print(f'{email}') print(f'{email}')
if __name__ == '__main__': if __name__ == '__main__':
# Pfad zur Cron-Definition (nur der String, z.B. "*/5 * * * *")
CRON_FILE = '/etc/sieve-schedule'
# Fallback, falls Datei fehlt
cron_string = "*/5 * * * *"
if os.path.exists(CRON_FILE):
with open(CRON_FILE, 'r') as f:
# Kommentare entfernen und String holen
content = f.read().strip()
if content and not content.startswith('#'):
cron_string = content
print(f"DynamoDB Sieve Sync gestartet. Zeitplan: {cron_string}")
print(f"AWS Region: {os.environ.get('AWS_DEFAULT_REGION', 'nicht gesetzt')}") # Debug Check
# Initialer Lauf beim Start? (Optional, hier auskommentiert)
sync() sync()
# Iterator erstellen
base_time = datetime.now()
iter = croniter(cron_string, base_time)
while True:
# Den nächsten Zeitpunkt berechnen
next_run = iter.get_next(datetime)
now = datetime.now()
sleep_seconds = (next_run - now).total_seconds()
if sleep_seconds > 0:
# Warten bis zum nächsten Slot
time.sleep(sleep_seconds)
try:
print(f"[{datetime.now()}] Starte Sync...")
sync()
except Exception as e:
print(f"Fehler beim Sync: {e}")
# Wichtig: Bei Fehler nicht abstürzen, sondern weitermachen
pass