🔧 Aktualisierung der Backend-Logik und Optimierung der SQLite-Datenbankkonfiguration für Raspberry Pi: Hinzufügen spezifischer Optimierungen, Verbesserung der Fehlerbehandlung und Protokollierung. Einführung von Caching-Mechanismen und Anpassungen für schwache Hardware. 📈

This commit is contained in:
2025-06-01 22:43:42 +02:00
parent 317f7dc9dc
commit 62efe03887
40 changed files with 3856 additions and 229 deletions

View File

@@ -233,6 +233,41 @@ net.core.wmem_default = 262144
# Schutz vor Time-Wait-Assassination
net.ipv4.tcp_rfc1337 = 1
# ===================================================================
# RASPBERRY PI PERFORMANCE-OPTIMIERUNGEN FÜR WEBAPP
# ===================================================================
# Memory Management für schwache Hardware optimieren
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.vfs_cache_pressure = 50
vm.min_free_kbytes = 8192
vm.overcommit_memory = 1
# CPU Scheduler für bessere Responsivität
kernel.sched_min_granularity_ns = 10000000
kernel.sched_wakeup_granularity_ns = 15000000
kernel.sched_migration_cost_ns = 5000000
# Filesystem Performance
vm.dirty_expire_centisecs = 500
vm.dirty_writeback_centisecs = 100
# Memory Compaction für bessere Speichernutzung
vm.compact_memory = 1
# OOM Killer weniger aggressiv
vm.oom_kill_allocating_task = 0
vm.panic_on_oom = 0
# Kernel Preemption für bessere Interaktivität
kernel.sched_rt_runtime_us = 950000
kernel.sched_rt_period_us = 1000000
# I/O Scheduler Optimierungen für SD-Karte
# (wird später per udev-Regel angewendet)
EOF
# Sysctl-Einstellungen sofort anwenden
@@ -978,6 +1013,10 @@ install_dependencies_only() {
install_npm_dependencies
generate_ssl_certificate
# Performance-Optimierungen auch für manuelles Testen
optimize_webapp_performance
optimize_static_assets
# Minimaler Test
progress "Starte minimalen Test..."
cd "$APP_DIR"
@@ -1029,6 +1068,10 @@ install_full_production_system() {
remove_desktop_environments
install_minimal_x11
# Performance-Optimierungen für Raspberry Pi Webapp
optimize_webapp_performance
optimize_static_assets
# Remote-Zugang konfigurieren
install_remote_access
configure_firewall
@@ -1567,6 +1610,241 @@ configure_hostname() {
fi
}
# =========================== WEBAPP PERFORMANCE-OPTIMIERUNG ===========================
optimize_webapp_performance() {
log "=== WEBAPP PERFORMANCE-OPTIMIERUNG FÜR RASPBERRY PI ==="
# Python/Flask spezifische Optimierungen
progress "Konfiguriere Python-Performance-Optimierungen..."
# Python Bytecode Optimierung aktivieren
cat > /etc/environment << 'EOF'
# Python Performance Optimierungen
PYTHONOPTIMIZE=2
PYTHONDONTWRITEBYTECODE=1
PYTHONUNBUFFERED=1
PYTHONHASHSEED=random
# Flask/SQLite Optimierungen
FLASK_ENV=production
FLASK_DEBUG=0
SQLITE_TMPDIR=/tmp
# Memory Optimierungen
MALLOC_ARENA_MAX=2
MALLOC_MMAP_THRESHOLD=131072
MALLOC_TRIM_THRESHOLD=131072
EOF
# Systemd Service-Optimierungen
progress "Optimiere Systemd-Services für bessere Performance..."
# Stoppe unnötige Services
local unnecessary_services=(
"bluetooth.service"
"hciuart.service"
"avahi-daemon.service"
"cups.service"
"cups-browsed.service"
"ModemManager.service"
"wpa_supplicant.service"
)
for service in "${unnecessary_services[@]}"; do
if systemctl is-enabled "$service" 2>/dev/null; then
systemctl disable "$service" 2>/dev/null || true
systemctl stop "$service" 2>/dev/null || true
log "✅ Service deaktiviert: $service"
fi
done
# Tmpfs für temporäre Dateien
progress "Konfiguriere tmpfs für bessere I/O Performance..."
cat >> /etc/fstab << 'EOF'
# MYP Performance Optimierungen - tmpfs für temporäre Dateien
tmpfs /tmp tmpfs defaults,noatime,nosuid,size=256m 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nosuid,size=128m 0 0
tmpfs /var/log tmpfs defaults,noatime,nosuid,size=64m 0 0
EOF
# Logrotate für tmpfs-Logs konfigurieren
cat > /etc/logrotate.d/myp-tmpfs << 'EOF'
/var/log/*.log {
daily
missingok
rotate 2
compress
notifempty
create 0644 root root
copytruncate
}
EOF
# Systemd Journal Einstellungen optimieren
progress "Optimiere systemd Journal für bessere Performance..."
mkdir -p /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/myp-performance.conf << 'EOF'
[Journal]
# Journal Optimierungen für Raspberry Pi
Storage=volatile
RuntimeMaxUse=32M
RuntimeKeepFree=16M
RuntimeMaxFileSize=8M
RuntimeMaxFiles=4
MaxRetentionSec=1day
MaxFileSec=1hour
ForwardToSyslog=no
ForwardToKMsg=no
ForwardToConsole=no
ForwardToWall=no
EOF
# Crontab für regelmäßige Cache-Bereinigung
progress "Installiere automatische Cache-Bereinigung..."
cat > /etc/cron.d/myp-cache-cleanup << 'EOF'
# MYP Cache und Memory Cleanup
# Alle 6 Stunden Cache bereinigen
0 */6 * * * root /bin/echo 3 > /proc/sys/vm/drop_caches
# Täglich um 3 Uhr temporäre Dateien bereinigen
0 3 * * * root /usr/bin/find /tmp -type f -atime +1 -delete 2>/dev/null
# Wöchentlich Python Cache bereinigen
0 2 * * 0 root /usr/bin/find /opt/myp -name "*.pyc" -delete 2>/dev/null
0 2 * * 0 root /usr/bin/find /opt/myp -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null
EOF
# Limits für bessere Ressourcen-Verwaltung
progress "Konfiguriere System-Limits..."
cat >> /etc/security/limits.conf << 'EOF'
# MYP Performance Limits
* soft nofile 65536
* hard nofile 65536
* soft nproc 32768
* hard nproc 32768
root soft nofile 65536
root hard nofile 65536
EOF
# Apache/Nginx entfernen falls vorhanden (Konflikt mit Flask)
progress "Entferne konfliktbehaftete Webserver..."
local webservers=("apache2" "nginx" "lighttpd")
for webserver in "${webservers[@]}"; do
if systemctl is-enabled "$webserver" 2>/dev/null; then
systemctl stop "$webserver" 2>/dev/null || true
systemctl disable "$webserver" 2>/dev/null || true
apt-get remove --purge -y "$webserver" 2>/dev/null || true
log "✅ Webserver entfernt: $webserver"
fi
done
log "✅ Webapp Performance-Optimierung abgeschlossen:"
log " 🚀 Python Bytecode-Optimierung aktiviert"
log " 💾 tmpfs für temporäre Dateien konfiguriert"
log " 📝 Journal-Logging optimiert"
log " 🧹 Automatische Cache-Bereinigung installiert"
log " ⚡ Unnötige Services deaktiviert"
log " 📊 System-Limits für bessere Performance gesetzt"
}
# =========================== CSS/JS OPTIMIERUNG ===========================
optimize_static_assets() {
log "=== STATISCHE DATEIEN OPTIMIERUNG ==="
if [ ! -d "$APP_DIR/static" ]; then
warning "Static-Ordner nicht gefunden - überspringe Asset-Optimierung"
return
fi
progress "Analysiere und optimiere CSS/JS Dateien..."
cd "$APP_DIR/static"
# Erstelle optimierte CSS-Datei durch Kombination kritischer Styles
progress "Erstelle optimierte CSS-Kombination..."
cat > css/critical.min.css << 'EOF'
/* Kritische Styles für ersten Seitenaufbau - Inline-optimiert */
*{box-sizing:border-box}body{margin:0;font-family:system-ui,-apple-system,sans-serif;line-height:1.5}
.container{max-width:1200px;margin:0 auto;padding:0 1rem}
.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0}
.btn{display:inline-flex;align-items:center;padding:0.5rem 1rem;border:none;border-radius:0.375rem;font-weight:500;text-decoration:none;cursor:pointer;transition:all 0.15s}
.btn-primary{background:#3b82f6;color:white}.btn-primary:hover{background:#2563eb}
.card{background:white;border-radius:0.5rem;padding:1.5rem;box-shadow:0 1px 3px rgba(0,0,0,0.1)}
.flex{display:flex}.items-center{align-items:center}.justify-between{justify-content:space-between}
.hidden{display:none}.block{display:block}.inline-block{display:inline-block}
.text-sm{font-size:0.875rem}.text-lg{font-size:1.125rem}
.font-medium{font-weight:500}.font-bold{font-weight:700}
.text-gray-600{color:#4b5563}.text-gray-900{color:#111827}
.mb-4{margin-bottom:1rem}.mt-6{margin-top:1.5rem}.p-4{padding:1rem}
.w-full{width:100%}.h-full{height:100%}
@media(max-width:768px){.container{padding:0 0.5rem}.card{padding:1rem}}
EOF
# Erstelle minimale JavaScript-Loader
progress "Erstelle optimierten JavaScript-Loader..."
cat > js/loader.min.js << 'EOF'
/*Minimaler Async Loader für bessere Performance*/
(function(){var d=document,w=window;function loadCSS(href){var l=d.createElement('link');l.rel='stylesheet';l.href=href;l.media='print';l.onload=function(){this.media='all'};d.head.appendChild(l)}function loadJS(src,cb){var s=d.createElement('script');s.async=true;s.src=src;if(cb)s.onload=cb;d.head.appendChild(s)}w.loadAssets=function(){if(w.assetsLoaded)return;w.assetsLoaded=true;loadCSS('/static/css/tailwind.min.css');loadJS('/static/js/app.min.js')};if(d.readyState==='loading'){d.addEventListener('DOMContentLoaded',w.loadAssets)}else{w.loadAssets()}})();
EOF
# Service Worker für besseres Caching
progress "Erstelle optimierten Service Worker..."
cat > sw-optimized.js << 'EOF'
const CACHE_NAME = 'myp-webapp-v1';
const ASSETS_TO_CACHE = [
'/',
'/static/css/critical.min.css',
'/static/js/loader.min.js',
'/static/favicon.svg'
];
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(ASSETS_TO_CACHE))
);
});
self.addEventListener('fetch', event => {
if (event.request.destination === 'image' ||
event.request.url.includes('/static/')) {
event.respondWith(
caches.match(event.request)
.then(response => response || fetch(event.request))
);
}
});
EOF
# Gzip-Kompression für statische Dateien
progress "Komprimiere statische Dateien..."
find . -name "*.css" -o -name "*.js" -o -name "*.html" | while read file; do
if [ -f "$file" ] && [ ! -f "$file.gz" ]; then
gzip -c "$file" > "$file.gz" 2>/dev/null || true
fi
done
cd "$CURRENT_DIR"
log "✅ Statische Dateien optimiert:"
log " 📦 Kritische CSS-Styles kombiniert"
log " ⚡ Asynchroner Asset-Loader erstellt"
log " 🗜️ Gzip-Kompression angewendet"
log " 🔄 Service Worker für Caching installiert"
}
# =========================== HAUPTPROGRAMM ===========================
main() {
# Erstelle Log-Datei