diff --git a/DMS/Dockerfile b/DMS/Dockerfile index bcbd697..8b722ec 100644 --- a/DMS/Dockerfile +++ b/DMS/Dockerfile @@ -1,13 +1,28 @@ FROM docker.io/mailserver/docker-mailserver:latest 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 && \ apt-get install -y --no-install-recommends \ python3 \ python3-pip \ - && pip3 install --break-system-packages --no-cache-dir boto3 \ + && pip3 install --break-system-packages --no-cache-dir boto3 croniter \ && apt-get clean \ - && rm -rf /var/lib/apt/lists/* \ No newline at end of file + && 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 \ No newline at end of file diff --git a/DMS/sieve-cron b/DMS/sieve-cron deleted file mode 100644 index 744017d..0000000 --- a/DMS/sieve-cron +++ /dev/null @@ -1 +0,0 @@ -*/5 * * * * root python3 /scripts/sync.py >> /var/log/sieve-sync.log 2>&1 diff --git a/DMS/sieve-schedule b/DMS/sieve-schedule new file mode 100644 index 0000000..410a87c --- /dev/null +++ b/DMS/sieve-schedule @@ -0,0 +1 @@ +*/5 * * * * \ No newline at end of file diff --git a/DMS/sieve-supervisor.conf b/DMS/sieve-supervisor.conf new file mode 100644 index 0000000..cf81c82 --- /dev/null +++ b/DMS/sieve-supervisor.conf @@ -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 " +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! \ No newline at end of file diff --git a/DMS/sync_dynamodb_to_sieve.py b/DMS/sync_dynamodb_to_sieve.py index f2bb3d6..6330db3 100644 --- a/DMS/sync_dynamodb_to_sieve.py +++ b/DMS/sync_dynamodb_to_sieve.py @@ -6,6 +6,13 @@ import boto3 import os from pathlib import Path import json +import time +from datetime import datetime +try: + from croniter import croniter +except ImportError: + print("Bitte 'croniter' via pip installieren!") + exit(1) # Config REGION = 'us-east-2' @@ -102,4 +109,44 @@ def sync(): print(f'✓ {email}') if __name__ == '__main__': - sync() \ No newline at end of file + # 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() + + # 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 \ No newline at end of file