Exemple #1
0
    def launch_next_process(self):
        """Look at the queue of async process, if the queue is not empty launch the next pending request.
        """
        try:
            LOGGER.debug("Checking for stored requests")

            stored_request = dblog.pop_first_stored()
            if not stored_request:
                LOGGER.debug("No stored request found")
                return

            (uuid, request_json) = (stored_request.uuid, stored_request.request)
            if not PY2:
                request_json = request_json.decode('utf-8')
            LOGGER.debug("Launching the stored request {}".format(str(uuid)))
            new_wps_request = WPSRequest()
            new_wps_request.json = json.loads(request_json)
            process_identifier = new_wps_request.identifier
            process = self.service.prepare_process_for_execution(process_identifier)
            process._set_uuid(uuid)
            process.async_ = True
            new_wps_response = ExecuteResponse(new_wps_request, process=process, uuid=uuid)
            new_wps_response.store_status_file = True
            process._run_async(new_wps_request, new_wps_response)
        except Exception as e:
            LOGGER.exception("Could not run stored process. {}".format(e))
Exemple #2
0
    def launch_next_process(self):
        """Look at the queue of async process, if the queue is not empty launch the next pending request.
        """
        try:
            LOGGER.debug("Checking for stored requests")

            stored_request = dblog.pop_first_stored()
            if not stored_request:
                LOGGER.debug("No stored request found")
                return

            (uuid, request_json) = (stored_request.uuid,
                                    stored_request.request)
            if not PY2:
                request_json = request_json.decode('utf-8')
            LOGGER.debug("Launching the stored request {}".format(str(uuid)))
            new_wps_request = WPSRequest()
            new_wps_request.json = json.loads(request_json)
            process_identifier = new_wps_request.identifier
            process = self.service.prepare_process_for_execution(
                process_identifier)
            process._set_uuid(uuid)
            process.async_ = True
            new_wps_response = ExecuteResponse(new_wps_request,
                                               process=process,
                                               uuid=uuid)
            new_wps_response.store_status_file = True
            process._run_async(new_wps_request, new_wps_response)
        except Exception as e:
            LOGGER.exception("Could not run stored process. {}".format(e))
Exemple #3
0
    def _run_process(self, wps_request, wps_response):
        #added because of http://stackoverflow.com/questions/30241911/psycopg2-error-databaseerror-error-with-no-message-from-the-libpq
        #db.get_engine(application).dispose()

        try:
            self._set_grass()
            wps_response = self.handler(wps_request, wps_response)

            # if status not yet set to 100% then do it after execution was successful
            if (not wps_response.status_percentage) or (wps_response.status_percentage != 100):
                LOGGER.debug('Updating process status to 100% if everything went correctly')
                wps_response.update_status('PyWPS Process finished', 100, wps_response_status.DONE_STATUS)
        except Exception as e:
            traceback.print_exc()
            LOGGER.debug('Retrieving file and line number where exception occurred')
            exc_type, exc_obj, exc_tb = sys.exc_info()
            found = False
            while not found:
                # search for the _handler method
                m_name = exc_tb.tb_frame.f_code.co_name
                if m_name == '_handler':
                    found = True
                else:
                    if exc_tb.tb_next is not None:
                        exc_tb = exc_tb.tb_next
                    else:
                        # if not found then take the first
                        exc_tb = sys.exc_info()[2]
                        break
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            method_name = exc_tb.tb_frame.f_code.co_name

            # update the process status to display process failed
            msg = 'Process error: %s.%s Line %i %s' % (fname, method_name, exc_tb.tb_lineno, e)
            LOGGER.error(msg)

            if not wps_response:
                raise NoApplicableCode('Response is empty. Make sure the _handler method is returning a valid object.')
            else:
                wps_response.update_status(msg, -1)

        # tr
        stored_requests = dblog.get_first_stored()
        if stored_requests:
            (uuid, request_json) = stored_requests
            new_wps_request = WPSRequest()
            new_wps_request.json = json.loads(request_json)
            new_wps_response = WPSResponse(self, new_wps_request, uuid)
            new_wps_response.status = wps_response_status.STORE_AND_UPDATE_STATUS
            self._set_uuid(uuid)
            self._run_async(new_wps_request, new_wps_response)
            dblog.remove_stored(uuid)

        return wps_response
