worker: move global variables to config class

This commit is contained in:
Jörg Thalheim 2023-09-15 09:09:41 +02:00
parent 3ccf4ba24f
commit aeffb167fe

View file

@ -3,6 +3,7 @@
import multiprocessing import multiprocessing
import os import os
import socket import socket
from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from buildbot_worker.bot import Worker from buildbot_worker.bot import Worker
@ -15,13 +16,20 @@ def require_env(key: str) -> str:
return val return val
PASSWD = Path(require_env("WORKER_PASSWORD_FILE")).read_text().strip("\r\n") @dataclass
BUILDBOT_DIR = require_env("BUILDBOT_DIR") class WorkerConfig:
MASTER_URL = require_env("MASTER_URL") password: str = Path(require_env("WORKER_PASSWORD_FILE")).read_text().rstrip("\r\n")
worker_count: int = int(
os.environ.get("WORKER_COUNT", str(multiprocessing.cpu_count()))
)
buildbot_dir: str = require_env("BUILDBOT_DIR")
master_url: str = require_env("MASTER_URL")
def setup_worker(application: service.Application, id: int) -> None: def setup_worker(
basedir = f"{BUILDBOT_DIR}-{id}" application: service.Application, id: int, config: WorkerConfig
) -> None:
basedir = f"{config.buildbot_dir}-{id}"
os.makedirs(basedir, mode=0o700, exist_ok=True) os.makedirs(basedir, mode=0o700, exist_ok=True)
hostname = socket.gethostname() hostname = socket.gethostname()
@ -36,10 +44,10 @@ def setup_worker(application: service.Application, id: int) -> None:
None, None,
None, None,
workername, workername,
PASSWD, config.password,
basedir, basedir,
keepalive, keepalive,
connection_string=MASTER_URL, connection_string=config.master_url,
umask=umask, umask=umask,
maxdelay=maxdelay, maxdelay=maxdelay,
numcpus=numcpus, numcpus=numcpus,
@ -50,9 +58,12 @@ def setup_worker(application: service.Application, id: int) -> None:
s.setServiceParent(application) s.setServiceParent(application)
def setup_workers(application: service.Application, config: WorkerConfig) -> None:
for i in range(config.worker_count):
setup_worker(application, i, config)
# note: this line is matched against to check that this is a worker # note: this line is matched against to check that this is a worker
# directory; do not edit it. # directory; do not edit it.
application = service.Application("buildbot-worker") application = service.Application("buildbot-worker")
setup_workers(application, WorkerConfig())
for i in range(multiprocessing.cpu_count()):
setup_worker(application, i)