From f1280a9622740c68f95b91a301d2ca8ac799724b Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 14 Oct 2019 10:28:06 +0200 Subject: [PATCH] Faster account creation by moving Nextcloud API call to background --- app/main.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index 4a36ebc..78f477c 100644 --- a/app/main.py +++ b/app/main.py @@ -11,6 +11,7 @@ import logging import sys import json import shlex +from threading import Thread ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) @@ -72,6 +73,19 @@ class MyHandler(BaseHTTPRequestHandler): self.respond(500, data) return + self.respond(200, json.dumps({'success': True})) + + # Run Nextcloud API call in seperate thread. Needed because the API calls take 50+ seconds + # each for unknown reasons. Instead we just run them in the background. + Thread(target=self.call_nextcloud_api, args=(target_email, password, fallback_email, nextcloud_quota)).start() + + def respond(self, status: int, message: str): + print(f'sending response status {status}, message {message}') + self.send_response(status) + self.end_headers() + self.wfile.write(message.encode()) + + def call_nextcloud_api(self, target_email: str, password: str, fallback_email: str, nextcloud_quota: str): # Nextcloud only creates external accounts after the first login. We login through the API # to trigger the account creation. headers = {'OCS-APIRequest': 'true'} @@ -88,18 +102,6 @@ class MyHandler(BaseHTTPRequestHandler): print(f'status code: {r1.status_code}') print(r2.text) print(f'status code: {r2.status_code}') - if r1.status_code != 200 or r2.status_code != 200: - data = json.dumps({'success': False, 'message': 'internal_error'}) - self.respond(403, data) - return - - self.respond(200, json.dumps({'success': True})) - - def respond(self, status: int, message: str): - print(f'sending response status {status}, message {message}') - self.send_response(status) - self.end_headers() - self.wfile.write(message.encode()) if __name__ == '__main__': -- GitLab