Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: services: helper: Fix periodic loop #1703

Merged
merged 1 commit into from
May 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions core/services/helper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import http
import logging
from enum import Enum
from pathlib import Path
from typing import Any, Dict, List, Optional
Expand All @@ -10,14 +11,15 @@
import machineid
import psutil
import requests
import uvicorn
from bs4 import BeautifulSoup
from commonwealth.utils.apis import GenericErrorHandlingRoute, PrettyJSONResponse
from commonwealth.utils.decorators import temporary_cache
from commonwealth.utils.logs import InterceptHandler, init_logger
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi_versioning import VersionedFastAPI, version
from pydantic import BaseModel
from uvicorn import Config, Server

PORT = 81
DOCS_CANDIDATE_URLS = ["/docs", "/v1.0/ui/"]
Expand All @@ -26,6 +28,10 @@
HTML_FOLDER = Path.joinpath(Path(__file__).parent.absolute(), "html")


logging.basicConfig(handlers=[InterceptHandler()], level=0)
init_logger("Helper")


class Website(str, Enum):
ArduPilot = "http://firmware.ardupilot.org"
AWS = "http://amazon.com"
Expand Down Expand Up @@ -181,14 +187,10 @@ async def check_internet_access() -> Any:
return await Helper.check_internet_access()


@fast_api_app.on_event("startup")
async def startup_event() -> None:
async def periodic() -> None:
while True:
await asyncio.sleep(60)
await Helper.check_internet_access()

asyncio.create_task(periodic())
async def periodic() -> None:
while True:
await asyncio.sleep(60)
await Helper.check_internet_access()


app = VersionedFastAPI(
Expand All @@ -201,4 +203,11 @@ async def periodic() -> None:
app.mount("/", StaticFiles(directory=str(HTML_FOLDER), html=True))

if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=PORT)
loop = asyncio.new_event_loop()

# Running uvicorn with log disabled so loguru can handle it
config = Config(app=app, loop=loop, host="0.0.0.0", port=81, log_config=None)
server = Server(config)

loop.create_task(periodic())
loop.run_until_complete(server.serve())