Skip to content

Commit 846d0ea

Browse files
authored
Change session files garbage collection strategy. #8335
Currently GC for session files is done by the /misc/cleanup endpoint which requires browser UI to be running. With this change, the pgAdmin server will open a separate thread at a fixed frequency which will take care of GC independently.
1 parent cd179a3 commit 846d0ea

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

web/pgadmin/misc/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pgadmin.utils.heartbeat import log_server_heartbeat, \
2626
get_server_heartbeat, stop_server_heartbeat
2727
import config
28+
import threading
2829
import time
2930
import json
3031
import os
@@ -183,6 +184,19 @@ def register(self, app, options):
183184
from .workspaces import blueprint as module
184185
self.submodules.append(module)
185186

187+
def autovacuum_sessions():
188+
try:
189+
with app.app_context():
190+
cleanup_session_files()
191+
finally:
192+
# repeat every five minutes until exit
193+
# https://github.com/python/cpython/issues/98230
194+
t = threading.Timer(5 * 60, autovacuum_sessions)
195+
t.daemon = True
196+
t.start()
197+
198+
app.register_before_app_start(autovacuum_sessions)
199+
186200
super().register(app, options)
187201

188202

@@ -213,8 +227,6 @@ def ping():
213227
@pgCSRFProtect.exempt
214228
def cleanup():
215229
driver.ping()
216-
# Cleanup session files.
217-
cleanup_session_files()
218230
return ""
219231

220232

web/setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from pgadmin import create_app
4343
from pgadmin.utils import clear_database_servers, dump_database_servers, \
4444
load_database_servers, _handle_error
45+
from pgadmin.utils.session import cleanup_session_files
4546
from pgadmin.setup import db_upgrade, create_app_data_directory
4647
from typing import Optional, List
4748
from typing_extensions import Annotated
@@ -636,6 +637,16 @@ def run_migration_for_others():
636637
run_migration_for_sqlite()
637638

638639

640+
class ManageSessions:
641+
642+
@app.command()
643+
def cleanup_session_files():
644+
"""Delete expired session files."""
645+
app = create_app(config.APP_NAME + '-cli')
646+
with app.app_context():
647+
cleanup_session_files()
648+
649+
639650
def main():
640651
app()
641652

0 commit comments

Comments
 (0)