From 17d2bf2a3e0b5431aabc24c23cf002f904cd7de0 Mon Sep 17 00:00:00 2001 From: allegroai <> Date: Sat, 11 Jul 2020 01:43:54 +0300 Subject: [PATCH] Change daemon --stop without any specific flag to terminate the agents by worker id lexicographic order --- trains_agent/commands/worker.py | 7 ++++--- trains_agent/helper/process.py | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/trains_agent/commands/worker.py b/trains_agent/commands/worker.py index ff54c65..fd987c2 100644 --- a/trains_agent/commands/worker.py +++ b/trains_agent/commands/worker.py @@ -707,9 +707,10 @@ class Worker(ServiceCommandSection): kwargs = self._verify_command_states(kwargs) docker = docker or kwargs.get('docker') + # We are not running a daemon we are killing one. + # find the pid send termination signal and leave if kwargs.get('stop', False): - self._kill_daemon() - return + return 1 if not self._kill_daemon() else 0 # make sure we only have a single instance, # also make sure we set worker_id properly and cache folders @@ -2355,7 +2356,7 @@ class Worker(ServiceCommandSection): def _kill_daemon(self): worker_id, worker_name = self._generate_worker_id_name() # Iterate over all running process - for pid, uid, slot, file in Singleton.get_running_pids(): + for pid, uid, slot, file in sorted(Singleton.get_running_pids(), key=lambda x: x[1] or ''): # wither we have a match for the worker_id or we just pick the first one if pid >= 0 and ( (worker_id and uid == worker_id) or diff --git a/trains_agent/helper/process.py b/trains_agent/helper/process.py index c5b364b..05cda8a 100644 --- a/trains_agent/helper/process.py +++ b/trains_agent/helper/process.py @@ -65,6 +65,7 @@ def terminate_process(pid, timeout=10.): except Exception: return True + def kill_all_child_processes(pid=None): # get current process if pid not provided include_parent = True