Skip to content

Commit

Permalink
Put watchdog loop behind __name__ == '__main__'
Browse files Browse the repository at this point in the history
I've been running runtime-introspection tools (particularly stubtest) on
docassemble's code base recently, and when you simply `import watchdog`, the
tool will get stuck in an infinite loop because the only thing in the file is
an infinite `while: True` loop.

This patch puts that behind `if __name__ == '__main__'`, so only people who
are running this module as the main module will get the desired infinite loop.
The only documented way of running this module is through `run-watchdog.sh`,
which runs `python -m docassemble.webapp.watchdog &`, so this shouldn't break
any existing functionality.
  • Loading branch information
BryceStevenWilley committed Aug 25, 2023
1 parent 2be3906 commit 047036d
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions docassemble_webapp/docassemble/webapp/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@
busy_pids = set()
index = 0

while True:
busy_now = set()
no_longer_busy = set()
for pid in psutil.pids():
try:
p = psutil.Process(pid)
if p.name() in ('apache2', 'uwsgi') and p.cpu_percent(interval=30.0) > 90.0:
busy_now.add(pid)
except:
continue
index += 1
index = index % 6
if index == 5:
for pid in busy_pids:
if pid not in busy_now:
no_longer_busy.add(pid)
for pid in no_longer_busy:
busy_pids.discard(pid)
for pid in busy_now:
if pid in busy_pids:
try:
p = psutil.Process(pid)
p.kill()
except:
pass
sys.stderr.write("Killed " + str(pid) + "\n")
if __name__ == '__main__':
while True:
busy_now = set()
no_longer_busy = set()
for pid in psutil.pids():
try:
p = psutil.Process(pid)
if p.name() in ('apache2', 'uwsgi') and p.cpu_percent(interval=30.0) > 90.0:
busy_now.add(pid)
except:
continue
index += 1
index = index % 6
if index == 5:
for pid in busy_pids:
if pid not in busy_now:
no_longer_busy.add(pid)
for pid in no_longer_busy:
busy_pids.discard(pid)
else:
busy_pids.add(pid)
time.sleep(5)
for pid in busy_now:
if pid in busy_pids:
try:
p = psutil.Process(pid)
p.kill()
except:
pass
sys.stderr.write("Killed " + str(pid) + "\n")
busy_pids.discard(pid)
else:
busy_pids.add(pid)
time.sleep(5)

0 comments on commit 047036d

Please sign in to comment.