From 8435f3c7f8c058f6db045c6fc413860e3aeaa1bb Mon Sep 17 00:00:00 2001 From: Jakob Langdal Date: Tue, 25 Feb 2025 12:20:28 +0000 Subject: [PATCH] Add timeout --- README.md | 20 +++++++++++++------- optimizerapi/optimizer.py | 10 +++++++--- optimizerapi/worker.py | 5 +++++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 682e535..c2dec66 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,13 @@ worker threads using the following commands: The Redis server can be controlled through the environment variable `REDIS_URL` which defaults to `redis://localhost:6379` +Time to live and timeout of the workers can be controlled with the following environment variables + +| Name | Description | +| -------------- | ------------------------------------------- | +| REDIS_TTL | Time to keep results in redis (default=500) | +| WORKER_TIMEOUT | Timeout in seconds (default=180) | + # Use [CORS](https://flask-cors.readthedocs.io/en/latest/index.html) The API server supports exposing its functionality to other origins than its own. @@ -92,13 +99,12 @@ The static API key is configured by the environment variable `AUTH_API_KEY` Keycloak is configured using the following environement variables - -|Name |Description | -|-------------------|-----------------------------------| -|AUTH_SERVER |Base url of your Keycloak server | -|AUTH_REALM_NAME |OAuth realm name | -|AUTH_CLIENT_ID |Client ID | -|AUTH_CLIENT_SECRET |Client secret | +| Name | Description | +| ------------------ | -------------------------------- | +| AUTH_SERVER | Base url of your Keycloak server | +| AUTH_REALM_NAME | OAuth realm name | +| AUTH_CLIENT_ID | Client ID | +| AUTH_CLIENT_SECRET | Client secret | # Adding or updating dependencies diff --git a/optimizerapi/optimizer.py b/optimizerapi/optimizer.py index 062d267..f0c8e60 100644 --- a/optimizerapi/optimizer.py +++ b/optimizerapi/optimizer.py @@ -43,15 +43,18 @@ REDIS_URL = "redis://localhost:6379" print("Connecting to" + REDIS_URL) redis = Redis.from_url(REDIS_URL) -queue = Queue(connection=redis) if "REDIS_TTL" in os.environ: TTL = int(os.environ["REDIS_TTL"]) else: TTL = 500 +if "WORKER_TIMEOUT" in os.environ: + WORKER_TIMEOUT = os.environ["WORKER_TIMEOUT"] +else: + WORKER_TIMEOUT = "180" +queue = Queue(connection=redis) plt.switch_backend("Agg") - def run(body) -> dict: """Executes the ProcessOptimizer @@ -79,10 +82,11 @@ def disconnect_check(): job_id = body_hash.hexdigest() try: job = Job.fetch(job_id, connection=redis) + print("Found existing job") except NoSuchJobError: print("Creating new job") - job = queue.enqueue(do_run_work, body, job_id=job_id, result_ttl=TTL) + job = queue.enqueue(do_run_work, body, job_id=job_id, result_ttl=TTL, job_timeout=WORKER_TIMEOUT, ) while job.return_value() is None: if disconnect_check(): try: diff --git a/optimizerapi/worker.py b/optimizerapi/worker.py index 23059e1..30cca80 100644 --- a/optimizerapi/worker.py +++ b/optimizerapi/worker.py @@ -11,6 +11,11 @@ else: REDIS_URL = "redis://localhost:6379" +if "WORKER_TIMEOUT" in os.environ: + WORKER_TIMEOUT = int(os.environ["WORKER_TIMEOUT"]) +else: + WORKER_TIMEOUT = 180 + if __name__ == "__main__": redis = Redis.from_url(REDIS_URL) queue = Queue(connection=redis)