-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
588 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
FROM python:3.7 | ||
|
||
ENV PYTHONUNBUFFERED 1 | ||
WORKDIR /opt/apiweb | ||
WORKDIR /opt/api | ||
|
||
COPY ./requirements.txt ./ | ||
COPY ./requirements-test.txt ./ | ||
RUN pip install --upgrade pip==20.3 | ||
RUN pip install --upgrade pip==20.3.3 | ||
RUN pip install -r ./requirements.txt -r requirements-test.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# pylint: disable=W0611 | ||
|
||
import os | ||
from time import sleep | ||
from flask import Flask | ||
from random import random | ||
from sqlalchemy_api_handler import ApiHandler | ||
from sqlalchemy_api_handler.utils import logger | ||
from sqlalchemy.exc import OperationalError | ||
|
||
from api.repository.checks import check_from_model | ||
from api.utils.config import VERSION | ||
from api.utils.setup import setup | ||
from api.utils.database import db | ||
|
||
|
||
SLEEP_TIME = 1 | ||
|
||
FLASK_APP = Flask(__name__) | ||
|
||
|
||
setup(FLASK_APP, | ||
with_cors=False) | ||
|
||
|
||
TABLE_NAME = f'check_health_{int(1000*random())}' | ||
class CheckHealth(db.Model, ApiHandler): | ||
__tablename__ = TABLE_NAME | ||
pass | ||
|
||
|
||
def check_from_model(model): | ||
database_working = False | ||
version = 'Api {} : '.format(VERSION) | ||
try: | ||
model.query.limit(1).all() | ||
database_working = True | ||
state = 'database is ok.' | ||
except Exception as e: | ||
logger.critical(str(e)) | ||
state = 'database is not ok.' | ||
output = '{}{}'.format(version, state) | ||
|
||
return database_working, output | ||
|
||
IS_DATABASE_CONNECT_OK = False | ||
while not IS_DATABASE_CONNECT_OK: | ||
try: | ||
CheckHealth.__table__.drop(db.session.bind, checkfirst=True) | ||
CheckHealth.__table__.create(db.session.bind) | ||
db.session.commit() | ||
except OperationalError: | ||
print('Could not connect to postgres db... Retry in {}s...'.format(SLEEP_TIME)) | ||
sleep(SLEEP_TIME) | ||
continue | ||
print('Connection to postgres db is okay.') | ||
IS_DATABASE_CONNECT_OK = True | ||
|
||
IS_DATABASE_HEALTH_OK = False | ||
while not IS_DATABASE_HEALTH_OK: | ||
IS_DATABASE_HEALTH_OK = check_from_model(CheckHealth)[0] | ||
db.session.execute(f'DROP TABLE {TABLE_NAME};') | ||
db.session.commit() | ||
if not IS_DATABASE_HEALTH_OK: | ||
print('Could not check database health... Retry in {}s...'.format(SLEEP_TIME)) | ||
else: | ||
print('Database health is ok.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from sqlalchemy_api_handler import ApiHandler | ||
from sqlalchemy_api_handler.mixins import TaskMixin | ||
|
||
from api.utils.database import db | ||
|
||
|
||
class Task(ApiHandler, | ||
db.Model, | ||
TaskMixin): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def import_tasks(): | ||
import api.tasks.hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from sqlalchemy_api_handler import ApiHandler | ||
|
||
from api.utils.celery import CELERY_APP | ||
|
||
|
||
@CELERY_APP.task | ||
def print_text(text): | ||
print(f'Hello {text}') | ||
return { 'text': text } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import os | ||
import celery | ||
|
||
from api.utils.config import APP_NAME | ||
|
||
|
||
CELERY_TASKS_LIMIT = int(os.environ.get('CELERY_TASKS_LIMIT') or 10) | ||
LOCALHOST_REDIS_URL = f'redis://apiredisdb:6379/0' | ||
REDIS_URL = os.environ.get('REDIS_URL', LOCALHOST_REDIS_URL) | ||
|
||
|
||
CELERY_APP = celery.Celery('{}-tasks'.format(APP_NAME), | ||
backend=REDIS_URL, | ||
broker=REDIS_URL, | ||
broker_pool_limit=CELERY_TASKS_LIMIT) | ||
CELERY_APP.conf.task_default_queue = 'default' | ||
CELERY_APP.is_disabled = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
import os | ||
import sys | ||
|
||
APP_NAME = os.environ.get('APP_NAME', '') | ||
COMMAND_NAME = os.environ.get('COMMAND_NAME', '') | ||
PORT = int(os.environ.get('PORT', 5000)) | ||
API_URL = 'http://api:{}'.format(PORT) | ||
|
||
IS_SCHEDULER = sys.argv[0].endswith('celery') \ | ||
and len(sys.argv) > 3 and \ | ||
sys.argv[2] == 'beat' | ||
IS_WORKER = sys.argv[0].endswith('celery') \ | ||
and len(sys.argv) > 3 \ | ||
and sys.argv[2] == 'worker' | ||
IS_APP = not IS_SCHEDULER and not IS_WORKER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.