@@ -24,7 +24,7 @@ import signal
2424import subprocess
2525import sys
2626import time
27- from typing import Iterable
27+ from typing import Iterable , Optional
2828
2929import yaml
3030
@@ -109,38 +109,36 @@ def start(pidfile: str, app: str, config_files: Iterable[str], daemonize: bool)
109109 return False
110110
111111
112- def stop (pidfile : str , app : str ) -> bool :
112+ def stop (pidfile : str , app : str ) -> Optional [ int ] :
113113 """Attempts to kill a synapse worker from the pidfile.
114114 Args:
115115 pidfile: path to file containing worker's pid
116116 app: name of the worker's appservice
117117
118118 Returns:
119- True if the process stopped successfully
120- False if process was already stopped or an error occured
119+ process id, or None if the process was not running
121120 """
122121
123122 if os .path .exists (pidfile ):
124123 pid = int (open (pidfile ).read ())
125124 try :
126125 os .kill (pid , signal .SIGTERM )
127126 write ("stopped %s" % (app ,), colour = GREEN )
128- return True
127+ return pid
129128 except OSError as err :
130129 if err .errno == errno .ESRCH :
131130 write ("%s not running" % (app ,), colour = YELLOW )
132131 elif err .errno == errno .EPERM :
133132 abort ("Cannot stop %s: Operation not permitted" % (app ,))
134133 else :
135134 abort ("Cannot stop %s: Unknown error" % (app ,))
136- return False
137135 else :
138136 write (
139137 "No running worker of %s found (from %s)\n The process might be managed by another controller (e.g. systemd)"
140138 % (app , pidfile ),
141139 colour = YELLOW ,
142140 )
143- return False
141+ return None
144142
145143
146144Worker = collections .namedtuple (
@@ -288,32 +286,23 @@ def main():
288286 action = options .action
289287
290288 if action == "stop" or action == "restart" :
291- has_stopped = True
289+ running_pids = []
292290 for worker in workers :
293- if not stop (worker .pidfile , worker .app ):
294- # A worker could not be stopped.
295- has_stopped = False
291+ pid = stop (worker .pidfile , worker .app )
292+ if pid is not None :
293+ running_pids . append ( pid )
296294
297295 if start_stop_synapse :
298- if not stop (pidfile , MAIN_PROCESS ):
299- has_stopped = False
300- if not has_stopped and action == "stop" :
301- sys .exit (1 )
296+ pid = stop (pidfile , MAIN_PROCESS )
297+ if pid is not None :
298+ running_pids .append (pid )
302299
303- # Wait for synapse to actually shutdown before starting it again
304- if action == "restart" :
305- running_pids = []
306- if start_stop_synapse and os .path .exists (pidfile ):
307- running_pids .append (int (open (pidfile ).read ()))
308- for worker in workers :
309- if os .path .exists (worker .pidfile ):
310- running_pids .append (int (open (worker .pidfile ).read ()))
311300 if len (running_pids ) > 0 :
312- write ("Waiting for process to exit before restarting ..." )
301+ write ("Waiting for processes to exit..." )
313302 for running_pid in running_pids :
314303 while pid_running (running_pid ):
315304 time .sleep (0.2 )
316- write ("All processes exited; now restarting... " )
305+ write ("All processes exited" )
317306
318307 if action == "start" or action == "restart" :
319308 error = False
0 commit comments