Exemple #4
0
    def _run_process(self, wps_request, wps_response):
        try:
            self._set_grass()
            wps_response.update_status('PyWPS Process started', 0)
            wps_response = self.handler(wps_request, wps_response)

            if (not wps_response.status_percentage) or (wps_response.status_percentage != 100):
                LOGGER.debug('Updating process status to 100% if everything went correctly')
                wps_response.update_status('PyWPS Process finished', 100,
                        STATUS.DONE_STATUS, clean=self.async)
        except Exception as e:
            traceback.print_exc()
            LOGGER.debug('Retrieving file and line number where exception occurred')
            exc_type, exc_obj, exc_tb = sys.exc_info()
            found = False
            while not found:
                # search for the _handler method
                m_name = exc_tb.tb_frame.f_code.co_name
                if m_name == '_handler':
                    found = True
                else:
                    if exc_tb.tb_next is not None:
                        exc_tb = exc_tb.tb_next
                    else:
                        # if not found then take the first
                        exc_tb = sys.exc_info()[2]
                        break
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            method_name = exc_tb.tb_frame.f_code.co_name

            # update the process status to display process failed
            msg = 'Process error: %s.%s Line %i %s' % (fname, method_name, exc_tb.tb_lineno, e)
            LOGGER.error(msg)

            if not wps_response:
                raise NoApplicableCode('Response is empty. Make sure the _handler method is returning a valid object.')
            else:
                wps_response.update_status(msg, -1)

        # tr
        stored_requests = dblog.get_first_stored()
        if len(stored_requests) > 0:
            (uuid, request_json) = stored_requests[0]
            new_wps_request = WPSRequest()
            new_wps_request.json = json.loads(request_json)
            new_wps_response = WPSResponse(self, new_wps_request, uuid)
            new_wps_response.status = STATUS.STORE_AND_UPDATE_STATUS
            self._set_uuid(uuid)
            self._run_async(new_wps_request, new_wps_response)
            dblog.remove_stored(uuid)


        return wps_response
Exemple #5
0
    def _run_process(self, wps_request, wps_response):
        try:
            self._set_grass()
            wps_response.update_status('PyWPS Process started', 0)
            wps_response = self.handler(wps_request, wps_response)

            # if (not wps_response.status_percentage) or (wps_response.status_percentage != 100):
            LOGGER.debug('Updating process status to 100% if everything went correctly')
            wps_response.update_status('PyWPS Process {} finished'.format(self.title),
                                       100, STATUS.DONE_STATUS, clean=self.async)
        except Exception as e:
            traceback.print_exc()
            LOGGER.debug('Retrieving file and line number where exception occurred')
            exc_type, exc_obj, exc_tb = sys.exc_info()
            found = False
            while not found:
                # search for the _handler method
                m_name = exc_tb.tb_frame.f_code.co_name
                if m_name == '_handler':
                    found = True
                else:
                    if exc_tb.tb_next is not None:
                        exc_tb = exc_tb.tb_next
                    else:
                        # if not found then take the first
                        exc_tb = sys.exc_info()[2]
                        break
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            method_name = exc_tb.tb_frame.f_code.co_name

            # update the process status to display process failed
            msg = 'Process error: %s.%s Line %i %s' % (fname, method_name, exc_tb.tb_lineno, e)
            LOGGER.error(msg)

            if not wps_response:
                raise NoApplicableCode('Response is empty. Make sure the _handler method is returning a valid object.')
            else:
                wps_response.update_status(msg, -1)

        # tr
        stored_request = dblog.get_first_stored()
        if stored_request:
            (uuid, request_json) = (stored_request.uuid, stored_request.request)
            new_wps_request = WPSRequest()
            new_wps_request.json = json.loads(request_json)
            new_wps_response = WPSResponse(self, new_wps_request, uuid)
            new_wps_response.status = STATUS.STORE_AND_UPDATE_STATUS
            self._set_uuid(uuid)
            self._run_async(new_wps_request, new_wps_response)
            dblog.remove_stored(uuid)

        return wps_response
