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

Add process hooks to tell systemd our state. #5732

Merged
merged 5 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5732.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add sd_notify hooks to ease systemd integration and allows usage of Type=Notify.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ After=matrix-synapse.service
BindsTo=matrix-synapse.service

[Service]
Type=simple
Type=notify
NotifyAccess=main
User=matrix-synapse
WorkingDirectory=/var/lib/matrix-synapse
EnvironmentFile=/etc/default/matrix-synapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Description=Synapse Matrix Homeserver

[Service]
Type=simple
Type=notify
NotifyAccess=main
User=matrix-synapse
WorkingDirectory=/var/lib/matrix-synapse
EnvironmentFile=/etc/default/matrix-synapse
Expand Down
4 changes: 3 additions & 1 deletion contrib/systemd/matrix-synapse.service
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
Description=Synapse Matrix homeserver

[Service]
Type=simple
Type=notify
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
NotifyAccess=main
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-abort

User=synapse
Expand Down
29 changes: 29 additions & 0 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

import gc
import logging
import os
import signal
import sys
import traceback

import sdnotify
from daemonize import Daemonize

from twisted.internet import defer, error, reactor
Expand Down Expand Up @@ -242,9 +244,16 @@ def start(hs, listeners=None):
if hasattr(signal, "SIGHUP"):

def handle_sighup(*args, **kwargs):
# Tell systemd our state, if we're using it. This will silently fail if
# we're not using systemd.
sd_channel = sdnotify.SystemdNotifier()
sd_channel.notify("RELOADING=1")

for i in _sighup_callbacks:
i(hs)

sd_channel.notify("READY=1")

signal.signal(signal.SIGHUP, handle_sighup)

register_sighup(refresh_certificate)
Expand All @@ -260,6 +269,7 @@ def handle_sighup(*args, **kwargs):
hs.get_datastore().start_profiling()

setup_sentry(hs)
setup_sdnotify(hs)
except Exception:
traceback.print_exc(file=sys.stderr)
reactor = hs.get_reactor()
Expand Down Expand Up @@ -292,6 +302,25 @@ def setup_sentry(hs):
scope.set_tag("worker_name", name)


def setup_sdnotify(hs):
"""Adds process state hooks to tell systemd what we are up to.
"""

# Tell systemd our state, if we're using it. This will silently fail if
# we're not using systemd.
sd_channel = sdnotify.SystemdNotifier()

hs.get_reactor().addSystemEventTrigger(
"after",
"startup",
lambda: sd_channel.notify("READY=1\nMAINPID=%s" % (os.getpid())),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this need to happen a bit later? IIRC we do some acme foo after the reactor starts but before we bind the http and replication listeners

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it looked like we started listening pretty much immediately we start the reactor, but actually we do some acme stuff first. Good spot.

)

hs.get_reactor().addSystemEventTrigger(
"before", "shutdown", lambda: sd_channel.notify("STOPPING=1")
)


def install_dns_limiter(reactor, max_dns_requests_in_flight=100):
"""Replaces the resolver with one that limits the number of in flight DNS
requests.
Expand Down
4 changes: 3 additions & 1 deletion synapse/app/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def start(config_options):
)

ps.setup()
reactor.callWhenRunning(_base.start, ps, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ps, config.worker_listeners
)

_base.start_worker_reactor("synapse-appservice", config)

Expand Down
4 changes: 3 additions & 1 deletion synapse/app/client_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ss, config.worker_listeners
)

_base.start_worker_reactor("synapse-client-reader", config)

Expand Down
4 changes: 3 additions & 1 deletion synapse/app/event_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ss, config.worker_listeners
)

_base.start_worker_reactor("synapse-event-creator", config)

Expand Down
4 changes: 3 additions & 1 deletion synapse/app/federation_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ss, config.worker_listeners
)

_base.start_worker_reactor("synapse-federation-reader", config)

Expand Down
4 changes: 3 additions & 1 deletion synapse/app/federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ss, config.worker_listeners
)

_base.start_worker_reactor("synapse-federation-sender", config)

Expand Down
4 changes: 3 additions & 1 deletion synapse/app/frontend_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ss, config.worker_listeners
)

_base.start_worker_reactor("synapse-frontend-proxy", config)

Expand Down
2 changes: 1 addition & 1 deletion synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def start():
reactor.stop()
sys.exit(1)

reactor.callWhenRunning(start)
reactor.addSystemEventTrigger("before", "startup", start)

return hs

Expand Down
4 changes: 3 additions & 1 deletion synapse/app/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ss, config.worker_listeners
)

_base.start_worker_reactor("synapse-media-repository", config)

Expand Down
2 changes: 1 addition & 1 deletion synapse/app/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def start():
_base.start(ps, config.worker_listeners)
ps.get_pusherpool().start()

reactor.callWhenRunning(start)
reactor.addSystemEventTrigger("before", "startup", start)

_base.start_worker_reactor("synapse-pusher", config)

Expand Down
4 changes: 3 additions & 1 deletion synapse/app/synchrotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ss, config.worker_listeners
)

_base.start_worker_reactor("synapse-synchrotron", config)

Expand Down
4 changes: 3 additions & 1 deletion synapse/app/user_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)
reactor.addSystemEventTrigger(
"before", "startup", _base.start, ss, config.worker_listeners
)

_base.start_worker_reactor("synapse-user-dir", config)

Expand Down
1 change: 1 addition & 0 deletions synapse/python_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"netaddr>=0.7.18",
"Jinja2>=2.9",
"bleach>=1.4.3",
"sdnotify>=0.3",
]

CONDITIONAL_REQUIREMENTS = {
Expand Down