<commit_before>import logging

import ee
import time
from ee.batch import Task

from ..task.task import ThreadTask

logger = logging.getLogger(__name__)


class State(object):
    PENDING = 'PENDING'
    ACTIVE = 'ACTIVE'
    COMPLETED = 'COMPLETED'
    FAILED = 'FAILED'


class MonitorEarthEngineExportTask(ThreadTask):
    def __init__(self, credentials, task_id, destination):
        super(MonitorEarthEngineExportTask, self).__init__('MonitorEarthEngineTask')
        self.credentials = credentials
        self.task_id = task_id
        self.destination = destination
        self._status = {
            'state': State.PENDING
        }

    def run(self):
        logger.info('Monitoring Earth Engine export. task_id: {0}, destination: {1}'
                    .format(self.task_id, self.destination))
        ee.InitializeThread(self.credentials)
        while self.running():
            time.sleep(10)
            status = Task(self.task_id).status()
            state = status['state']
            if state == Task.State.READY:
                self._update_state(State.PENDING)
            elif state == Task.State.RUNNING:
                self._update_state(State.ACTIVE)
            elif state == Task.State.COMPLETED:
                self._update_state(State.COMPLETED)
                self.resolve()
                return
            else:
                error_message = status['error_message']
                self._status = {
                    'state': State.FAILED,
                    'message': error_message
                }
                raise Exception(
                    'Earth Engine export failed: {}'.format(error_message)
                    if error_message
                    else 'Earth Engine export failed'
                )

    def status(self):
        return self._status

    def status_message(self):
        state = self.status()['state']
        if state == State.PENDING:<commit_msg>Improved error message when GEE task has been canceled.<commit_after>import logging

import ee
import time
from ee.batch import Task

from ..task.task import ThreadTask

logger = logging.getLogger(__name__)


class State(object):
    PENDING = 'PENDING'
    ACTIVE = 'ACTIVE'
    COMPLETED = 'COMPLETED'
    FAILED = 'FAILED'


class MonitorEarthEngineExportTask(ThreadTask):
    def __init__(self, credentials, task_id, destination):
        super(MonitorEarthEngineExportTask, self).__init__('MonitorEarthEngineTask')
        self.credentials = credentials
        self.task_id = task_id
        self.destination = destination
        self._status = {
            'state': State.PENDING
        }

    def run(self):
        logger.info('Monitoring Earth Engine export. task_id: {0}, destination: {1}'
                    .format(self.task_id, self.destination))
        ee.InitializeThread(self.credentials)
        while self.running():
            time.sleep(10)
            status = Task(self.task_id).status()
            state = status['state']
            if state == Task.State.READY:
                self._update_state(State.PENDING)
            elif state == Task.State.RUNNING:
                self._update_state(State.ACTIVE)
            elif state == Task.State.COMPLETED:
                self._update_state(State.COMPLETED)
                self.resolve()
                return
            else:
                error_message = status['error_message'] if state == Task.State.FAILED else 'Task was canceled'
                self._status = {
                    'state': State.FAILED,
                    'message': error_message
                }
                raise Exception(
                    'Earth Engine export failed: {}'.format(error_message)
                    if error_message
                    else 'Earth Engine export failed'
                )

    def status(self):
        return self._status

    def status_message(self):
        state = self.status()['state']
        if state == State.PENDING:
{'fraction_non_alphanumeric': 0.09292369680181314, 'fraction_numerical': 0.002014605892722236, 'mean_word_length': 1.845272206303725, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4554221', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1037, 'n_words': 236}