Skip to content

Commit

Permalink
[windows] record restarts on a specific timeframe
Browse files Browse the repository at this point in the history
Drop the global count of restarts in favor of a timeframe based one.
  • Loading branch information
yannmh committed Jun 9, 2015
1 parent 6eb1390 commit 203e6ea
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions win32/agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# stdlib
from collections import deque
import logging
import modules
import multiprocessing
Expand Down Expand Up @@ -127,10 +128,15 @@ class ProcessWatchDog(object):
Monitor the attached process.
Restarts when it exits until the limit set is reached.
"""
_RESTART_TIMEFRAME = 3600

def __init__(self, name, process, max_restarts=5):
"""
:param max_restarts: maximum number of restarts per _RESTART_TIMEFRAME timeframe.
"""
self._name = name
self._process = process
self._count_restarts = 0
self._restarts = deque([])
self._MAX_RESTARTS = max_restarts

def start(self):
Expand All @@ -145,14 +151,25 @@ def is_alive(self):
def is_enabled(self):
return self._process.is_enabled

def _can_restart(self):
now = time.time()
while(self._restarts and self._restarts[0] < now - self._RESTART_TIMEFRAME):
self._restarts.popleft()

return len(self._restarts) < self._MAX_RESTARTS

def restart(self):
self._count_restarts += 1
if self._count_restarts >= self._MAX_RESTARTS:
if not self._can_restart():
servicemanager.LogInfoMsg(
"%s reached the limit of restarts. Not restarting..." % self._name)
"{0} reached the limit of restarts ({1} tries during the last {2}s"
" (max authorized: {3})). Not restarting..."
.format(self._name, len(self._restarts),
self._RESTART_TIMEFRAME, self._MAX_RESTARTS)
)
self._process.is_enabled = False
return

self._restarts.append(time.time())
# Make a new proc instances because multiprocessing
# won't let you call .start() twice on the same instance.
if self._process.is_alive():
Expand Down

0 comments on commit 203e6ea

Please sign in to comment.