import logging
from collections import namedtuple

from django.conf import settings
from django.db import IntegrityError

import six
from celery.result import GroupResult

from corehq.util.metrics import metrics_counter
from soil.exceptions import TaskFailedError

TaskProgress = namedtuple('TaskProgress',
                          ['current', 'total', 'percent', 'error', 'error_message'])


class STATES(object):
    missing = -1
    not_started = 0
    started = 1
    success = 2
    failed = 3


class TaskStatus(namedtuple('TaskStatus', ['result', 'error', 'state', 'progress'])):
    def missing(self):
        return self.state == STATES.missing

    def not_started(self):
        return self.state == STATES.not_started

    def started(self):
        return self.state == STATES.started

    def success(self):