Skip to content

Commit fc692c7

Browse files
authored
Merge pull request #81 from BoostV/control-worker-timeout
Control worker timeout
2 parents d52d9b4 + 8435f3c commit fc692c7

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ worker threads using the following commands:
6262

6363
The Redis server can be controlled through the environment variable `REDIS_URL` which defaults to `redis://localhost:6379`
6464

65+
Time to live and timeout of the workers can be controlled with the following environment variables
66+
67+
| Name | Description |
68+
| -------------- | ------------------------------------------- |
69+
| REDIS_TTL | Time to keep results in redis (default=500) |
70+
| WORKER_TIMEOUT | Timeout in seconds (default=180) |
71+
6572
# Use [CORS](https://flask-cors.readthedocs.io/en/latest/index.html)
6673

6774
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`
9299

93100
Keycloak is configured using the following environement variables
94101

95-
96-
|Name |Description |
97-
|-------------------|-----------------------------------|
98-
|AUTH_SERVER |Base url of your Keycloak server |
99-
|AUTH_REALM_NAME |OAuth realm name |
100-
|AUTH_CLIENT_ID |Client ID |
101-
|AUTH_CLIENT_SECRET |Client secret |
102+
| Name | Description |
103+
| ------------------ | -------------------------------- |
104+
| AUTH_SERVER | Base url of your Keycloak server |
105+
| AUTH_REALM_NAME | OAuth realm name |
106+
| AUTH_CLIENT_ID | Client ID |
107+
| AUTH_CLIENT_SECRET | Client secret |
102108

103109
# Adding or updating dependencies
104110

optimizerapi/optimizer.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@
4343
REDIS_URL = "redis://localhost:6379"
4444
print("Connecting to" + REDIS_URL)
4545
redis = Redis.from_url(REDIS_URL)
46-
queue = Queue(connection=redis)
4746
if "REDIS_TTL" in os.environ:
4847
TTL = int(os.environ["REDIS_TTL"])
4948
else:
5049
TTL = 500
50+
if "WORKER_TIMEOUT" in os.environ:
51+
WORKER_TIMEOUT = os.environ["WORKER_TIMEOUT"]
52+
else:
53+
WORKER_TIMEOUT = "180"
5154

55+
queue = Queue(connection=redis)
5256
plt.switch_backend("Agg")
5357

54-
5558
def run(body) -> dict:
5659
"""Executes the ProcessOptimizer
5760
@@ -79,10 +82,11 @@ def disconnect_check():
7982
job_id = body_hash.hexdigest()
8083
try:
8184
job = Job.fetch(job_id, connection=redis)
85+
8286
print("Found existing job")
8387
except NoSuchJobError:
8488
print("Creating new job")
85-
job = queue.enqueue(do_run_work, body, job_id=job_id, result_ttl=TTL)
89+
job = queue.enqueue(do_run_work, body, job_id=job_id, result_ttl=TTL, job_timeout=WORKER_TIMEOUT, )
8690
while job.return_value() is None:
8791
if disconnect_check():
8892
try:

optimizerapi/worker.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
else:
1212
REDIS_URL = "redis://localhost:6379"
1313

14+
if "WORKER_TIMEOUT" in os.environ:
15+
WORKER_TIMEOUT = int(os.environ["WORKER_TIMEOUT"])
16+
else:
17+
WORKER_TIMEOUT = 180
18+
1419
if __name__ == "__main__":
1520
redis = Redis.from_url(REDIS_URL)
1621
queue = Queue(connection=redis)

0 commit comments

Comments
 (0)