Exemple #6
0
    def _run_process(self, wps_request, wps_response):
        try:
            wps_response = self.handler(wps_request, wps_response)

            # if status not yet set to 100% then do it after execution was successful
            if wps_response.status_percentage != 100:
                LOGGER.debug(
                    'Updating process status to 100% if everything went correctly'
                )
                wps_response.update_status('PyWPS Process finished', 100)
        except Exception as e:
            traceback.print_exc()
            LOGGER.debug(
                'Retrieving file and line number where exception occurred')
            exc_type, exc_obj, exc_tb = sys.exc_info()
            found = False
            while not found:
                # search for the _handler method
                m_name = exc_tb.tb_frame.f_code.co_name
                if m_name == '_handler':
                    found = True
                else:
                    if exc_tb.tb_next is not None:
                        exc_tb = exc_tb.tb_next
                    else:
                        # if not found then take the first
                        exc_tb = sys.exc_info()[2]
                        break
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            method_name = exc_tb.tb_frame.f_code.co_name

            # update the process status to display process failed
            msg = 'Process error: %s.%s Line %i %s' % (fname, method_name,
                                                       exc_tb.tb_lineno, e)
            LOGGER.error(msg)
            wps_response.update_status(msg, -1)

        # tr
        stored_requests = dblog.get_first_stored()
        if len(stored_requests) > 0:
            (uuid, request_json) = stored_requests[0]
            new_wps_request = WPSRequest()
            new_wps_request.json = json.loads(request_json)
            new_wps_response = WPSResponse(self, new_wps_request, uuid)
            new_wps_response.status = WPSResponse.STORE_AND_UPDATE_STATUS
            self._set_uuid(uuid)
            self._run_async(new_wps_request, new_wps_response)
            dblog.remove_stored(uuid)

        return wps_response
Exemple #7
0
    def call(self, http_request):

        try:
            # This try block handle Exception generated before the request is accepted. Once the request is accepted
            # a valid wps_reponse must exist. To report error use the wps_response using
            # wps_response._update_status(WPS_STATUS.FAILED, ...).
            #
            # We need this behaviour to handle the status file correctly, once the request is accepted, a
            # status file may be created and failure must be reported in this file instead of a raw ows:ExceptionReport
            #
            # Exeception from CapabilityResponse and DescribeResponse are always catched by this try ... except close
            # because they never have status.

            request_uuid = uuid.uuid1()

            environ_cfg = http_request.environ.get('PYWPS_CFG')
            if 'PYWPS_CFG' not in os.environ and environ_cfg:
                LOGGER.debug('Setting PYWPS_CFG to %s', environ_cfg)
                os.environ['PYWPS_CFG'] = environ_cfg

            wps_request = WPSRequest(http_request)
            LOGGER.info('Request: %s', wps_request.operation)
            if wps_request.operation in [
                    'getcapabilities', 'describeprocess', 'execute'
            ]:
                log_request(request_uuid, wps_request)
                try:
                    response = None
                    if wps_request.operation == 'getcapabilities':
                        response = self.get_capabilities(
                            wps_request, request_uuid)
                        response._update_status(WPS_STATUS.SUCCEEDED, u'', 100)

                    elif wps_request.operation == 'describeprocess':
                        response = self.describe(wps_request, request_uuid,
                                                 wps_request.identifiers)
                        response._update_status(WPS_STATUS.SUCCEEDED, u'', 100)

                    elif wps_request.operation == 'execute':
                        response = self.execute(wps_request.identifier,
                                                wps_request, request_uuid)
                    return response
                except Exception as e:
                    # This ensure that logged request get terminated in case of exception while the request is not
                    # accepted
                    store_status(request_uuid, WPS_STATUS.FAILED,
                                 u'Request rejected due to exception', 100)
                    raise e
            else:
                raise RuntimeError("Unknown operation %r" %
                                   wps_request.operation)

        except NoApplicableCode as e:
            return e
        except HTTPException as e:
            return NoApplicableCode(e.description, code=e.code)
        except Exception as e:
            msg = "No applicable error code, please check error log."
            return NoApplicableCode(msg, code=500)
