Example #1
0
    def delete(self, task_id):
        """Revoke task
        ---
        tags: [tasks]
        parameters:
          - in: path
            name: task_id
            description: "task id"
            schema:
                type: string
            required: true
        responses:
            204:
                description: task revoked
                content:
                    text/plain:
                        schema:
                            type: string
        """

        try:
            task = get_task_by_id(task_id)
        except:
            return 'No such task', 404

        try:
            celery.control.revoke(task_id, terminate=True)
        except Exception as err:
            return 'We failed to revoke the task', 500

        # We have a valid task in celery, we need to find the task in sql
        # try:
        # Update its stored status to deleted
        save_revoked_task_result(task_id)

        # If it's a ranker task, we will find it and delete it
        # If it's a builder task, let's just let it go
        task = get_task_by_id(task_id)
        request_url = ''
        if task['type'] == TASK_TYPES['answer']:
            request_url = f"http://{os.environ['RANKER_HOST']}:{os.environ['RANKER_PORT']}/api/task/{task['remote_task_id']}"
            response = requests.delete(request_url)
            # We could check the reponses, but there is nothing we would tell the user either way
            # elif task['type'] == TASK_TYPES['update']:
            #     request_url = f"http://{os.environ['BUILDER_HOST']}:{os.environ['BUILDER_PORT']}/api/task/{task['remote_task_id']}/log"
            # else:
            #     raise Exception('Invalid task type')
        # except:
        #     return 'Task not found', 404

        return '', 204
Example #2
0
    def get(self, task_id):
        """Get status for task
        ---
        tags: [tasks]
        parameters:
          - in: path
            name: task_id
            description: "task id"
            schema:
                type: string
            required: true
        responses:
            200:
                description: task
                content:
                    application/json:
                        schema:
                            $ref: '#/definitions/Task'
        """

        try:
            task = get_task_by_id(task_id)
        except:
            return 'Task not found', 404

        return task
Example #3
0
    def get(self, task_id):
        """
        Get activity log for a task and logs of remote tasks associated with the task.
        ---
        tags: [tasks]
        parameters:
          - in: path
            name: task_id
            description: ID of task
            schema:
                type: string
            required: true
        responses:
            200:
                description: text
        """
        try:
            task = get_task_by_id(task_id)
        except KeyError:
            return 'Task ID not found', 404

        try:
            request_url = ''
            if task['type'] == TASK_TYPES['answer']:
                request_url = f"http://{os.environ['RANKER_HOST']}:{os.environ['RANKER_PORT']}/api/task/{task['remote_task_id']}/log"
            elif task['type'] == TASK_TYPES['update']:
                request_url = f"http://{os.environ['BUILDER_HOST']}:{os.environ['BUILDER_PORT']}/api/task/{task['remote_task_id']}/log"
            else:
                raise Exception('Invalid task type')
            response = requests.get(request_url)
            remote_log = response.content.decode('utf-8')
            remote_log = remote_log.replace(
                '\\n',
                '\n')  # Undo new line escaping, else it will happen twice
            remote_log = remote_log.replace('\\\\', '\\')  # Undo slashing
            remote_log = remote_log.replace('\\"',
                                            '"')  # Undo quotation escaping

            # Remove surrounding quotes
            first_quote = remote_log.find('"')
            last_quote = remote_log.rfind('"')
            if first_quote >= 0 and last_quote >= 0:
                remote_log = remote_log[(first_quote + 1):last_quote]
        except:
            remote_log = 'Error fetching log file.'

        try:
            local_log_file = os.path.join(os.environ['ROBOKOP_HOME'], 'logs',
                                          'manager_task_logs',
                                          f'{task_id}.log')
            if os.path.isfile(local_log_file):
                with open(local_log_file, 'r') as log_file:
                    local_log = log_file.read()
            else:
                local_log = ''
        except:
            local_log = 'Error fetching log file.'

        result = {
            'manager_task_id': task['id'],
            'remote_task_id': task['remote_task_id'],
            'task_type': task['type'],
            'task_log': local_log,
            'remote_task_log': remote_log
        }
        return result, 200