Skip to content

Commit

Permalink
supervisor now clean zombie killer processes that were left without a…
Browse files Browse the repository at this point in the history
…nyone to call for wait on them
  • Loading branch information
Gal Ben David committed Jan 17, 2021
1 parent 18f1250 commit 229c891
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions sergeant/supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def __init__(
self.concurrent_workers = concurrent_workers
self.max_worker_memory_usage = max_worker_memory_usage

self.supevisor_process = psutil.Process()

self.extra_signature: typing.Dict[str, typing.Any] = {
'supervisor': {
'worker_module_name': self.worker_module_name,
Expand Down Expand Up @@ -154,6 +156,8 @@ def supervise_loop(
worker=worker,
)

self.clean_zombies()

time.sleep(0.5)

self.logger.info(
Expand Down Expand Up @@ -288,6 +292,7 @@ def respawn_a_worker(
worker: SupervisedWorker,
) -> None:
worker.kill()

self.current_workers.remove(worker)
new_worker = SupervisedWorker(
worker_module_name=self.worker_module_name,
Expand All @@ -304,12 +309,31 @@ def stop_a_worker(
worker: SupervisedWorker,
) -> None:
worker.kill()

self.current_workers.remove(worker)
self.logger.info(
msg=f'worker has stopped: {worker.process.pid}',
extra=self.extra_signature,
)

def clean_zombies(
self,
):
supervisor_zombie_children = [
child_process
for child_process in self.supevisor_process.children()
if child_process.status() == psutil.STATUS_ZOMBIE
]
if supervisor_zombie_children:
self.logger.info(
msg=f'cleaning {len(supervisor_zombie_children)} zombies',
extra=self.extra_signature,
)
psutil.wait_procs(
procs=supervisor_zombie_children,
timeout=1.0,
)


def main() -> None:
parser = argparse.ArgumentParser(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setuptools.setup(
name='sergeant',
version='0.18.1',
version='0.18.2',
author='Gal Ben David',
author_email='[email protected]',
url='https://github.com/Intsights/sergeant',
Expand Down

0 comments on commit 229c891

Please sign in to comment.