From bd50baa639115adacc2b88798394939cd694e8ee Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Tue, 2 Apr 2024 15:18:31 -0400 Subject: [PATCH] re-initialize Application in push workers for windows --- relay/application.py | 24 +++++++++++++++++++++--- relay/misc.py | 2 ++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/relay/application.py b/relay/application.py index 0c2266f..cc87c22 100644 --- a/relay/application.py +++ b/relay/application.py @@ -22,7 +22,7 @@ from .cache import get_cache from .config import Config from .database import get_database from .http_client import HttpClient -from .misc import check_open_port, get_resource +from .misc import IS_WINDOWS, check_open_port, get_resource from .template import Template from .views import VIEWS from .views.api import handle_api_path @@ -296,9 +296,14 @@ class CacheCleanupThread(Thread): class PushWorker(multiprocessing.Process): def __init__(self, queue: multiprocessing.Queue): + if Application.DEFAULT is None: + raise RuntimeError('Application not setup yet') + multiprocessing.Process.__init__(self) + self.queue = queue self.shutdown = multiprocessing.Event() + self.path = Application.DEFAULT.config.path def stop(self) -> None: @@ -310,8 +315,17 @@ class PushWorker(multiprocessing.Process): async def handle_queue(self) -> None: - client = HttpClient() - client.open() + if IS_WINDOWS: + app = Application(self.path) + client = app.client + + client.open() + app.database.connect() + app.cache.setup() + + else: + client = HttpClient() + client.open() while not self.shutdown.is_set(): try: @@ -325,6 +339,10 @@ class PushWorker(multiprocessing.Process): except Exception: traceback.print_exc() + if IS_WINDOWS: + app.database.disconnect() + app.cache.close() + await client.close() diff --git a/relay/misc.py b/relay/misc.py index 82b1fd2..729cfd7 100644 --- a/relay/misc.py +++ b/relay/misc.py @@ -3,6 +3,7 @@ from __future__ import annotations import aputils import json import os +import platform import socket import typing @@ -38,6 +39,7 @@ ResponseType = typing.TypedDict('ResponseType', { }) IS_DOCKER = bool(os.environ.get('DOCKER_RUNNING')) +IS_WINDOWS = platform.system() == 'Windows' MIMETYPES = { 'activity': 'application/activity+json',