This function will iterate through session directory and check the last modified time, if it older than (session expiration time + 1) days then delete that file.
()
| 531 | |
| 532 | |
| 533 | def cleanup_session_files(): |
| 534 | """ |
| 535 | This function will iterate through session directory and check the last |
| 536 | modified time, if it older than (session expiration time + 1) days then |
| 537 | delete that file. |
| 538 | """ |
| 539 | iterate_session_files = False |
| 540 | |
| 541 | global LAST_CHECK_SESSION_FILES |
| 542 | if LAST_CHECK_SESSION_FILES is None or \ |
| 543 | datetime.datetime.now() >= LAST_CHECK_SESSION_FILES + \ |
| 544 | datetime.timedelta(hours=config.CHECK_SESSION_FILES_INTERVAL): |
| 545 | iterate_session_files = True |
| 546 | LAST_CHECK_SESSION_FILES = datetime.datetime.now() |
| 547 | |
| 548 | if iterate_session_files: |
| 549 | for root, dirs, files in os.walk( |
| 550 | current_app.config['SESSION_DB_PATH']): |
| 551 | for file_name in files: |
| 552 | absolute_file_name = os.path.join(root, file_name) |
| 553 | st = os.stat(absolute_file_name) |
| 554 | |
| 555 | # Get the last modified time of the session file |
| 556 | last_modified_time = \ |
| 557 | datetime.datetime.fromtimestamp(st.st_mtime) |
| 558 | |
| 559 | # Calculate session file expiry time. |
| 560 | file_expiration_time = \ |
| 561 | last_modified_time + \ |
| 562 | current_app.permanent_session_lifetime + \ |
| 563 | datetime.timedelta(days=1) |
| 564 | |
| 565 | if file_expiration_time <= datetime.datetime.now() and \ |
| 566 | os.path.exists(absolute_file_name): |
| 567 | os.unlink(absolute_file_name) |
no test coverage detected