Skip to content

Commit

Permalink
Change .env MongoDB key names, optimize MongoDB document existence ch…
Browse files Browse the repository at this point in the history
…eck, optimize task existence check
  • Loading branch information
aangelos28 committed Oct 24, 2024
1 parent 602c434 commit bf2aace
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
COMPOSE_PROJECT_NAME=eos

# MongoDB admin credentials
EOS_MONGO_INITDB_ROOT_USERNAME=
EOS_MONGO_INITDB_ROOT_PASSWORD=
EOS_MONGODB_ROOT_USER=
EOS_MONGODB_ROOT_PASSWORD=

# MinIO admin credentials
EOS_MINIO_ROOT_USER=
Expand Down
8 changes: 4 additions & 4 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ services:
context: .
dockerfile: mongodb/Dockerfile
args:
- MONGO_INITDB_ROOT_USERNAME=${EOS_MONGO_INITDB_ROOT_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${EOS_MONGO_INITDB_ROOT_PASSWORD}
- MONGO_INITDB_ROOT_USERNAME=${EOS_MONGODB_ROOT_USER}
- MONGO_INITDB_ROOT_PASSWORD=${EOS_MONGODB_ROOT_PASSWORD}
image: eos-mongodb/latest
container_name: eos-mongodb
hostname: eos-mongodb
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: ${EOS_MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${EOS_MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_ROOT_USERNAME: ${EOS_MONGODB_ROOT_USER}
MONGO_INITDB_ROOT_PASSWORD: ${EOS_MONGODB_ROOT_PASSWORD}
ports:
- "27017:27017"
networks:
Expand Down
2 changes: 1 addition & 1 deletion eos/persistence/abstract_async_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def count(self, **query: dict) -> int:
pass

@abstractmethod
async def exists(self, count: int = 1, **query: dict) -> bool:
async def exists(self, **query: dict) -> bool:
pass

@abstractmethod
Expand Down
7 changes: 3 additions & 4 deletions eos/persistence/mongodb_async_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ async def count(self, session: AgnosticClientSession | None = None, **kwargs) ->
"""
return await self._collection.count_documents(session=session, filter=kwargs)

async def exists(self, count: int = 1, session: AgnosticClientSession | None = None, **kwargs) -> bool:
async def exists(self, session: AgnosticClientSession | None = None, **kwargs) -> bool:
"""
Check if the number of entities that match the query exist in the collection.
Check if an entity exists in the collection.
:param count: The number of entities to check for.
:param session: The optional session to use for the operation.
:param kwargs: Query parameters.
:return: Whether the entity exists.
"""
return await self.count(session=session, **kwargs) >= count
return await self._collection.find_one(kwargs, {"_id": 1}, session=session) is not None

async def get_one(self, session: AgnosticClientSession | None = None, **kwargs) -> dict[str, Any]:
"""
Expand Down
2 changes: 1 addition & 1 deletion eos/tasks/task_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def create_task(
:param containers: The input containers for the task.
:param metadata: Additional metadata to be stored with the task.
"""
if await self._tasks.get_one(experiment_id=experiment_id, id=task_id):
if await self._tasks.exists(experiment_id=experiment_id, id=task_id):
raise EosTaskExistsError(f"Cannot create task '{task_id}' as a task with that ID already exists.")

task_spec = self._configuration_manager.task_specs.get_spec_by_type(task_type)
Expand Down
7 changes: 1 addition & 6 deletions tests/test_mongodb_async_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,8 @@ async def test_count_and_exists(self, repository):
]
await asyncio.gather(*[repository.create(entity) for entity in entities])

count, exists, exists_more = await asyncio.gather(
repository.count(), repository.exists(count=2), repository.exists(count=3)
)

count = await repository.count()
assert count == 2
assert exists
assert not exists_more

@pytest.mark.asyncio
async def test_delete_many(self, repository):
Expand Down

0 comments on commit bf2aace

Please sign in to comment.