Esempio n. 1
0
def setup_bsp_server(
    rule_runner: RuleRunner | None = None, *, notification_names: set[str] | None = None
):
    rule_runner = rule_runner or RuleRunner(rules=bsp_rules())
    notification_names = notification_names or set()
    stdio_destination = native_engine.stdio_thread_get_destination()

    with setup_pipes() as pipes:
        context = BSPContext()
        rule_runner.set_session_values({BSPContext: context})
        conn = BSPConnection(
            rule_runner.scheduler,
            rule_runner.union_membership,
            context,
            pipes.server_reader,
            pipes.server_writer,
        )

        def run_bsp_server():
            native_engine.stdio_thread_set_destination(stdio_destination)
            conn.run()

        bsp_thread = Thread(target=run_bsp_server)
        bsp_thread.daemon = True
        bsp_thread.start()

        client_reader = JsonRpcStreamReader(pipes.client_reader)
        client_writer = JsonRpcStreamWriter(pipes.client_writer)
        notifications = Notifications([])
        endpoint = Endpoint(
            {name: functools.partial(notifications._record, name) for name in notification_names},
            lambda msg: client_writer.write(msg),
        )

        def run_client():
            client_reader.listen(lambda msg: endpoint.consume(msg))

        client_thread = Thread(target=run_client)
        client_thread.daemon = True
        client_thread.start()

        try:
            yield endpoint, notifications
        finally:
            client_reader.close()
            client_writer.close()
Esempio n. 2
0
 def __init__(
     self,
     scheduler: Any,
     context: StreamingWorkunitContext,
     callbacks: Iterable[WorkunitsCallback],
     report_interval: float,
     max_workunit_verbosity: LogLevel,
     pantsd: bool,
 ) -> None:
     super().__init__(daemon=True)
     self.scheduler = scheduler
     self.context = context
     self.stop_request = threading.Event()
     self.report_interval = report_interval
     self.callbacks = callbacks
     self.max_workunit_verbosity = max_workunit_verbosity
     # TODO: Have a thread per callback so that some callbacks can always finish async even
     #  if others must be finished synchronously.
     self.block_until_complete = not pantsd or any(
         callback.can_finish_async is False for callback in self.callbacks)
     # Get the parent thread's logging destination. Note that this thread has not yet started
     # as we are only in the constructor.
     self.logging_destination = native_engine.stdio_thread_get_destination()