Skip to content

Commit

Permalink
[DOP-22054] Move migrations to separated script
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed Dec 6, 2024
1 parent 27ea965 commit edc3621
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 75 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ dev-worker: db-start broker-start ##@Application Run development broker (without


prod-build-server: ##@Application Build docker image for server
docker build --progress=plain -t mtsrus/syncmaster-backend:develop -f ./docker/Dockerfile.backend $(ARGS) .
docker build --progress=plain -t mtsrus/syncmaster-backend:develop -f ./docker/Dockerfile.backend --target=prod $(ARGS) .

prod-build-scheduler: ##@Application Build docker image for scheduler
docker build --progress=plain -t mtsrus/syncmaster-scheduler:develop -f ./docker/Dockerfile.scheduler --target=prod $(ARGS) .

prod-build-worker: ##@Application Build docker image for worker
docker build --progress=plain -t mtsrus/syncmaster-worker:develop -f ./docker/Dockerfile.worker $(ARGS) .
docker build --progress=plain -t mtsrus/syncmaster-worker:develop -f ./docker/Dockerfile.worker --target=prod $(ARGS) .

prod-build: prod-build-server prod-build-worker ##@Application Build docker images

Expand Down
29 changes: 28 additions & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ services:
timeout: 5s
retries: 3

migrations:
image: mtsrus/syncmaster-worker:${WORKER_IMAGE_TAG:-test}
restart: none
build:
dockerfile: docker/Dockerfile.worker
context: .
target: test
volumes:
- ./syncmaster:/app/syncmaster
- ./tests:/app/tests
entrypoint: [python, -m, syncmaster.db.migrations, upgrade, head]
env_file: .env.docker
depends_on:
db:
condition: service_healthy

