From 24c98f833804e4054c8d1d7385c6d1701b37dfbe Mon Sep 17 00:00:00 2001 From: Israel Yago Pereira Date: Wed, 4 Aug 2021 16:33:04 -0300 Subject: [PATCH 1/3] WIP: Index content from e_documentation folder --- .dockerignore | 5 +++++ Dockerfile | 5 ++++- src/main.py | 2 ++ startup.sh | 5 +++++ 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 startup.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9d88fd3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.git +.gitignore +.example.env +/venv +/src/__pycache__ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2f29265..7368a6b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,12 @@ EXPOSE 5000 WORKDIR /usr/src/app +COPY ./startup.sh ./startup.sh +RUN ["chmod", "+x", "./startup.sh"] + COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY src . -CMD [ "flask", "run" ] \ No newline at end of file +ENTRYPOINT [ "sh", "./startup.sh" ] diff --git a/src/main.py b/src/main.py index c567800..eb8dd10 100644 --- a/src/main.py +++ b/src/main.py @@ -22,6 +22,8 @@ if __name__ == "__main__": es_host = os.getenv('ES_HOST', 'localhost') es_port = os.getenv('ES_PORT', 9200) + print(f"Connecting to {es_host}:{es_port}") + es = indexer.connect_elastic(es_host, es_port) print("ElasticSearch connection established") diff --git a/startup.sh b/startup.sh new file mode 100644 index 0000000..2f22f9b --- /dev/null +++ b/startup.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +python main.py "/e_docs_website" + +flask run -- GitLab From d06e44d27d70c25b52b81146a1244058b16fd9a6 Mon Sep 17 00:00:00 2001 From: Israel Yago Pereira Date: Thu, 5 Aug 2021 15:01:58 -0300 Subject: [PATCH 2/3] Added ES connection timeout/retries --- Dockerfile | 5 +---- src/indexer.py | 14 +++++++++++++- startup.sh | 5 ----- 3 files changed, 14 insertions(+), 10 deletions(-) delete mode 100644 startup.sh diff --git a/Dockerfile b/Dockerfile index 7368a6b..ce2a8ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,12 +4,9 @@ EXPOSE 5000 WORKDIR /usr/src/app -COPY ./startup.sh ./startup.sh -RUN ["chmod", "+x", "./startup.sh"] - COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY src . -ENTRYPOINT [ "sh", "./startup.sh" ] +ENTRYPOINT [ "flask", "run" ] diff --git a/src/indexer.py b/src/indexer.py index fe4501e..bc3a5b2 100644 --- a/src/indexer.py +++ b/src/indexer.py @@ -1,10 +1,22 @@ from elasticsearch import Elasticsearch +from time import sleep index_name = "blog" doc_type = "post" def connect_elastic(host='localhost', port=9200): - return Elasticsearch([{'host': host, 'port': port}]) + + MAXIMUM_ATTEMPS = 12 + connection = Elasticsearch([{'host': host, 'port': port}]) + for _ in range(MAXIMUM_ATTEMPS): + is_connected = connection.ping() + if not is_connected: + sleep(5) + continue + + return connection + + raise TimeoutError(f"Could not connect to elasticsearch server at {host}:{port}.") def refresh_index(es): if es.indices.exists(index=index_name): diff --git a/startup.sh b/startup.sh deleted file mode 100644 index 2f22f9b..0000000 --- a/startup.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -python main.py "/e_docs_website" - -flask run -- GitLab From 8a2d03b92294d7f3180b8b6bbca701cd9961fbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnau=20V=C3=A0zquez?= Date: Fri, 6 Aug 2021 07:46:11 +0000 Subject: [PATCH 3/3] keep CMD so it can be overridden. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ce2a8ee..4d4ec75 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,4 @@ RUN pip install --no-cache-dir -r requirements.txt COPY src . -ENTRYPOINT [ "flask", "run" ] +CMD [ "flask", "run" ] -- GitLab