Skip to content

Commit

Permalink
Merge pull request #680 from BryceStevenWilley/watchdog_main
Browse files Browse the repository at this point in the history
Put watchdog loop behind `__name__ == '__main__'`
  • Loading branch information
jhpyle authored Sep 9, 2023
2 parents bb8dc94 + 047036d commit a14dd6d
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 a14dd6d

Please sign in to comment.