Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Give appropriate exit codes when synctl fails #5992

Merged
merged 10 commits into from
Sep 18, 2019
1 change: 1 addition & 0 deletions changelog.d/5992.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Give appropriate exit codes when synctl fails.
40 changes: 36 additions & 4 deletions synctl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,20 @@ def abort(message, colour=RED, stream=sys.stderr):
sys.exit(1)


def start(configfile, daemonize=True):
def start(configfile: str, daemonize: bool = True) -> bool:
"""Attempts to start synapse.
Args:
configfile: path to a yaml synapse config file
daemonize: whether to daemonize synapse or keep it attached to the current
session

Returns:
True if the process started successfully
False if there was an error starting the process

If deamonize is False it will only return once synapse exits.
"""

write("Starting ...")
args = SYNAPSE

Expand All @@ -83,25 +96,39 @@ def start(configfile, daemonize=True):
try:
subprocess.check_call(args)
write("started synapse.app.homeserver(%r)" % (configfile,), colour=GREEN)
return True
except subprocess.CalledProcessError as e:
write(
"error starting (exit code: %d); see above for logs" % e.returncode,
colour=RED,
)
return False


def start_worker(app: str, configfile: str, worker_configfile: str) -> bool:
"""Attempts to start a synapse worker.
Args:
app: name of the worker's appservice
configfile: path to a yaml synapse config file
worker_configfile: path to worker specific yaml synapse file

Returns:
boolean stating whether synapse started successfully if daemonize is True
JorikSchellekens marked this conversation as resolved.
Show resolved Hide resolved
"""

def start_worker(app, configfile, worker_configfile):
args = [sys.executable, "-B", "-m", app, "-c", configfile, "-c", worker_configfile]

try:
subprocess.check_call(args)
write("started %s(%r)" % (app, worker_configfile), colour=GREEN)
return True
except subprocess.CalledProcessError as e:
write(
"error starting %s(%r) (exit code: %d); see above for logs"
% (app, worker_configfile, e.returncode),
colour=RED,
)
return False


def stop(pidfile, app):
Expand Down Expand Up @@ -292,11 +319,13 @@ def main():
write("All processes exited; now restarting...")

if action == "start" or action == "restart":
error = False
if start_stop_synapse:
# Check if synapse is already running
if os.path.exists(pidfile) and pid_running(int(open(pidfile).read())):
abort("synapse.app.homeserver already running")
start(configfile, bool(options.daemonize))

error |= start(configfile, bool(options.daemonize))
JorikSchellekens marked this conversation as resolved.
Show resolved Hide resolved

for worker in workers:
env = os.environ.copy()
Expand All @@ -307,12 +336,15 @@ def main():
for cache_name, factor in iteritems(worker.cache_factors):
os.environ["SYNAPSE_CACHE_FACTOR_" + cache_name.upper()] = str(factor)

start_worker(worker.app, configfile, worker.configfile)
error |= start_worker(worker.app, configfile, worker.configfile)

# Reset env back to the original
os.environ.clear()
os.environ.update(env)

if error:
exit(1)


if __name__ == "__main__":
main()