From 746606682f85382cdea02289b40c5c4e1833afb2 Mon Sep 17 00:00:00 2001 From: beagold <86345081+beagold@users.noreply.github.com> Date: Fri, 30 Aug 2024 19:19:24 +0200 Subject: [PATCH] Gracefully handle connection errors when polling (#48) * Handle common connection errors gracefully * Cleanup repository Correct readme command Add support for .env configuration Bump docker image to 3.12-alpine Fix `docker run` stop signal to avoid waiting shutdown timeout --- .gitignore | 12 +++++++++++- Dockerfile | 7 ++++--- README.md | 8 +++++++- docker-compose.yml | 6 ++++-- runner.py | 24 +++++++++++++++++------- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 3e7318d..4c885d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,22 @@ +# Virtual enviroments venv/ .venv/ + +# Python cache __pycache__/ + +# Editor configuration files .idea/ *.iml .vscode/ .settings/ .classpath + +# Nox +.nox/ + +# Container files +.env updated.txt config.yaml data/ -.nox/ diff --git a/Dockerfile b/Dockerfile index a168559..e09f479 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,11 @@ -FROM python:3.11-buster +FROM python:3.12-alpine WORKDIR /code COPY ./runner.py ./runner.py COPY ./dev-requirements/constraints.txt ./requirements.txt -RUN pip install -Ur requirements.txt +RUN pip install -Ur requirements.txt -ENTRYPOINT python runner.py +STOPSIGNAL SIGINT +ENTRYPOINT ["python", "runner.py"] diff --git a/README.md b/README.md index ef1f8c5..c3ba469 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,10 @@ Polls GitHub to notify of any new commits to the API documentation, then sends i ## Running -Run `docker-compose up -d -e 'DAPI_TRACKER_PATH'='http://discord-webhook-url-here'`. +Create a file called `.env` in the root of the repository and add: + +``` +DAPI_TRACKER_WEBHOOK_URL='' +``` + +Then run it using `docker compose up -d`. diff --git a/docker-compose.yml b/docker-compose.yml index cfd52cd..35c355d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,9 @@ -version: "3" services: poller: build: . - restart: always + + restart: unless-stopped + + env_file: .env volumes: - ./data:/data diff --git a/runner.py b/runner.py index 03909a1..ba60155 100644 --- a/runner.py +++ b/runner.py @@ -150,13 +150,23 @@ def main(webhook_url: str, tracker_path: pathlib.Path, period: int, api_url: str last_update = _now() while True: - last_update = _poll( - webhook_url=webhook_url, - tracker_path=tracker_path, - api_url=api_url, - params=params_dict, - last_update=last_update, - ) + try: + last_update = _poll( + webhook_url=webhook_url, + tracker_path=tracker_path, + api_url=api_url, + params=params_dict, + last_update=last_update, + ) + except ( + requests.exceptions.ConnectionError, + requests.exceptions.Timeout, + requests.exceptions.HTTPError, + requests.exceptions.JSONDecodeError, + requests.exceptions.InvalidJSONError, + ) as ex: + logging.exception("Failed to fetch latest update, backing off and trying again later", exc_info=ex) + time.sleep(period)