Example #1
0
async def test_receive_signals() -> None:
    with open_signal_receiver(signal.SIGUSR1, signal.SIGUSR2) as sigiter:
        await to_thread.run_sync(os.kill, os.getpid(), signal.SIGUSR1)
        await to_thread.run_sync(os.kill, os.getpid(), signal.SIGUSR2)
        with fail_after(1):
            assert await sigiter.__anext__() == signal.SIGUSR1
            assert await sigiter.__anext__() == signal.SIGUSR2
Example #2
0
async def test_receive_signals():
    async with open_signal_receiver(signal.SIGUSR1, signal.SIGUSR2) as sigiter:
        await run_sync_in_worker_thread(os.kill, os.getpid(), signal.SIGUSR1)
        await run_sync_in_worker_thread(os.kill, os.getpid(), signal.SIGUSR2)
        async with fail_after(1):
            assert await sigiter.__anext__() == signal.SIGUSR1
            assert await sigiter.__anext__() == signal.SIGUSR2
Example #3
0
    async def signal_handler() -> None:
        with open_signal_receiver(signal.SIGUSR1) as sigiter:
            async for v in sigiter:
                pytest.fail("SIGUSR1 should not be sent")

            pytest.fail("signal_handler should have been cancelled")

        pytest.fail("open_signal_receiver should not suppress cancellation")
Example #4
0
async def test_task_group_cancellation_consume() -> None:
    async def consume(sigiter: AsyncIterable[int]) -> None:
        async for v in sigiter:
            pytest.fail("SIGUSR1 should not be sent")

        pytest.fail("consume should have been cancelled")

    with open_signal_receiver(signal.SIGUSR1) as sigiter:
        async with create_task_group() as tg:
            tg.start_soon(consume, sigiter)
            tg.cancel_scope.cancel()
Example #5
0
    async def _start(self, processors="all"):
        context.worker_id.set(self.id)
        assert processors == "all" or isinstance(processors, list)

        if self.started:
            raise Misconfigured("Worker already running")
        logger.info("starting-worker", processors=processors)

        # Import all modules likely to contain Runnel objects (e.g. processors, tasks)
        # that must be registered with the app.
        autodiscover(self.app)

        # Load lua scripts.
        for script in (Path(__file__).parent / "lua").glob("*.lua"):
            self.app.scripts[script.stem] = self.app.redis.register_script(
                script.read_text())

        # Create executors for all chosen processors.
        for proc in self.app.processors.values():
            if processors == "all" or proc.name in processors:
                await proc.prepare()
                self.executors.add(Executor(id=base64uuid(), processor=proc))

        # First leadership election attempt.
        await self._elect_leader()

        self.app.workers.add(self)
        try:
            async with anyio.open_signal_receiver(signal.SIGINT,
                                                  signal.SIGTERM) as signals:
                async with anyio.create_task_group() as tg:
                    # The main executor tasks.
                    for e in self.executors:
                        await tg.spawn(e.start)

                    # Background tasks, e.g. timers, crontabs etc.
                    for t in self.app.tasks:
                        await tg.spawn(t, self)

                    # Leadership polling.
                    await tg.spawn(self.elect_leader)
                    self.started = True

                    # Allow for graceful shutdown.
                    async for signum in signals:
                        logger.critical("signal-received", signum=signum)
                        await tg.cancel_scope.cancel()
                        return
        finally:
            self.app.workers.remove(self)
            if not self.app.workers:
                self.app.redis.connection_pool.disconnect()
            logger.critical("worker-exit",
                            executor_ids=[e.id for e in self.executors])
Example #6
0
async def _signal_handler(
    ros_node: Node, *, task_status: TaskStatus = anyio.TASK_STATUS_IGNORED
) -> None:
    async with anyio.open_signal_receiver(signal.SIGINT, signal.SIGTERM) as signals:
        task_status.started()
        async for signum in signals:
            if signum == signal.SIGINT:
                print("Ctrl+C pressed!")
            else:
                print("Terminated!")

            ros_node.signal_shutdown()
            return
Example #7
0
async def run(
    xmlrpc_port: int = 0,
    local_address: Optional[str] = None,
    debug: bool = False,
) -> None:
    init_logging(
        ColoredFormatter(
            "%(asctime)s master[%(process)d] %(message)s",
            "%b %d %H:%M:%S",
        ),
        logging.DEBUG if debug else logging.INFO,
    )
    async with init_master(xmlrpc_port, local_address):
        async with anyio.open_signal_receiver(signal.SIGINT,
                                              signal.SIGTERM) as signals:
            async for signum in signals:
                if signum == signal.SIGINT:
                    print("Ctrl+C pressed!")
                else:
                    print("Terminated!")
                break
Example #8
0
 async def test_open_signal_receiver(self) -> None:
     with pytest.deprecated_call():
         async with open_signal_receiver(signal.SIGINT):
             pass
Example #9
0
 async def signal_handler():
     async with open_signal_receiver(signal.SIGUSR1) as sigiter:
         async for v in sigiter:
             assert False