Exemple #8
0
    def __call__(self, http_request):

        request_uuid = uuid.uuid1()

        environ_cfg = http_request.environ.get('PYWPS_CFG')
        if 'PYWPS_CFG' not in os.environ and environ_cfg:
            LOGGER.debug('Setting PYWPS_CFG to %s', environ_cfg)
            os.environ['PYWPS_CFG'] = environ_cfg

        try:
            wps_request = WPSRequest(http_request)
            LOGGER.info('Request: %s', wps_request.operation)
            if wps_request.operation in [
                    'getcapabilities', 'describeprocess', 'execute'
            ]:
                log_request(request_uuid, wps_request)
                response = None
                if wps_request.operation == 'getcapabilities':

                    response = self.get_capabilities(wps_request, request_uuid)

                elif wps_request.operation == 'describeprocess':
                    response = self.describe(wps_request, request_uuid,
                                             wps_request.identifiers)

                elif wps_request.operation == 'execute':
                    response = self.execute(wps_request.identifier,
                                            wps_request, request_uuid)
                update_response(request_uuid, response, close=True)
                return response
            else:
                update_response(request_uuid, response, close=True)
                raise RuntimeError("Unknown operation %r" %
                                   wps_request.operation)

        except HTTPException as e:
            # transform HTTPException to OWS NoApplicableCode exception
            if not isinstance(e, NoApplicableCode):
                e = NoApplicableCode(e.description, code=e.code)

            class FakeResponse:
                message = e.locator
                status = e.code
                status_percentage = 100

            try:
                update_response(request_uuid, FakeResponse, close=True)
            except NoApplicableCode as e:
                return e
            return e
        except Exception as e:
            e = NoApplicableCode(
                "No applicable error code, please check error log", code=500)
            return e
Exemple #9
0
def run_process(identifier, wps_request_json):

    processes = ProcessesGateway(PROCESSES_GATEWAY_HOST,
                                 PROCESSES_GATEWAY_USER,
                                 PROCESSES_GATEWAY_PASS,
                                 PROCESSES_GATEWAY_TIMEOUT,
                                 PROCESSES_GATEWAY_DIRECTORY)

    request = WPSRequest()
    request.json = wps_request_json
    request.status = 'false'

    logging.info('WPS request: {}'.format(dumps(wps_request_json)))

    with processes.get_process_context(
            identifier, LOCAL_PROCESSES_REPOSITORY) as process_context:
        service = Service([process_context.get_process_instance()],
                          CONFIGURATION_FILE)
        response = service.processes.get(identifier).execute(request, uuid1())
        outputs = OutputsSerializer.to_json(response.outputs)

    logging.info('Process outputs: {}'.format(dumps(outputs)))

    return dict(process=identifier, outputs=outputs)
Exemple #10
0
    def __call__(self, http_request):
        try:
            wps_request = WPSRequest(http_request)
            if wps_request.operation == 'getcapabilities':
                return self.get_capabilities()

            elif wps_request.operation == 'describeprocess':
                return self.describe(wps_request.identifiers)

            elif wps_request.operation == 'execute':
                return self.execute(wps_request.identifier, wps_request)
            else:
                raise RuntimeError("Unknown operation %r"
                                   % wps_request.operation)

        except HTTPException as e:
            # transform HTTPException to OWS NoApplicableCode exception
            if not isinstance(e, NoApplicableCode):
                e = NoApplicableCode(e.description, code=e.code)
            return e
