old_contents: from __future__ import absolute_import
  
  from datetime import datetime, timedelta
  
  from changes.config import queue
  from changes.constants import Status
  from changes.experimental.stats import RCount, incr
  from changes.models import Task
  from changes.queue.task import TrackedTask, tracked_task
  
  CHECK_TIME = timedelta(minutes=60)
  
  
  @tracked_task
  def cleanup_tasks():
      with RCount('cleanup_tasks'):
          """
          Find any tasks which haven't checked in within a reasonable time period and
          requeue them if necessary.
          """
          now = datetime.utcnow()
          cutoff = now - CHECK_TIME
  
          pending_tasks = Task.query.filter(
              Task.status != Status.finished,
              Task.date_modified < cutoff,
          )
  
          for task in pending_tasks:
              incr('cleanup_unfinished')
              task_func = TrackedTask(queue.get_task(task.task_name))
              task_func.delay(
                  task_id=task.task_id.hex,
                  parent_task_id=task.parent_id.hex if task.parent_id else None,
                  **task.data['kwargs']
              )
  
new_contents: from __future__ import absolute_import
  
  from datetime import datetime, timedelta
  
  from changes.config import queue
  from changes.constants import Status
  from changes.experimental.stats import RCount, incr
  from changes.models import Task
  from changes.queue.task import TrackedTask, tracked_task
  
  CHECK_TIME = timedelta(minutes=60)
  EXPIRE_TIME = timedelta(days=7)
  
  
  @tracked_task
  def cleanup_tasks():
      """
      Find any tasks which haven't checked in within a reasonable time period and
      requeue them if necessary.
  
      Additionally remove any old Task entries which are completed.
      """
      with RCount('cleanup_tasks'):
          now = datetime.utcnow()
  
          pending_tasks = Task.query.filter(
              Task.status != Status.finished,
              Task.date_modified < now - CHECK_TIME,
          )
  
          for task in pending_tasks:
              incr('cleanup_unfinished')
              task_func = TrackedTask(queue.get_task(task.task_name))
              task_func.delay(
                  task_id=task.task_id.hex,
                  parent_task_id=task.parent_id.hex if task.parent_id else None,
                  **task.data['kwargs']
              )
  
          Task.query.filter(
              Task.status == Status.finished,
              Task.date_modified < now - EXPIRE_TIME,
          ).delete()
  
subject: Remove old task entries as part of cleanup_tasks