mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-21 22:41:43 +03:00
e24bcf2a39
We seem to be getting flakes where we try and use the same port for two different servers. This is because in certain cases we cannot simply allocate the port dynamically, but have to decide it in advance, leading to a race condition. We resolve this by keeping track of the ports we allocate when using this method, making sure we never allocate them twice. We also make sure we allocate from a different pool of ports to the usual dynamic port pool (typically above port 32768, and often above port 49152). PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8903 GitOrigin-RevId: 375a23867591a4566493dddbc550c58cf88ea392
31 lines
866 B
Python
31 lines
866 B
Python
import os
|
|
import subprocess
|
|
from typing import Optional
|
|
|
|
import ports
|
|
|
|
class NodeGraphQL:
|
|
def __init__(self, worker_id: str, script: str, env: dict[str, str] = {}, port: Optional[int] = None):
|
|
self.script = script
|
|
self.env = env
|
|
self.port = port if port else ports.find_free_port(worker_id)
|
|
self.proc: Optional[subprocess.Popen[bytes]] = None
|
|
|
|
def start(self):
|
|
self.proc = subprocess.Popen(['node', self.script], env={**os.environ, **self.env, 'PORT': str(self.port)})
|
|
try:
|
|
ports.wait_for_port(self.port, timeout = 30)
|
|
except:
|
|
self.proc.kill()
|
|
self.proc = None
|
|
raise
|
|
|
|
def stop(self):
|
|
if self.proc:
|
|
self.proc.terminate()
|
|
self.proc = None
|
|
|
|
@property
|
|
def url(self):
|
|
return f'http://localhost:{self.port}'
|