Skip to content

Commit

Permalink
Merge pull request #53 from freifunkMUC/automatic_cleanup
Browse files Browse the repository at this point in the history
Automatic cleanup of stale peers
  • Loading branch information
awlx authored Sep 7, 2021
2 parents 5001f80 + 095189e commit e581195
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions wgkex/worker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import wgkex.config.config as config
from wgkex.worker import mqtt
from wgkex.worker.netlink import wg_flush_stale_peers
import threading
import time


class Error(Exception):
Expand All @@ -12,6 +15,13 @@ class DomainsNotInConfig(Error):
"""If no domains exist in configuration file."""


def clean_up_worker(domain: str) -> None:
while True:
time.sleep(300)
print(f"Running cleanup task for {domain}")
wg_flush_stale_peers(domain)


def main():
"""Starts MQTT listener.
Expand All @@ -21,6 +31,14 @@ def main():
domains = config.load_config().get("domains")
if not domains:
raise DomainsNotInConfig("Could not locate domains in configuration.")
clean_up_threads = []
for domain in domains:
print(f"Scheduling cleanup task for {domain}")
thread = threading.Thread(
target=clean_up_worker, args=(domain.split("ffmuc_")[1],)
)
thread.start()
clean_up_threads.append(thread)
mqtt.connect(domains)


Expand Down

0 comments on commit e581195

Please sign in to comment.