Compare commits
35 Commits
185bfd93a2
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b6b04ed37a | |||
| b2857d68c1 | |||
|
|
047223ec31 | ||
|
|
d4dc42451b | ||
|
|
afeb1e2dcd | ||
|
|
a3c438af80 | ||
|
|
c3a982fbd8 | ||
|
|
1d85544321 | ||
|
|
581350d633 | ||
|
|
a9f09c953d | ||
|
|
47c5257eef | ||
|
|
be8a5d5ad1 | ||
|
|
6c6a549aff | ||
|
|
c1249bfcd0 | ||
|
|
30b587e564 | ||
|
|
2ddd88337e | ||
|
|
4236d3a7cf | ||
|
|
789a3298e5 | ||
|
|
60e13b11ec | ||
|
|
104ac7377c | ||
|
|
8c238e7023 | ||
|
|
2da584cca9 | ||
|
|
8ee3573d9a | ||
|
|
9dfc115224 | ||
| 68ded8537a | |||
| 739d2366e5 | |||
| a55baa613c | |||
| acfbb98e31 | |||
| f35115d2f2 | |||
| 2ff6c22e0b | |||
| 11c3026738 | |||
| 5efc884f12 | |||
| a328d6feb2 | |||
| be7bc1ef2c | |||
| 2dd497c8e2 |
@@ -2,30 +2,14 @@ version: "3.4"
|
|||||||
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
queues-nginx:
|
|
||||||
image: mathwave/sprint-repo:queues
|
|
||||||
networks:
|
|
||||||
- common-infra-nginx
|
|
||||||
environment:
|
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
queues:
|
queues:
|
||||||
image: mathwave/sprint-repo:queues
|
image: mathwave/sprint-repo:queues
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues-development
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
MONGO_HOST: "mongo.dev.chocomarsh.com"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
NEW: "true"
|
STAGE: "development"
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -35,7 +19,5 @@ services:
|
|||||||
order: start-first
|
order: start-first
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
common-infra-nginx:
|
queues-development:
|
||||||
external: true
|
|
||||||
queues:
|
|
||||||
external: true
|
external: true
|
||||||
|
|||||||
@@ -2,31 +2,14 @@ version: "3.4"
|
|||||||
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
queues-nginx:
|
|
||||||
image: mathwave/sprint-repo:queues
|
|
||||||
networks:
|
|
||||||
- common-infra-nginx
|
|
||||||
environment:
|
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
queues:
|
queues:
|
||||||
image: mathwave/sprint-repo:queues
|
image: mathwave/sprint-repo:queues
|
||||||
networks:
|
networks:
|
||||||
- common-infra-nginx
|
|
||||||
- queues
|
- queues
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
MONGO_HOST: "mongo.chocomarsh.com"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
NEW: "true"
|
STAGE: "production"
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -36,7 +19,5 @@ services:
|
|||||||
order: start-first
|
order: start-first
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
common-infra-nginx:
|
|
||||||
external: true
|
|
||||||
queues:
|
queues:
|
||||||
external: true
|
external: true
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ jobs:
|
|||||||
run: docker push mathwave/sprint-repo:queues
|
run: docker push mathwave/sprint-repo:queues
|
||||||
deploy-dev:
|
deploy-dev:
|
||||||
name: Deploy dev
|
name: Deploy dev
|
||||||
runs-on: [dev]
|
runs-on: [prod]
|
||||||
needs: push
|
needs: push
|
||||||
steps:
|
steps:
|
||||||
- name: login
|
- name: login
|
||||||
@@ -40,5 +40,4 @@ jobs:
|
|||||||
- name: deploy
|
- name: deploy
|
||||||
env:
|
env:
|
||||||
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
||||||
QUEUES_TOKEN_DEV: ${{ secrets.QUEUES_TOKEN_DEV }}
|
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml infra-development
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml infra
|
|
||||||
|
|||||||
@@ -40,5 +40,4 @@ jobs:
|
|||||||
- name: deploy
|
- name: deploy
|
||||||
env:
|
env:
|
||||||
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
|
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
|
||||||
QUEUES_TOKEN_PROD: ${{ secrets.QUEUES_TOKEN_PROD }}
|
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml infra
|
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml infra
|
||||||
|
|||||||
@@ -8,4 +8,6 @@ RUN pip install -r requirements.txt
|
|||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
ENTRYPOINT ["python", "main.py"]
|
ENTRYPOINT ["python", "main.py"]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import asyncio
|
||||||
|
import collections
|
||||||
import fastapi
|
import fastapi
|
||||||
import pydantic
|
import pydantic
|
||||||
import typing
|
import typing
|
||||||
@@ -5,18 +7,24 @@ import typing
|
|||||||
from app.storage.mongo import tasks
|
from app.storage.mongo import tasks
|
||||||
|
|
||||||
|
|
||||||
|
locks = collections.defaultdict(asyncio.Lock)
|
||||||
router = fastapi.APIRouter()
|
router = fastapi.APIRouter()
|
||||||
|
|
||||||
|
|
||||||
class Response(pydantic.BaseModel):
|
class Task(pydantic.BaseModel):
|
||||||
id: str
|
id: str
|
||||||
attempt: int
|
attempt: int
|
||||||
payload: dict
|
payload: dict
|
||||||
|
|
||||||
|
|
||||||
|
class Response(pydantic.BaseModel):
|
||||||
|
task: Task|None
|
||||||
|
|
||||||
|
|
||||||
@router.get('/api/v1/take', responses={404: {'description': 'Not found'}})
|
@router.get('/api/v1/take', responses={404: {'description': 'Not found'}})
|
||||||
async def execute(queue: typing.Annotated[str, fastapi.Header()]) -> Response:
|
async def execute(queue: typing.Annotated[str, fastapi.Header()]) -> Response:
|
||||||
task = await tasks.take_task(queue)
|
async with locks[queue]:
|
||||||
|
task = await tasks.take_task(queue)
|
||||||
if not task:
|
if not task:
|
||||||
raise fastapi.HTTPException(404)
|
return Response(task=None)
|
||||||
return Response(id=str(task._id), attempt=task.attempts, payload=task.payload)
|
return Response(task=Task(id=str(task._id), attempt=task.attempts, payload=task.payload))
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import asyncio
|
|
||||||
import bson
|
import bson
|
||||||
import datetime
|
import datetime
|
||||||
import pydantic
|
import pydantic
|
||||||
import typing
|
|
||||||
|
|
||||||
from app.storage.mongo import database
|
from app.storage.mongo import database
|
||||||
from app.utils import time
|
from app.utils import time
|
||||||
@@ -28,7 +26,7 @@ async def add_task(task: Task) -> str:
|
|||||||
return result.inserted_id
|
return result.inserted_id
|
||||||
|
|
||||||
|
|
||||||
async def take_task(queue: str) -> typing.Optional[Task]:
|
async def take_task(queue: str) -> Task|None:
|
||||||
now = time.now()
|
now = time.now()
|
||||||
async for raw_task in collection.find({'queue': queue, 'available_from': {'$lte': now}}):
|
async for raw_task in collection.find({'queue': queue, 'available_from': {'$lte': now}}):
|
||||||
task = Task.model_validate(raw_task)
|
task = Task.model_validate(raw_task)
|
||||||
|
|||||||
15
main.py
15
main.py
@@ -1,6 +1,5 @@
|
|||||||
import fastapi
|
import fastapi
|
||||||
import uvicorn
|
import uvicorn
|
||||||
import os
|
|
||||||
|
|
||||||
from app.routers import take
|
from app.routers import take
|
||||||
from app.routers import put
|
from app.routers import put
|
||||||
@@ -9,16 +8,20 @@ from app.routers import finish
|
|||||||
from app.storage import mongo
|
from app.storage import mongo
|
||||||
|
|
||||||
|
|
||||||
app = fastapi.FastAPI()
|
app = fastapi.FastAPI(debug=True)
|
||||||
|
|
||||||
app.include_router(take.router)
|
app.include_router(take.router)
|
||||||
app.include_router(put.router)
|
app.include_router(put.router)
|
||||||
app.include_router(finish.router)
|
app.include_router(finish.router)
|
||||||
|
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def unicorn_exception_handler(request: fastapi.Request, exc: Exception):
|
||||||
|
return fastapi.JSONResponse(
|
||||||
|
status_code=500,
|
||||||
|
content={"message": f"Oops! {exc.name} did something. There goes a rainbow... {exc}"},
|
||||||
|
)
|
||||||
|
|
||||||
mongo.create_indexes()
|
mongo.create_indexes()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if os.getenv('NEW') == 'true':
|
uvicorn.run(app, host="0.0.0.0", port=1239)
|
||||||
uvicorn.run(app, host="0.0.0.0", port=1239)
|
|
||||||
uvicorn.run(app, host="0.0.0.0", port=1238)
|
|
||||||
|
|||||||
@@ -1,20 +1,44 @@
|
|||||||
annotated-types==0.7.0
|
annotated-types==0.7.0
|
||||||
anyio==4.6.2.post1
|
anyio==4.6.2.post1
|
||||||
APScheduler==3.10.4
|
APScheduler==3.10.4
|
||||||
|
certifi==2024.12.14
|
||||||
|
charset-normalizer==3.4.1
|
||||||
click==8.1.7
|
click==8.1.7
|
||||||
dnspython==2.7.0
|
dnspython==2.7.0
|
||||||
|
email_validator==2.2.0
|
||||||
fastapi==0.115.4
|
fastapi==0.115.4
|
||||||
|
fastapi-cli==0.0.7
|
||||||
h11==0.14.0
|
h11==0.14.0
|
||||||
|
httpcore==1.0.7
|
||||||
|
httptools==0.6.4
|
||||||
|
httpx==0.28.1
|
||||||
idna==3.10
|
idna==3.10
|
||||||
|
Jinja2==3.1.5
|
||||||
|
markdown-it-py==3.0.0
|
||||||
|
MarkupSafe==3.0.2
|
||||||
|
mdurl==0.1.2
|
||||||
motor==3.6.0
|
motor==3.6.0
|
||||||
pydantic==2.9.2
|
pydantic==2.9.2
|
||||||
pydantic_core==2.23.4
|
pydantic_core==2.23.4
|
||||||
|
Pygments==2.18.0
|
||||||
pymongo==4.9.2
|
pymongo==4.9.2
|
||||||
|
python-dotenv==1.0.1
|
||||||
|
python-multipart==0.0.20
|
||||||
pytz==2024.2
|
pytz==2024.2
|
||||||
|
PyYAML==6.0.2
|
||||||
redis==5.2.0
|
redis==5.2.0
|
||||||
|
requests==2.32.3
|
||||||
|
rich==13.9.4
|
||||||
|
rich-toolkit==0.12.0
|
||||||
|
shellingham==1.5.4
|
||||||
six==1.16.0
|
six==1.16.0
|
||||||
sniffio==1.3.1
|
sniffio==1.3.1
|
||||||
starlette==0.41.2
|
starlette==0.41.2
|
||||||
|
typer==0.15.1
|
||||||
typing_extensions==4.12.2
|
typing_extensions==4.12.2
|
||||||
tzlocal==5.2
|
tzlocal==5.2
|
||||||
|
urllib3==2.3.0
|
||||||
uvicorn==0.32.0
|
uvicorn==0.32.0
|
||||||
|
uvloop==0.21.0
|
||||||
|
watchfiles==1.0.3
|
||||||
|
websockets==14.1
|
||||||
|
|||||||
Reference in New Issue
Block a user