From 4b1ee9406bdef1f1fb5c86a16feeb8cd2a43b636 Mon Sep 17 00:00:00 2001 From: Rafael Nebot Medina Date: Wed, 9 Dec 2020 11:57:34 +0000 Subject: [PATCH] Polishing deployment of Docker image * Ahead compilation of Cython modules (so that "pyximport" finds modules already compiled) * Use of Named Lock to avoid database initialization problems with multiple gunicorn workers starting at the same --- Dockerfile | 3 +++ README.md | 2 +- nexinfosys/restful_service/service_main.py | 9 ++++++++- requirements.txt | 1 + 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 51fc140..d938917 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ FROM python:3.7.3-slim-stretch # docker build -t magicnexush2020/magic-nis-backend . # # 1.bis) REFRESH image into Docker Hub: +# ("docker login" first) # docker push magicnexush2020/magic-nis-backend: # # Usage example: @@ -113,4 +114,6 @@ CMD ["/usr/local/bin/gunicorn", "--workers=3", "--log-level=debug", "--timeout=2 #CMD ["supervisord", "-c", "/etc/supervisord.conf"] COPY nexinfosys /app/nexinfosys +RUN cythonize -a -i /app/nexinfosys/restful_service/helper_accel.pyx && \ + cythonize -a -i /app/nexinfosys/command_generators/parser_spreadsheet_utils_accel.pyx diff --git a/README.md b/README.md index d940db3..83cdd8c 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ The pip version is obviously for execution as package, and does not include the `pip install nexinfosys` -* Use class class *NIS*, with methods similar to *NISClient* class in [nexinfosys-client](https://github.com/MAGIC-nexus/nis-python-client) package. +* Use class **NIS**, with methods similar to **NISClient** class in [nexinfosys-client](https://github.com/MAGIC-nexus/nis-python-client) package. #### Docker image diff --git a/nexinfosys/restful_service/service_main.py b/nexinfosys/restful_service/service_main.py index 8770c86..1b18543 100644 --- a/nexinfosys/restful_service/service_main.py +++ b/nexinfosys/restful_service/service_main.py @@ -15,6 +15,7 @@ # from flask import (jsonify, abort, redirect, url_for, # # ) +from NamedAtomicLock import NamedAtomicLock from flask import (Response, request, session as flask_session, send_from_directory, redirect ) from flask.helpers import get_root_path @@ -124,7 +125,13 @@ def construct_session_persistence_backend(): # >>>> THE INITIALIZATION CODE <<<< # -initialize_databases() +lock = NamedAtomicLock("nis-backend-lock") +lock.acquire() +try: + initialize_databases() +finally: + lock.release() + nexinfosys.data_source_manager = register_external_datasources() d = construct_session_persistence_backend() diff --git a/requirements.txt b/requirements.txt index 93d875d..f117910 100644 --- a/requirements.txt +++ b/requirements.txt @@ -64,6 +64,7 @@ jsonpickle==1.2 python_magic>=0.4.13 # To be removed, it is used only by the Magic Box file type detection dotted>=0.1.8 # <- Dotted notation: !!Fantastic!! sympy>=1.1.1 +NamedAtomicLock==1.1.3 #sympy==1.3 #cachier==1.2.2 #tablib <- dictmapper (convert nested dict's to CSV)