Stop browsers that weren't accessed in a long time. #41
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I had a bit of time on my hand, so I implemented an initial idea for stopping browsers that weren't accessed in a long time to free up used ports.
Whenever a user opens a workspace, the server updates the access time of the corresponding background process.
The clean up is happening during our
lifespan()
function as an infinitely running async background task.In order for this to work I added the methods
update_access_time(browser)
andbrowsers_accessed_before(time)
to the repositories.processtimeout.expiration_loop()
is the background task called in the lifespan.Note that in order to be able to test this functionality, I had to create a special lifetime for our tests, as the continuous loop would have prevented our tests from ever completing. You can find it in
tests/ocrdmonitor/fixures/lifespan.py
It uses the same tasks as the production lifetime (found in
ocrdmonitor/main.py
), but with different arguments forexpiration_loop
to only loop once.The timeout for browsers is configured in
init.sh
with the new environment variableOCRD_BROWSER__TIMEOUT
.Its value is a duration in ISO 8601 format as that is supported out of the box by
pydantic
(https://docs.pydantic.dev/latest/api/standard_library_types/#datetimetimedelta)