fix: PID-Lock verhindert doppelte Bot-Instanzen
This commit is contained in:
parent
9cd3aebbaa
commit
5fa57d5ecf
1 changed files with 34 additions and 0 deletions
|
|
@ -4,9 +4,39 @@ import asyncio
|
|||
import logging
|
||||
import sys
|
||||
import os
|
||||
import fcntl
|
||||
import atexit
|
||||
import signal
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
PIDFILE = "/tmp/hausmeister-bot.pid"
|
||||
_lock_fp = None
|
||||
|
||||
|
||||
def _acquire_lock():
|
||||
"""Stellt sicher, dass nur eine Bot-Instanz läuft (PID-File + flock)."""
|
||||
global _lock_fp
|
||||
_lock_fp = open(PIDFILE, "w")
|
||||
try:
|
||||
fcntl.flock(_lock_fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except OSError:
|
||||
print(f"ABBRUCH: Bot läuft bereits (PID-File {PIDFILE} ist gelockt)", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
_lock_fp.write(str(os.getpid()))
|
||||
_lock_fp.flush()
|
||||
|
||||
|
||||
def _release_lock():
|
||||
global _lock_fp
|
||||
if _lock_fp:
|
||||
try:
|
||||
fcntl.flock(_lock_fp, fcntl.LOCK_UN)
|
||||
_lock_fp.close()
|
||||
os.unlink(PIDFILE)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
from telegram import BotCommand, Update, ReplyKeyboardMarkup, KeyboardButton
|
||||
from telegram.ext import (
|
||||
Application, CommandHandler, MessageHandler, filters, ContextTypes,
|
||||
|
|
@ -291,6 +321,10 @@ def main():
|
|||
log.error("TG_HAUSMEISTER_TOKEN fehlt in homelab.conf!")
|
||||
sys.exit(1)
|
||||
|
||||
_acquire_lock()
|
||||
atexit.register(_release_lock)
|
||||
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))
|
||||
|
||||
log.info("Starte Orbitalo Hausmeister-Bot...")
|
||||
app = Application.builder().token(token).build()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue