Skip to content

Commit

Permalink
Put port number and primary port number in config file.
Browse files Browse the repository at this point in the history
Get rid of the corresponding ugly code in rundb.py.
  • Loading branch information
vdbergh authored and ppigazzini committed May 12, 2024
1 parent ee4cde1 commit 291c616
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 46 deletions.
3 changes: 3 additions & 0 deletions server/development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ debugtoolbar.hosts = 0.0.0.0/0 ::1

mako.directories = fishtest:templates

fishtest.port = 6542
fishtest.primary_port = 6542

###
# wsgi server configuration
###
Expand Down
8 changes: 7 additions & 1 deletion server/fishtest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ def file_hash(file):
for j in static_full_path(i).glob(f"*.{i}")
}

rundb = RunDb()
port = int(settings.get("fishtest.port", -1))
primary_port = int(settings.get("fishtest.primary_port", -1))
# If the port number cannot be determined like during unit tests or CI,
# assume the instance is primary for backward compatibility.
is_primary_instance = port == primary_port

rundb = RunDb(port=port, is_primary_instance=is_primary_instance)

def add_rundb(event):
event.request.rundb = rundb
Expand Down
50 changes: 5 additions & 45 deletions server/fishtest/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,46 +44,8 @@
last_rundb = None


def get_port():
params = {}
args = sys.argv
if len(args) <= 1:
return -1
if os.path.basename(args[0]) != "pserve":
return -2
config = args[1]
for arg in args[2:]:
if arg[0] == "-" or "=" not in arg:
continue
arg = arg.split("=")
params[arg[0].strip()] = arg[1].strip()
c = configparser.ConfigParser(defaults=params)
try:
c.read(config)
except:
return -3
try:
section = c["server:main"]
except:
return -4
if "port" in section:
port = section["port"]
elif "listen" in section:
listen = section["listen"]
port = listen.split(":")[-1]
else:
return -5
try:
return int(port)
except:
return -6


PRIMARY_INSTANCE_PORTS = [6542, 6543]


class RunDb:
def __init__(self, db_name="fishtest_new"):
def __init__(self, db_name="fishtest_new", port=-1, is_primary_instance=True):
# MongoDB server is assumed to be on the same machine, if not user should
# use ssh with port forwarding to access the remote host.
self.conn = MongoClient(os.getenv("FISHTEST_HOST") or "localhost")
Expand All @@ -96,9 +58,7 @@ def __init__(self, db_name="fishtest_new"):
self.nndb = self.db["nns"]
self.runs = self.db["runs"]
self.deltas = self.db["deltas"]
self.port = get_port()
if self.port < 0:
print(f"Unable to obtain the port number. Error: {self.port}", flush=True)
self.port = port
self.task_runs = []

self.task_duration = 1800 # 30 minutes
Expand All @@ -115,6 +75,8 @@ def __init__(self, db_name="fishtest_new"):
if self.port >= 0:
self.actiondb.system_event(message=f"start fishtest@{self.port}")

self.__is_primary_instance = is_primary_instance

def new_run(
self,
base_tag,
Expand Down Expand Up @@ -259,9 +221,7 @@ def new_run(
return self.runs.insert_one(new_run).inserted_id

def is_primary_instance(self):
# If the port number cannot be determined like during unit tests or CI,
# assume the instance is primary for backward compatibility.
return self.port < 0 or self.port in PRIMARY_INSTANCE_PORTS
return self.__is_primary_instance

def upload_pgn(self, run_id, pgn_zip):
record = {"run_id": run_id, "pgn_zip": Binary(pgn_zip), "size": len(pgn_zip)}
Expand Down
3 changes: 3 additions & 0 deletions server/production.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pyramid.default_locale_name = en

mako.directories = fishtest:templates

fishtest.port = %(http_port)s
fishtest.primary_port = 6543

###
# wsgi server configuration
###
Expand Down

0 comments on commit 291c616

Please sign in to comment.