🔍 Code Extractor

function cleanup_old_tasks

Maturity: 46

Removes tasks from the active_tasks dictionary that are older than 1 hour (3600 seconds) based on their creation timestamp.

File:
/tf/active/vicechatdev/docchat/app.py
Lines:
208 - 220
Complexity:
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

  • datetime
  • threading
  • logging

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function cleanup_old_tasks_v1 93.6% similar

    Removes tasks from the active_tasks dictionary that are older than 1 hour (3600 seconds) based on their creation timestamp, using thread-safe locking.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function cleanup_old_documents 68.8% similar

    Periodically removes documents and their associated files that are older than 2 hours from the uploaded_documents dictionary, cleaning up both file system storage and memory.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function file_cleanup 57.4% similar

    Removes files older than 60 seconds from the './static/files/' directory.

    From: /tf/active/vicechatdev/datacapture_integrated.py
  • function create_task_v1 56.2% similar

    Creates and registers a new background task entry in a thread-safe manner, initializing its status, progress, user information, and timestamps.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function complete_task 54.2% similar

    Updates the status of a task to 'completed' in a thread-safe manner, storing the result and completion timestamp.

    From: /tf/active/vicechatdev/docchat/app.py
← Back to Browse