"""
Clean up the pilots.

Initial delay: rand(60 minute)
Periodic delay: 60 minutes
"""

import logging
import random
import time
from datetime import datetime, timedelta

from tornado.ioloop import IOLoop

from iceprod.server.util import str2datetime

logger = logging.getLogger('pilot_monitor')

def pilot_cleanup(module):
    """
    Initial entrypoint.

    Args:
        module (:py:class:`iceprod.server.modules.schedule`): schedule module
    """
    # initial delay
    IOLoop.current().call_later(random.randint(60,60*60), run,
                                module.rest_client)

async def run(rest_client, debug=False):
    """
    Actual runtime / loop.

    Args:
        rest_client (:py:class:`iceprod.core.rest_client.Client`): rest client
        debug (bool): debug flag to propagate exceptions
    """
    start_time = time.time()
    time_limit = datetime.utcnow() - timedelta(days=7)
    async def reset_pilot(pilot_id):
        await rest_client.request('DELETE', '/pilots/{}'.format(pilot_id))
    try:
        pilots = await rest_client.request('GET', '/pilots')
        for pilot in pilots.values():
            if 'grid_queue_id' not in pilot or not pilot['grid_queue_id']:
                await reset_pilot(pilot['pilot_id'])
            elif 'last_update' not in pilot or str2datetime(pilot['last_update']) < time_limit:
                await reset_pilot(pilot['pilot_id'])
                
    except Exception:
        logger.error('error cleaning pilots', exc_info=True)
        if debug:
            raise

    # run again after 5 minute delay
    stop_time = time.time()
    delay = max(60*60 - (stop_time-start_time), 60)
    IOLoop.current().call_later(delay, run, rest_client)

@@ -1137,138 +1137,8 @@
  if 
-'grid_queue_id' not in pilot or not pilot%5B'grid_queue_id'%5D:%0A                await reset_pilot(pilot%5B'pilot_id'%5D)%0A            elif 
 'las
@@ -1265,32 +1265,16 @@
 t_id'%5D)%0A
-                
 %0A    exc

stop deleting pilots with no grid_queue_id. it interferes with supercomputer mode