<s> from __future__ import absolute_import <EOL> from datetime import datetime , timedelta <EOL> from changes . config import queue <EOL> from changes . constants import Status <EOL> from changes . models import Task <EOL> from changes . queue . task import TrackedTask <EOL> CHECK_TIME = timedelta ( minutes = <NUM_LIT> ) <EOL> EXPIRE_TIME = timedelta ( days = <NUM_LIT:7> ) <EOL> def cleanup_tasks ( ) : <EOL> """<STR_LIT>""" <EOL> now = datetime . utcnow ( ) <EOL> pending_tasks = Task . query . filter ( <EOL> Task . status != Status . finished , <EOL> Task . date_modified < now - CHECK_TIME , <EOL> ) <EOL> for task in pending_tasks : <EOL> task_func = TrackedTask ( queue . get_task ( task . task_name ) ) <EOL> task_func . delay ( <EOL> task_id = task . task_id . hex , <EOL> parent_task_id = task . parent_id . hex if task . parent_id else None , <EOL> ** task . data [ '<STR_LIT>' ] <EOL> ) <EOL> Task . query . filter ( <EOL> Task . status == Status . finished , <EOL> Task . date_modified < now - EXPIRE_TIME , <EOL> ) . delete ( ) </s>
