Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put watchdog loop behind __name__ == '__main__' #680

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)