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

Commit

Permalink
Convert the main methods run with the reactor to async.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Aug 31, 2020
1 parent 45e8f77 commit e569089
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
1 change: 1 addition & 0 deletions changelog.d/8213.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert various parts of the codebase to async/await.
14 changes: 5 additions & 9 deletions synapse/app/admin_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def start_listening(self, listeners):
pass


@defer.inlineCallbacks
def export_data_command(hs, args):
async def export_data_command(hs, args):
"""Export data for a user.
Args:
Expand All @@ -91,10 +90,8 @@ def export_data_command(hs, args):
user_id = args.user_id
directory = args.output_directory

res = yield defer.ensureDeferred(
hs.get_handlers().admin_handler.export_user_data(
user_id, FileExfiltrationWriter(user_id, directory=directory)
)
res = await hs.get_handlers().admin_handler.export_user_data(
user_id, FileExfiltrationWriter(user_id, directory=directory)
)
print(res)

Expand Down Expand Up @@ -232,11 +229,10 @@ def start(config_options):
# We also make sure that `_base.start` gets run before we actually run the
# command.

@defer.inlineCallbacks
def run(_reactor):
with LoggingContext("command"):
yield _base.start(ss, [])
yield args.func(ss, args)
_base.start(ss, [])
return defer.ensureDeferred(args.func(ss, args))

_base.start_worker_reactor(
"synapse-admin-cmd", config, run_command=lambda: task.react(run)
Expand Down
18 changes: 8 additions & 10 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,26 +411,24 @@ async def do_acme() -> bool:

return provision

@defer.inlineCallbacks
def reprovision_acme():
async def reprovision_acme():
"""
Provision a certificate from ACME, if required, and reload the TLS
certificate if it's renewed.
"""
reprovisioned = yield defer.ensureDeferred(do_acme())
reprovisioned = await do_acme()
if reprovisioned:
_base.refresh_certificate(hs)

@defer.inlineCallbacks
def start():
async def start():
try:
# Run the ACME provisioning code, if it's enabled.
if hs.config.acme_enabled:
acme = hs.get_acme_handler()
# Start up the webservices which we will respond to ACME
# challenges with, and then provision.
yield defer.ensureDeferred(acme.start_listening())
yield defer.ensureDeferred(do_acme())
await acme.start_listening()
await do_acme()

# Check if it needs to be reprovisioned every day.
hs.get_clock().looping_call(reprovision_acme, 24 * 60 * 60 * 1000)
Expand All @@ -439,8 +437,8 @@ def start():
if hs.config.oidc_enabled:
oidc = hs.get_oidc_handler()
# Loading the provider metadata also ensures the provider config is valid.
yield defer.ensureDeferred(oidc.load_metadata())
yield defer.ensureDeferred(oidc.load_jwks())
await oidc.load_metadata()
await oidc.load_jwks()

_base.start(hs, config.listeners)

Expand All @@ -456,7 +454,7 @@ def start():
reactor.stop()
sys.exit(1)

reactor.callWhenRunning(start)
reactor.callWhenRunning(lambda: defer.ensureDeferred(start()))

return hs

Expand Down

0 comments on commit e569089

Please sign in to comment.