Esempio n. 1
0
    def _wait_for_results(self) -> RemoteResult:
        """Wait for results off the queue. If there is an exception raised,
        raise an appropriate RPC exception.

        This does not handle joining, but does terminate the process if it
        timed out.
        """
        if (self.subscriber is None or self.started is None
                or self.process is None):
            raise dbt.exceptions.InternalException(
                '_wait_for_results() called before handle()')

        try:
            msg = self.subscriber.dispatch_until_exit(
                started=self.started,
                timeout=self.timeout,
            )
        except dbt.exceptions.Exception as exc:
            raise dbt_error(exc)
        except Exception as exc:
            raise server_error(exc)
        if isinstance(msg, QueueErrorMessage):
            raise RPCException.from_error(msg.error)
        elif isinstance(msg, QueueTimeoutMessage):
            if not self._single_threaded:
                self.process.terminate()
            raise timeout_error(self.timeout)
        elif isinstance(msg, QueueResultMessage):
            return msg.result
        else:
            raise dbt.exceptions.InternalException(
                'Invalid message type {} (result={})'.format(msg))
Esempio n. 2
0
 def handle_exception(self, e, ctx):
     logger.debug('Got an exception: {}'.format(e), exc_info=True)
     if isinstance(e, dbt.exceptions.Exception):
         if isinstance(e, dbt.exceptions.RuntimeException):
             e.add_node(ctx.node)
         return dbt_error(e)
     elif isinstance(e, RPCException):
         return e
     else:
         return server_error(e)
Esempio n. 3
0
 def handle_error(self, exc_type, exc_value, exc_tb) -> bool:
     if isinstance(exc_value, RPCException):
         self.handler.error = exc_value
     elif isinstance(exc_value, dbt.exceptions.Exception):
         self.handler.error = dbt_error(exc_value)
     else:
         # we should only get here if we got a BaseException that is not
         # an Exception (we caught those in _wait_for_results), or a bug
         # in get_result's call stack. Either way, we should set an
         # error so we can figure out what happened on thread death
         self.handler.error = server_error(exc_value)
     if self.handler.state != TaskHandlerState.Killed:
         self.handler.state = TaskHandlerState.Error
     self.set_end()
     return False
Esempio n. 4
0
    def task_exec(self) -> None:
        """task_exec runs first inside the child process"""
        signal.signal(signal.SIGTERM, sigterm_handler)
        # the first thing we do in a new process: push logging back over our
        # queue
        handler = QueueLogHandler(self.queue)
        with handler.applicationbound():
            self._spawn_setup()
            # copy threads over into our credentials, if it exists and is set.
            # some commands, like 'debug', won't have a threads value at all.
            if getattr(self.task.args, 'threads', None) is not None:
                self.task.config.threads = self.task.args.threads
            rpc_exception = None
            result = None
            try:
                result = self.task.handle_request()
            except RPCException as exc:
                rpc_exception = exc
            except dbt.exceptions.RPCKilledException as exc:
                # do NOT log anything here, you risk triggering a deadlock on
                # the queue handler we inserted above
                rpc_exception = dbt_error(exc)
            except dbt.exceptions.Exception as exc:
                logger.debug('dbt runtime exception', exc_info=True)
                rpc_exception = dbt_error(exc)
            except Exception as exc:
                with OutputHandler(sys.stderr).applicationbound():
                    logger.error('uncaught python exception', exc_info=True)
                rpc_exception = server_error(exc)

            # put whatever result we got onto the queue as well.
            if rpc_exception is not None:
                handler.emit_error(rpc_exception.error)
            elif result is not None:
                handler.emit_result(result)
            else:
                error = dbt_error(
                    dbt.exceptions.InternalException(
                        'after request handling, neither result nor error is None!'
                    ))
                handler.emit_error(error.error)