Skip to content

Commit

Permalink
Make THREAD_WATCH_TIMEOUT configurable via env var (#1903)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsangmeister committed Oct 10, 2023
1 parent 732341a commit 2d8d4a4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 44 deletions.
83 changes: 48 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,77 +71,90 @@ The action component listens to port 9002. The presenter component listens to po

## Environment variables

* OPENSLIDES_BACKEND_COMPONENT
### Functionality

* `OPENSLIDES_BACKEND_COMPONENT`

Use one of the following values to start only one component of this service: `action` or `presenter`. Defaults to all of them using different child processes. If using `all` you can shut down all compontes by sending SIGTERM to Python master process.

* ACTION_PORT
* `ACTION_PORT`

Action component listens on this port. Default: 9002
Action component listens on this port. Default: `9002`

* PRESENTER_PORT
* `PRESENTER_PORT`

Presenter component listens on this port. Default 9003
Presenter component listens on this port. Default `9003`

* OPENSLIDES_DEVELOPMENT
* `OPENTELEMETRY_ENABLED`

Set this variable e. g. to 1 to set loglevel to `debug` and activate Gunicorn's reload mechanism.
Set this variable e. g. to `1` to enable span reporting to an OpenTelemetry collector (defined in the main OpenSlides repository).

* OPENTELEMETRY_ENABLED
* `OPENSLIDES_LOGLEVEL`

Set this variable e. g. to 1 to enable span reporting to an OpenTelemetry collector (defined in the main OpenSlides repository).
In production mode you can set the loglevel to `debug`, `info`, `warning`, `error` or `critical`. Default is `info`.

* OPENSLIDES_LOGLEVEL
* `OPENSLIDES_BACKEND_NUM_WORKERS`

In production mode you can set the loglevel to `debug`, `info`, `warning`, `error` or `critical`. Default is `info`.
Number of Gunicorn workers. Default: `1`

* OPENSLIDES_BACKEND_RAISE_4XX
* `OPENSLIDES_BACKEND_WORKER_TIMEOUT`

Set this variable to raise HTTP 400 and 403 as exceptions instead of valid HTTP responses.
Gunicorn worker timeout in seconds. Default: `30`

* `OPENSLIDES_BACKEND_THREAD_WATCH_TIMEOUT`

* DATASTORE_READER_PROTOCOL
Seconds after which an action is delegated to an action worker. `-1` deactivates action workers all together. Default: `1.0`

Protocol of datastore reader service. Default: http
### Development

* `OPENSLIDES_DEVELOPMENT`

Set this variable e. g. to `1` to set loglevel to `debug` and activate Gunicorn's reload mechanism.

* `OPENSLIDES_BACKEND_RAISE_4XX`

Set this variable to raise HTTP 400 and 403 as exceptions instead of valid HTTP responses.

* DATASTORE_READER_HOST
### Connection to other services
* `DATASTORE_READER_PROTOCOL`

Host of datastore reader service. Default: localhost
Protocol of datastore reader service. Default: `http`

* DATASTORE_READER_PORT
* `DATASTORE_READER_HOST`

Port of datastore reader service. Default: 9010
Host of datastore reader service. Default: `localhost`

* DATASTORE_READER_PATH
* `DATASTORE_READER_PORT`

Path of datastore reader service. Default: /internal/datastore/reader
Port of datastore reader service. Default: `9010`

* DATASTORE_WRITER_PROTOCOL
* `DATASTORE_READER_PATH`

Protocol of datastore writer service. Default: http
Path of datastore reader service. Default: `/internal/datastore/reader`

* DATASTORE_WRITER_HOST
* `DATASTORE_WRITER_PROTOCOL`

Host of datastore writer service. Default: localhost
Protocol of datastore writer service. Default: `http`

* DATASTORE_WRITER_PORT
* `DATASTORE_WRITER_HOST`

Port of datastore writer service. Default: 9011
Host of datastore writer service. Default: `localhost`

* DATASTORE_WRITER_PATH
* `DATASTORE_WRITER_PORT`

Path of datastore writer service. Default: /internal/datastore/writer
Port of datastore writer service. Default: `9011`

* OPENSLIDES_BACKEND_NUM_WORKERS
* `DATASTORE_WRITER_PATH`

Number of Gunicorn workers. Default: 1
Path of datastore writer service. Default: `/internal/datastore/writer`

* OPENSLIDES_BACKEND_WORKER_TIMEOUT
* `AUTH_HOST`

Gunicorn worker timeout in seconds. Default: 30
Host of auth service. Used by the `authlib` package. Default: `localhost`

* AUTH_HOST and AUTH_PORT
* `AUTH_PORT`

Implicitly used by the authlib to get the endpoint for the auth-service
Port of auth service. Used by the `authlib` package. Default: `9004`


# Some curl examples
Expand Down
6 changes: 3 additions & 3 deletions openslides_backend/action/action_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
from .action_handler import ActionHandler
from .util.typing import ActionsResponse, Payload

THREAD_WATCH_TIMEOUT = 1.0


def handle_action_in_worker_thread(
payload: Payload,
Expand Down Expand Up @@ -51,7 +49,9 @@ def handle_action_in_worker_thread(
action_worker_thread.start()
while not action_worker_thread.started:
sleep(0.001) # The action_worker_thread should gain the lock and NOT this one
if lock.acquire(timeout=THREAD_WATCH_TIMEOUT):

timeout = float(handler.env.OPENSLIDES_BACKEND_THREAD_WATCH_TIMEOUT)
if lock.acquire(timeout=timeout):
lock.release()
if hasattr(action_worker_thread, "exception"):
raise action_worker_thread.exception
Expand Down
7 changes: 4 additions & 3 deletions openslides_backend/shared/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Environment:
"OPENSLIDES_BACKEND_NUM_THREADS": "3",
"OPENSLIDES_BACKEND_RAISE_4XX": "false",
"OPENSLIDES_BACKEND_WORKER_TIMEOUT": "30",
"OPENSLIDES_BACKEND_THREAD_WATCH_TIMEOUT": "1",
"OPENSLIDES_DEVELOPMENT": "false",
"OPENSLIDES_LOGLEVEL": Loglevel.NOTSET.name,
"OPENTELEMETRY_ENABLED": "false",
Expand All @@ -54,10 +55,10 @@ class Environment:
}

def __init__(self, os_env: Any, *args: Any, **kwargs: Any) -> None:
for k, v in list(self.vars.items()):
env = os_env.get(k)
for key in self.vars.keys():
env = os_env.get(key)
if env is not None:
self.vars[k] = env
self.vars[key] = env

def __getattr__(self, attr: str) -> str:
value = self.vars.get(attr)
Expand Down
5 changes: 2 additions & 3 deletions tests/system/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from datastore.shared.util import DeletedModelsBehaviour, is_reserved_field
from fastjsonschema.exceptions import JsonSchemaException

from openslides_backend.action import action_worker
from openslides_backend.models.base import Model, model_registry
from openslides_backend.services.auth.interface import AuthenticationService
from openslides_backend.services.datastore.interface import DatastoreService
Expand Down Expand Up @@ -93,8 +92,8 @@ def setUp(self) -> None:
self.vote_service.clear_all()
self.anon_client = self.create_client()

def set_thread_watch_timeout(self, thread_watch_timeout: float) -> None:
action_worker.THREAD_WATCH_TIMEOUT = thread_watch_timeout
def set_thread_watch_timeout(self, timeout: float) -> None:
self.app.env.vars["OPENSLIDES_BACKEND_THREAD_WATCH_TIMEOUT"] = str(timeout)

def tearDown(self) -> None:
if thread := self.__class__.get_thread_by_name("action_worker"):
Expand Down

0 comments on commit 2d8d4a4

Please sign in to comment.