initial
This commit is contained in:
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})
|
||||
Reference in New Issue
Block a user