Skip to content

Commit

Permalink
Gracefully handle connection errors when polling (#48)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
beagold authored Aug 30, 2024
1 parent 5797016 commit 7466066
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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/
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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='<discord-webhook-url-here>'
```

Then run it using `docker compose up -d`.
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
version: "3"
services:
poller:
build: .
restart: always

restart: unless-stopped

env_file: .env
volumes:
- ./data:/data
24 changes: 17 additions & 7 deletions runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 7466066

Please sign in to comment.