Exemple #11
0
    def _run_process(self, wps_request, wps_response):
        try:
            self._set_grass(wps_request)
            # if required set HOME to the current working directory.
            if config.get_config_value('server', 'sethomedir') is True:
                os.environ['HOME'] = self.workdir
                LOGGER.info('Setting HOME to current working directory: %s',
                            os.environ['HOME'])
            LOGGER.debug('ProcessID=%s, HOME=%s', self.uuid,
                         os.environ.get('HOME'))
            wps_response.update_status('PyWPS Process started', 0)
            wps_response = self.handler(wps_request, wps_response)

            # if (not wps_response.status_percentage) or (wps_response.status_percentage != 100):
            LOGGER.debug(
                'Updating process status to 100% if everything went correctly')
            wps_response.update_status('PyWPS Process {} finished'.format(
                self.title),
                                       100,
                                       STATUS.DONE_STATUS,
                                       clean=self. async)
        except Exception as e:
            traceback.print_exc()
            LOGGER.debug(
                'Retrieving file and line number where exception occurred')
            exc_type, exc_obj, exc_tb = sys.exc_info()
            found = False
            while not found:
                # search for the _handler method
                m_name = exc_tb.tb_frame.f_code.co_name
                if m_name == '_handler':
                    found = True
                else:
                    if exc_tb.tb_next is not None:
                        exc_tb = exc_tb.tb_next
                    else:
                        # if not found then take the first
                        exc_tb = sys.exc_info()[2]
                        break
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            method_name = exc_tb.tb_frame.f_code.co_name

            # update the process status to display process failed
            msg = 'Process error: %s.%s Line %i %s' % (fname, method_name,
                                                       exc_tb.tb_lineno, e)
            LOGGER.error(msg)

            if not wps_response:
                raise NoApplicableCode(
                    'Response is empty. Make sure the _handler method is returning a valid object.'
                )
            elif wps_request.raw:
                raise
            else:
                wps_response.update_status(msg, -1, status=STATUS.ERROR_STATUS)

        # tr
        stored_request = dblog.get_first_stored()
        if stored_request:
            try:
                (uuid, request_json) = (stored_request.uuid,
                                        stored_request.request)
                if not PY2:
                    request_json = request_json.decode('utf-8')
                new_wps_request = WPSRequest()
                new_wps_request.json = json.loads(request_json)
                process_identifier = new_wps_request.identifier
                process = self.service.prepare_process_for_execution(
                    process_identifier)
                process._set_uuid(uuid)
                process. async = True
                response_cls = get_response("execute")
                new_wps_response = response_cls(new_wps_request,
                                                process=process,
                                                uuid=uuid)
                new_wps_response.status = STATUS.STORE_AND_UPDATE_STATUS
                process._run_async(new_wps_request, new_wps_response)
                dblog.remove_stored(uuid)
            except Exception as e:
                LOGGER.error("Could not run stored process. %s", e)

        return wps_response
