44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import os, signal, subprocess, time, sys
|
|
import logging
|
|
|
|
INTERVAL = int(os.getenv("FETCH_INTERVAL_SECONDS", "120"))
|
|
GETMAILDIR = "/var/getmail"
|
|
CONFIG_DIR = "/getmail"
|
|
JOBS = [f for f in os.listdir(CONFIG_DIR) if f.startswith("getmailrc_")]
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="[%(asctime)s] %(levelname)s %(message)s",
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
)
|
|
|
|
stop = False
|
|
def handle(sig, frame):
|
|
global stop
|
|
logging.info(f"Received signal {sig}, stopping runner...")
|
|
stop = True
|
|
|
|
for s in (signal.SIGINT, signal.SIGTERM):
|
|
signal.signal(s, handle)
|
|
|
|
def run_once():
|
|
args = ["getmail", "--getmaildir", GETMAILDIR]
|
|
for job in JOBS:
|
|
args += ["-r", f"{CONFIG_DIR}/{job}"]
|
|
logging.info(f"Running getmail with jobs: {JOBS}")
|
|
try:
|
|
rc = subprocess.call(args, stdout=sys.stdout, stderr=sys.stderr)
|
|
logging.info(f"getmail exited with code {rc}")
|
|
return rc
|
|
except Exception as e:
|
|
logging.error(f"Error running getmail: {e}")
|
|
return 1
|
|
|
|
logging.info("Starting getmail runner...")
|
|
while not stop:
|
|
rc = run_once()
|
|
for _ in range(INTERVAL):
|
|
if stop: break
|
|
time.sleep(1)
|
|
logging.info("Exiting getmail runner.")
|
|
sys.exit(0) |