backend:
image: mtsrus/syncmaster-backend:${BACKEND_IMAGE_TAG:-test}
restart: unless-stopped
Expand All @@ -41,15 +57,22 @@ services:
volumes:
- ./syncmaster:/app/syncmaster
- ./docs/_static:/app/docs/_static
- ./cached_jars:/root/.ivy2
- ./reports:/app/reports
- ./tests:/app/tests
- ./pyproject.toml:/app/pyproject.toml
depends_on:
db:
condition: service_healthy
migrations:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
healthcheck:
test: [CMD-SHELL, curl -f http://localhost:8000/monitoring/ping]
interval: 30s
timeout: 5s
retries: 3
start_period: 5s
profiles: [backend, all]

scheduler:
Expand All @@ -68,6 +91,8 @@ services:
depends_on:
db:
condition: service_healthy
migrations:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
profiles: [scheduler, all]
Expand All @@ -94,6 +119,8 @@ services:
depends_on:
db:
condition: service_healthy
migrations:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
profiles: [worker, scheduler, s3, oracle, hdfs, hive, clickhouse, mysql, mssql, all]
Expand Down
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,26 @@ services:
timeout: 5s
retries: 3

migrations:
image: mtsrus/syncmaster-backend:${TAG:-develop}
restart: none
build:
dockerfile: docker/Dockerfile.backend
context: .
target: prod
entrypoint: [python, -m, syncmaster.db.migrations, upgrade, head]
env_file: .env.docker
depends_on:
db:
condition: service_healthy

backend:
image: mtsrus/syncmaster-backend:${TAG:-develop}
restart: unless-stopped
build:
dockerfile: docker/Dockerfile.backend
context: .
target: prod
ports:
- 8000:8000
environment:
Expand All @@ -49,6 +63,8 @@ services:
depends_on:
db:
condition: service_healthy
migrations:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
healthcheck:
Expand All @@ -64,10 +80,13 @@ services:
build:
dockerfile: docker/Dockerfile.worker
context: .
target: prod
env_file: .env.docker
depends_on:
db:
condition: service_healthy
migrations:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy

Expand All @@ -77,10 +96,13 @@ services:
build:
dockerfile: docker/Dockerfile.scheduler
context: .
target: prod
env_file: .env.docker
depends_on:
db:
condition: service_healthy
migrations:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy

Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ENV SYNCMASTER__SERVER__OPENAPI__SWAGGER__JS_URL=/static/swagger/swagger-ui-bund
SYNCMASTER__SERVER__STATIC_FILES__DIRECTORY=/app/syncmaster/backend/static


FROM base as test
FROM base AS test

RUN poetry install --no-root --extras "backend" --with test --without docs,dev
RUN sed -i 's/python -m/coverage run -m/g' /app/entrypoint.sh
5 changes: 3 additions & 2 deletions docker/Dockerfile.scheduler
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ FROM base AS prod

COPY ./syncmaster/ /app/syncmaster/

FROM base as test

FROM base AS test

RUN poetry install --no-root --all-extras --with test --without docs,dev
RUN sed -i 's/python -m/coverage run -m/g' /app/entrypoint.sh
RUN sed -i 's/python -m/coverage run -m/g' /app/entrypoint.sh
2 changes: 1 addition & 1 deletion docker/Dockerfile.worker
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ FROM base as prod
COPY ./syncmaster/ /app/syncmaster/


FROM base as test
FROM base AS test

ENV SYNCMASTER__WORKER__CREATE_SPARK_SESSION_FUNCTION=tests.spark.get_worker_spark_session

Expand Down
2 changes: 0 additions & 2 deletions docker/entrypoint_backend.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bash
set -e

python -m syncmaster.db.migrations upgrade head

if [[ "x${SYNCMASTER__ENTRYPOINT__SUPERUSERS}" != "x" ]]; then
superusers=$(echo "${SYNCMASTER__ENTRYPOINT__SUPERUSERS}" | tr "," " ")
python -m syncmaster.backend.scripts.manage_superusers add ${superusers}
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/next_release/163.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Now migrations are executed in a dedicated one-off container, instead of being run as a part of ``backend`` container.
6 changes: 2 additions & 4 deletions docs/scheduler/start_scheduler.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Starting the Scheduler
======================


With docker
-----------

Expand All @@ -14,12 +13,12 @@ Options can be set via ``.env`` file or ``environment`` section in ``docker-comp
.. dropdown:: ``docker-compose.yml``

.. literalinclude:: ../../docker-compose.yml
:emphasize-lines: 68-83
:emphasize-lines: 90-103

.. dropdown:: ``.env.docker``

.. literalinclude:: ../../.env.docker
:emphasize-lines: 11-25
:emphasize-lines: 11-25

To start the worker container you need to run the command:

Expand All @@ -28,7 +27,6 @@ To start the worker container you need to run the command:
docker compose up scheduler -d --wait --wait-timeout 200
Without docker
--------------

Expand Down
12 changes: 5 additions & 7 deletions docs/worker/start_worker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ Starting the Celery Worker
Before starting the worker you need to create a queue.
The queue is created by sending a post request to ``/queues`` endpoint (See Swagger doc for details).


With docker
-----------

Installation process
~~~~~~~~~~~~~~~~~~~~

Docker will download worker image of syncmaster worker & broker, and run them.
Docker will download worker image, and then start worker container with dependencies.
Options can be set via ``.env`` file or ``environment`` section in ``docker-compose.yml``

.. dropdown:: ``docker-compose.yml``

.. literalinclude:: ../../docker-compose.yml
:emphasize-lines: 55-66
:emphasize-lines: 75-88

.. dropdown:: ``.env.docker``

.. literalinclude:: ../../.env.docker
:emphasize-lines: 11-22
:emphasize-lines: 11-22

To start the worker container you need to run the command:

Expand All @@ -33,21 +32,20 @@ To start the worker container you need to run the command:
docker compose up worker -d --wait --wait-timeout 200
Without docker
--------------

To start the worker you need to run the command

.. code-block:: bash
python -m celery -A syncmaster.worker.celery worker
python -m celery -A syncmaster.worker.celery worker --max-tasks-per-child=1
You can specify options like concurrency and queues by adding additional flags:

.. code-block:: bash
celery -A -A syncmaster.worker.celery worker --concurrency=4 --max-tasks-per-child=1 --loglevel=info
python -m celery -A syncmaster.worker.celery worker --concurrency=4 --max-tasks-per-child=1 --loglevel=info
Refer to the `Celery <https://docs.celeryq.dev/en/stable/>`_ documentation for more advanced start options.
54 changes: 3 additions & 51 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ apscheduler = { version = "^3.10.4", optional = true }
starlette-exporter = {version = "^0.23.0", optional = true}
itsdangerous = {version = "*", optional = true}
python-keycloak = {version = "^4.7.0", optional = true}
devtools = {version = "*", optional = true}

[tool.poetry.extras]
backend = [
Expand All @@ -91,7 +90,6 @@ backend = [
"coloredlogs",
"python-json-logger",
"asyncpg",
"devtools",
"itsdangerous",
"python-keycloak",
"pyyaml",
Expand All @@ -110,7 +108,6 @@ worker = [
"uuid6",
"coloredlogs",
"python-json-logger",
"python-keycloak",
]

scheduler = [
Expand Down
2 changes: 1 addition & 1 deletion syncmaster/backend/providers/auth/dummy_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

import logging
from pprint import pformat
from time import time
from typing import Annotated, Any

from devtools import pformat
from fastapi import Depends, FastAPI

from syncmaster.backend.dependencies import Stub
Expand Down

0 comments on commit edc3621

Please sign in to comment.