Exemple #12
0
    def _run_process(self, wps_request, wps_response):
        try:
            self._set_grass()
            # if required set HOME to the current working directory.
            if config.get_config_value('server', 'sethomedir') is True:
                os.environ['HOME'] = self.workdir
                LOGGER.info('Setting HOME to current working directory: %s', os.environ['HOME'])
            LOGGER.debug('ProcessID=%s, HOME=%s', self.uuid, os.environ.get('HOME'))
            wps_response.update_status('PyWPS Process started', 0)
            wps_response = self.handler(wps_request, wps_response)

            # if (not wps_response.status_percentage) or (wps_response.status_percentage != 100):
            LOGGER.debug('Updating process status to 100% if everything went correctly')
            wps_response.update_status('PyWPS Process {} finished'.format(self.title),
                                       100, STATUS.DONE_STATUS, clean=self.async)
        except Exception as e:
            traceback.print_exc()
            LOGGER.debug('Retrieving file and line number where exception occurred')
            exc_type, exc_obj, exc_tb = sys.exc_info()
            found = False
            while not found:
                # search for the _handler method
                m_name = exc_tb.tb_frame.f_code.co_name
                if m_name == '_handler':
                    found = True
                else:
                    if exc_tb.tb_next is not None:
                        exc_tb = exc_tb.tb_next
                    else:
                        # if not found then take the first
                        exc_tb = sys.exc_info()[2]
                        break
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            method_name = exc_tb.tb_frame.f_code.co_name

            # update the process status to display process failed
            msg = 'Process error: %s.%s Line %i %s' % (fname, method_name, exc_tb.tb_lineno, e)
            LOGGER.error(msg)

            if not wps_response:
                raise NoApplicableCode('Response is empty. Make sure the _handler method is returning a valid object.')
            elif wps_request.raw:
                raise
            else:
                wps_response.update_status(msg, -1, status=STATUS.ERROR_STATUS)

        # tr
        stored_request = dblog.get_first_stored()
        if stored_request:
            try:
                (uuid, request_json) = (stored_request.uuid, stored_request.request)
                if not PY2:
                    request_json = request_json.decode('utf-8')
                new_wps_request = WPSRequest()
                new_wps_request.json = json.loads(request_json)
                process_identifier = new_wps_request.identifier
                process = self.service.prepare_process_for_execution(process_identifier)
                process._set_uuid(uuid)
                process.async = True
                response_cls = get_response("execute")
                new_wps_response = response_cls(new_wps_request, process=process, uuid=uuid)
                new_wps_response.status = STATUS.STORE_AND_UPDATE_STATUS
                process._run_async(new_wps_request, new_wps_response)
                dblog.remove_stored(uuid)
            except Exception as e:
                LOGGER.error("Could not run stored process. %s", e)

        return wps_response
Exemple #13
0
    def _run_process(self, wps_request, wps_response):
        #added because of http://stackoverflow.com/questions/30241911/psycopg2-error-databaseerror-error-with-no-message-from-the-libpq
        #db.get_engine(application).dispose()

        try:
            self._set_grass()
            wps_response = self.handler(wps_request, wps_response)

            # if status not yet set to 100% then do it after execution was successful
            if (not wps_response.status_percentage) or (
                    wps_response.status_percentage != 100):
                LOGGER.debug(
                    'Updating process status to 100% if everything went correctly'
                )
                wps_response.update_status('PyWPS Process finished', 100,
                                           wps_response_status.DONE_STATUS)
        except Exception as e:
            traceback.print_exc()
            LOGGER.debug(
                'Retrieving file and line number where exception occurred')
            exc_type, exc_obj, exc_tb = sys.exc_info()
            found = False
            while not found:
                # search for the _handler method
                m_name = exc_tb.tb_frame.f_code.co_name
                if m_name == '_handler':
                    found = True
                else:
                    if exc_tb.tb_next is not None:
                        exc_tb = exc_tb.tb_next
                    else:
                        # if not found then take the first
                        exc_tb = sys.exc_info()[2]
                        break
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            method_name = exc_tb.tb_frame.f_code.co_name

            # update the process status to display process failed
            msg = 'Process error: %s.%s Line %i %s' % (fname, method_name,
                                                       exc_tb.tb_lineno, e)
            LOGGER.error(msg)

            if not wps_response:
                raise NoApplicableCode(
                    'Response is empty. Make sure the _handler method is returning a valid object.'
                )
            else:
                wps_response.update_status(msg, -1)

        # tr
        stored_requests = dblog.get_first_stored()
        if stored_requests:
            (uuid, request_json) = stored_requests
            new_wps_request = WPSRequest()
            new_wps_request.json = json.loads(request_json)
            new_wps_response = WPSResponse(self, new_wps_request, uuid)
            new_wps_response.status = wps_response_status.STORE_AND_UPDATE_STATUS
            self._set_uuid(uuid)
            self._run_async(new_wps_request, new_wps_response)
            dblog.remove_stored(uuid)

        return wps_response