'Called when a task is failed, called by `on_task_status`'

def on_task_failed(self, task):

    if ('schedule' not in task):
        old_task = self.taskdb.get_task(task['project'], task['taskid'], fields=['schedule'])
        if (old_task is None):
            logging.error(('unknown    status    pack:    %s' % task))
            return
        task['schedule'] = old_task.get('schedule', {})
    retries = task['schedule'].get('retries', self.default_schedule['retries'])
    retried = task['schedule'].get('retried', 0)
    project_info = self.projects[task['project']]
    retry_delay = (project_info.retry_delay or self.DEFAULT_RETRY_DELAY)
    next_exetime = retry_delay.get(retried, retry_delay.get('', self.DEFAULT_RETRY_DELAY['']))
    if (task['schedule'].get('auto_recrawl') and ('age' in task['schedule'])):
        next_exetime = min(next_exetime, task['schedule'].get('age'))
    elif (retried >= retries):
        next_exetime = (-1)
    elif (('age' in task['schedule']) and (next_exetime > task['schedule'].get('age'))):
        next_exetime = task['schedule'].get('age')
    if (next_exetime < 0):
        task['status'] = self.taskdb.FAILED
        task['lastcrawltime'] = time.time()
        self.update_task(task)
        project = task['project']
        self._cnt['5m'].event((project, 'failed'), (+ 1))
        self._cnt['1h'].event((project, 'failed'), (+ 1))
        self._cnt['1d'].event((project, 'failed'), (+ 1))
        self._cnt['all'].event((project, 'failed'), (+ 1)).event((project, 'pending'), (-1))
        logger.info(('task    failed    %(project)s:%(taskid)s    %(url)s' % task))
        return task
    else:
        task['schedule']['retried'] = (retried + 1)
        task['schedule']['exetime'] = (time.time() + next_exetime)
        task['lastcrawltime'] = time.time()
        self.update_task(task)
        self.put_task(task)
        project = task['project']
        self._cnt['5m'].event((project, 'retry'), (+ 1))
        self._cnt['1h'].event((project, 'retry'), (+ 1))
        self._cnt['1d'].event((project, 'retry'), (+ 1))
        logger.info(('task    retry    %d/%d    %%(project)s:%%(taskid)s    %%(url)s' % (retried, retries)), task)
        return task
