From 4a4da06bf5323acfcd8a7035c2c7411107093f5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Bouy=C3=A9?= Date: Fri, 27 Dec 2019 14:51:45 +0000 Subject: [PATCH 1/7] Update synctl: On restart don't start if nothing was stopped. Warns that the process might not be started by synctl (#5166) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Romain Bouyé --- synctl | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/synctl b/synctl index 45acece30bde..22c0ce7cbaa5 100755 --- a/synctl +++ b/synctl @@ -132,12 +132,23 @@ def start_worker(app: str, configfile: str, worker_configfile: str) -> bool: return False -def stop(pidfile, app): +def stop(pidfile, app) -> bool: + """Attempts to kill a synapse worker from the pidfile. + Args: + pidfile: path to file containing worker's pid + app: name of the worker's appservice + + Returns: + True if the process stopped successfully + False if process was stopped or an error occured + """ + if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, signal.SIGTERM) write("stopped %s" % (app,), colour=GREEN) + return True except OSError as err: if err.errno == errno.ESRCH: write("%s not running" % (app,), colour=YELLOW) @@ -145,6 +156,14 @@ def stop(pidfile, app): abort("Cannot stop %s: Operation not permitted" % (app,)) else: abort("Cannot stop %s: Unknown error" % (app,)) + return False + else: + write( + "No running worker found. (from '%s')\nThe process might be managed by another controller (e.g. systemd)" + % pidfile, + colour=YELLOW, + ) + return False Worker = collections.namedtuple( @@ -298,11 +317,16 @@ def main(): action = options.action if action == "stop" or action == "restart": + hasStopped = False for worker in workers: - stop(worker.pidfile, worker.app) + if stop(worker.pidfile, worker.app): + hasStopped = True if start_stop_synapse: - stop(pidfile, "synapse.app.homeserver") + if stop(pidfile, "synapse.app.homeserver"): + hasStopped = True + if not hasStopped: + sys.exit(1) # Wait for synapse to actually shutdown before starting it again if action == "restart": From 50b6aa3f9714d0e6471cd67d7c7668b4eab38412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Bouy=C3=A9?= Date: Fri, 27 Dec 2019 16:17:33 +0000 Subject: [PATCH 2/7] Add changelog newsfile for #5166 --- changelog.d/6590.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/6590.misc diff --git a/changelog.d/6590.misc b/changelog.d/6590.misc new file mode 100644 index 000000000000..7b82f32577f1 --- /dev/null +++ b/changelog.d/6590.misc @@ -0,0 +1 @@ +synctl: On restart don't start if nothing was stopped. Warns that the process might not be started by synctl \ No newline at end of file From cc16e505c888cf8875ed509facf93bb4be5292f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Bouy=C3=A9?= Date: Fri, 27 Dec 2019 22:11:17 +0000 Subject: [PATCH 3/7] Missing '.' in news fragment --- changelog.d/6590.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/6590.misc b/changelog.d/6590.misc index 7b82f32577f1..c04e5cd174f0 100644 --- a/changelog.d/6590.misc +++ b/changelog.d/6590.misc @@ -1 +1 @@ -synctl: On restart don't start if nothing was stopped. Warns that the process might not be started by synctl \ No newline at end of file +synctl: On restart don't start if nothing was stopped. Warns that the process might not be started by synctl. \ No newline at end of file From 17df9ea060393d97bbd418a867db7308da532c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Bouy=C3=A9?= Date: Sun, 5 Apr 2020 16:54:02 +0100 Subject: [PATCH 4/7] Update synctl: Don't restart if all workers aren't stopped. Plus Clarify comment and add missing type hint --- synctl | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/synctl b/synctl index 22c0ce7cbaa5..a1b0b1018954 100755 --- a/synctl +++ b/synctl @@ -132,7 +132,7 @@ def start_worker(app: str, configfile: str, worker_configfile: str) -> bool: return False -def stop(pidfile, app) -> bool: +def stop(pidfile: str, app: str) -> bool: """Attempts to kill a synapse worker from the pidfile. Args: pidfile: path to file containing worker's pid @@ -140,7 +140,7 @@ def stop(pidfile, app) -> bool: Returns: True if the process stopped successfully - False if process was stopped or an error occured + False if process was already stopped or an error occured """ if os.path.exists(pidfile): @@ -159,8 +159,8 @@ def stop(pidfile, app) -> bool: return False else: write( - "No running worker found. (from '%s')\nThe process might be managed by another controller (e.g. systemd)" - % pidfile, + "No running worker of %s found (from %s)\nThe process might be managed by another controller (e.g. systemd)" + % (app, pidfile), colour=YELLOW, ) return False @@ -317,15 +317,21 @@ def main(): action = options.action if action == "stop" or action == "restart": - hasStopped = False - for worker in workers: - if stop(worker.pidfile, worker.app): - hasStopped = True + has_stopped = False + + if len(workers) > 0: + stopped_worker_count = 0 + for worker in workers: + if stop(worker.pidfile, worker.app): + stopped_worker_count += 1 + # If all the workers were stopped + if len(workers) == stopped_worker_count: + has_stopped = True if start_stop_synapse: if stop(pidfile, "synapse.app.homeserver"): - hasStopped = True - if not hasStopped: + has_stopped = True + if not has_stopped: sys.exit(1) # Wait for synapse to actually shutdown before starting it again From e568a9bddc4a4722d0f0da6044b8c2e0c1cb8480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Bouy=C3=A9?= Date: Wed, 8 Apr 2020 18:30:51 +0100 Subject: [PATCH 5/7] Fix style error: blank line containing whitespaces --- synctl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synctl b/synctl index a1b0b1018954..a4627ebca057 100755 --- a/synctl +++ b/synctl @@ -318,7 +318,7 @@ def main(): if action == "stop" or action == "restart": has_stopped = False - + if len(workers) > 0: stopped_worker_count = 0 for worker in workers: From 02ae287ba2646bedb29ebb24c168d7ad2666fe0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Bouy=C3=A9?= Date: Mon, 18 May 2020 14:30:03 +0100 Subject: [PATCH 6/7] Update synctl: simplify logic to stop the workers --- synctl | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/synctl b/synctl index a4627ebca057..efaa4273af39 100755 --- a/synctl +++ b/synctl @@ -317,20 +317,15 @@ def main(): action = options.action if action == "stop" or action == "restart": - has_stopped = False - - if len(workers) > 0: - stopped_worker_count = 0 - for worker in workers: - if stop(worker.pidfile, worker.app): - stopped_worker_count += 1 - # If all the workers were stopped - if len(workers) == stopped_worker_count: - has_stopped = True + has_stopped = True + for worker in workers: + if not stop(worker.pidfile, worker.app): + # A worker doesn't stop + has_stopped = False if start_stop_synapse: - if stop(pidfile, "synapse.app.homeserver"): - has_stopped = True + if not stop(pidfile, "synapse.app.homeserver"): + has_stopped = False if not has_stopped: sys.exit(1) From c0e3b33fbfeff75b8e81592588bee081d596116a Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 19 May 2020 08:15:43 -0400 Subject: [PATCH 7/7] Fix some wording. --- changelog.d/6590.misc | 2 +- synctl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog.d/6590.misc b/changelog.d/6590.misc index c04e5cd174f0..05a0156212f1 100644 --- a/changelog.d/6590.misc +++ b/changelog.d/6590.misc @@ -1 +1 @@ -synctl: On restart don't start if nothing was stopped. Warns that the process might not be started by synctl. \ No newline at end of file +`synctl` now warns if it was unable to stop Synapse and will not attempt to start Synapse if nothing was stopped. Contributed by Romain Bouyé. diff --git a/synctl b/synctl index e3aa8ec1e526..81e9bf6b5c39 100755 --- a/synctl +++ b/synctl @@ -322,7 +322,7 @@ def main(): has_stopped = True for worker in workers: if not stop(worker.pidfile, worker.app): - # A worker doesn't stop + # A worker could not be stopped. has_stopped = False if start_stop_synapse: