async def error_handler_middleware(request: Request, handler: RequestHandler) -> StreamResponse: try: return await handler(request) except HTTPRedirection as e: # redirects are implemented as exceptions in aiohttp for whatever reason... raise e except NotFoundError as e: kind = type(e).__name__ message = f"Error: {kind}\nMessage: {str(e)}" log.info(f"Request {request} has failed with exception: {message}", exc_info=exc_info(e)) raise HTTPNotFound(text=message) from e except (ClientError, AttributeError) as e: kind = type(e).__name__ ex_str = str(e) message = f"Error: {kind}\nMessage: {ex_str}" log.info(f"Request {request} has failed with exception: {message}", exc_info=exc_info(e)) await event_sender.core_event( CoreEvent.ClientError, {"version": version(), "kind": kind, "message": ex_str} ) raise HTTPBadRequest(text=message) from e except Exception as e: kind = type(e).__name__ ex_str = str(e) message = f"Error: {kind}\nMessage: {ex_str}" log.warning(f"Request {request} has failed with exception: {message}", exc_info=exc_info(e)) await event_sender.core_event( CoreEvent.ServerError, {"version": version(), "kind": kind, "message": ex_str} ) raise HTTPBadRequest(text=message) from e
async def emit_events() -> None: # information about the model model = await model_handler.load_model() await event_sender.core_event( CoreEvent.ModelInfo, dict(version=version()), model_count=len(model.kinds), ) # information about all subscribers/actors subscribers = await subscription_handler.all_subscribers() await event_sender.core_event( CoreEvent.SubscriberInfo, subscriber_count=sum(1 for _ in subscribers), # do not count wildcard listeners active=sum(1 for channels in message_bus.active_listener.values() if channels != ["*"]), ) # information about all workers await event_sender.core_event( CoreEvent.WorkerQueueInfo, worker_count=len(worker_task_queue.work_count), worker_tasks_count=len(worker_task_queue.worker_by_task_name), outstanding_tasks=len(worker_task_queue.outstanding_tasks), unassigned_tasks=len(worker_task_queue.unassigned_tasks), )
async def on_start() -> None: # queue must be created inside an async function! cli_deps.extend(forked_tasks=Queue()) await db.start() await event_sender.start() await subscriptions.start() await scheduler.start() await worker_task_queue.start() await event_emitter.start() await cli.start() await task_handler.start() await api.start() if created: await event_sender.core_event(CoreEvent.SystemInstalled) await event_sender.core_event( CoreEvent.SystemStarted, { "version": version(), "created_at": to_json(system_data.created_at), "system": platform.system(), "platform": platform.platform(), "inside_docker": info.inside_docker, }, cpu_count=info.cpus, mem_total=info.mem_total, mem_available=info.mem_available, )
def system_info() -> SystemInfo: mem = psutil.virtual_memory() return SystemInfo( version=version(), cpus=mp.cpu_count(), mem_available=iec_size_format(mem.available), mem_total=iec_size_format(mem.total), inside_docker=os.path.exists("/.dockerenv"), # this file is created by the docker runtime started_at=started_at, )
def system_info() -> SystemInfo: mem = psutil.virtual_memory() return SystemInfo( version=version(), git_hash=git_hash_from_file() or "n/a", cpus=mp.cpu_count(), mem_available=iec_size_format(mem.available), mem_total=iec_size_format(mem.total), inside_docker=inside_docker(), started_at=started_at, )
async def test_system_info_command(cli: CLI) -> None: info = AccessJson.wrap_object( (await cli.execute_cli_command("system info", stream.list))[0][0]) assert info.version == version() assert info.name == "resotocore" assert info.cpus > 0