function cleanup_old_tasks
Removes tasks from the active_tasks dictionary that are older than 1 hour (3600 seconds) based on their creation timestamp.
/tf/active/vicechatdev/docchat/app.py
208 - 220
simple
Purpose
This function performs periodic cleanup of stale tasks to prevent memory leaks and maintain system performance. It iterates through all active tasks, calculates their age, and removes those exceeding the 1-hour threshold. The function uses thread-safe operations with a lock to prevent race conditions in multi-threaded environments. It's typically called periodically by a background scheduler or maintenance thread.
Source Code
def cleanup_old_tasks():
"""Clean up tasks older than 1 hour"""
with task_lock:
current_time = datetime.now()
to_remove = []
for task_id, task in active_tasks.items():
age = current_time - task['created_at']
if age.total_seconds() > 3600:
to_remove.append(task_id)
for task_id in to_remove:
del active_tasks[task_id]
logger.info(f"Cleaned up old task: {task_id}")
Return Value
This function returns None (implicitly). It performs side effects by modifying the global active_tasks dictionary and logging cleanup operations.
Dependencies
datetimethreadinglogging
Required Imports
from datetime import datetime
from threading import Lock
import logging
Usage Example
from datetime import datetime
from threading import Lock
import logging
# Setup required globals
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
task_lock = Lock()
active_tasks = {
'task_1': {'created_at': datetime.now(), 'data': 'some data'},
'task_2': {'created_at': datetime(2023, 1, 1), 'data': 'old data'}
}
# Call the cleanup function
cleanup_old_tasks()
# Result: task_2 will be removed if it's older than 1 hour
print(f"Remaining tasks: {list(active_tasks.keys())}")
Best Practices
- Ensure task_lock is properly initialized as a threading.Lock() before calling this function
- The active_tasks dictionary must contain tasks with a 'created_at' key storing datetime objects
- This function should be called periodically (e.g., every 15-30 minutes) by a background scheduler
- Consider adjusting the 3600 seconds threshold based on your application's requirements
- Ensure proper logging configuration is in place to capture cleanup events
- Do not call this function too frequently as it iterates through all active tasks
- The function modifies global state, so ensure proper synchronization in multi-threaded environments
- Monitor the logger output to track cleanup patterns and identify potential issues with task accumulation
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function cleanup_old_tasks_v1 93.6% similar
-
function cleanup_old_documents 68.8% similar
-
function file_cleanup 57.4% similar
-
function create_task_v1 56.2% similar
-
function complete_task 54.2% similar