initial
This commit is contained in:
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
0
app/routers/__init__.py
Normal file
0
app/routers/__init__.py
Normal file
23
app/routers/acquire.py
Normal file
23
app/routers/acquire.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import datetime
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
from app.storage.mongo import locks
|
||||
from app.utils import time
|
||||
|
||||
|
||||
class RequestBody(pydantic.BaseModel):
|
||||
name: str
|
||||
ttl: int
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/acquire', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={'409': {'description': 'Conflict'}})
|
||||
async def execute(body: RequestBody):
|
||||
try:
|
||||
await locks.acquire(locks.Lock(name=body.name, locked_until=time.now() + datetime.timedelta(seconds=body.ttl)))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
raise fastapi.HTTPException(409)
|
||||
19
app/routers/release.py
Normal file
19
app/routers/release.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import datetime
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
from app.storage.mongo import locks
|
||||
from app.utils import time
|
||||
|
||||
|
||||
class RequestBody(pydantic.BaseModel):
|
||||
name: str
|
||||
ttl: int
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/release', status_code=fastapi.status.HTTP_202_ACCEPTED)
|
||||
async def execute(body: RequestBody):
|
||||
await locks.release(locks.Lock(name=body.name, locked_until=time.now() + datetime.timedelta(seconds=body.ttl)))
|
||||
0
app/storage/__init__.py
Normal file
0
app/storage/__init__.py
Normal file
20
app/storage/mongo/__init__.py
Normal file
20
app/storage/mongo/__init__.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
import motor
|
||||
import motor.motor_asyncio
|
||||
import pymongo
|
||||
|
||||
|
||||
MONGO_HOST = os.getenv('MONGO_HOST', 'localhost')
|
||||
MONGO_PASSWORD = os.getenv('MONGO_PASSWORD', 'password')
|
||||
|
||||
CONNECTION_STRING = f'mongodb://mongo:{MONGO_PASSWORD}@{MONGO_HOST}:27017/'
|
||||
|
||||
|
||||
database: 'motor.MotorDatabase' = motor.motor_asyncio.AsyncIOMotorClient(CONNECTION_STRING).locks
|
||||
|
||||
def create_indexes():
|
||||
client = pymongo.MongoClient(CONNECTION_STRING)
|
||||
database = client.get_database('locks')
|
||||
database.get_collection('locks').create_index([
|
||||
('name', 1),
|
||||
], unique=True)
|
||||
23
app/storage/mongo/locks.py
Normal file
23
app/storage/mongo/locks.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import bson
|
||||
import datetime
|
||||
import pydantic
|
||||
|
||||
from app.storage.mongo import database
|
||||
from app.utils import time
|
||||
from bson import codec_options
|
||||
|
||||
|
||||
collection = database.get_collection("locks", codec_options=codec_options.CodecOptions(tz_aware=True))
|
||||
|
||||
|
||||
class Lock(pydantic.BaseModel):
|
||||
name: str
|
||||
locked_until: pydantic.AwareDatetime
|
||||
|
||||
|
||||
async def acquire(lock: Lock):
|
||||
await collection.insert_one(lock.model_dump())
|
||||
|
||||
|
||||
async def release(name: str):
|
||||
await collection.delete_one({'name': name})
|
||||
4
app/utils/time.py
Normal file
4
app/utils/time.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import datetime
|
||||
|
||||
|
||||
now = lambda: datetime.datetime.now(datetime.UTC)
|
||||
Reference in New Issue
Block a user