-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 🎸 add concept of Resource #784
Merged
Merged
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
52b9df8
feat: 🎸 add concept of Resource
severo 7be733d
ci: 🎡 fix unit tests for the job
severo 203d789
refactor: 💡 remove dead code
severo 6aa2398
Merge branch 'main' into create_resources_concept
severo 073809c
test: 💍 give more time to avoid timeout to mongodb
severo 5da80a8
refactor: 💡 rename resource into resources
severo 2f7bef4
refactor: 💡 use InitVar to avoid mutating fields
severo 7d047b0
fix: 🐛 release the resources on shutdown
severo c7b9de9
fix: 🐛 avoid using InitVar since they are hard to use
severo c4d755b
test: 💍 fix parameter name
severo 59c78b9
fix: 🐛 no need to call .allocate, it's done in __post_init__
severo 9d4039f
feat: 🎸 use primitive parameters, add release, add tests
severo 8952cb5
Merge branch 'main' into create_resources_concept
severo 4681b97
style: 💄 fix style
severo 12a52b1
Merge branch 'main' into create_resources_concept
severo d99d0e4
refactor: 💡 rename AssetsDirectory to AssetsStorageAccess
severo 2301eb9
fix: 🐛 allocate assets storage for all the workers
severo 318f1a2
Merge branch 'main' into create_resources_concept
severo e3317c9
refactor: 💡 change env var names for coherence
severo 0c5f89a
fix: 🐛 missing env var
severo 8613ecc
ci: 🎡 fix env var
severo a7afa28
refactor: 💡 replace LogResource with init_logging
severo ebe20fb
feat: 🎸 add init_assets_directory method
severo 1a8fb1e
refactor: 💡 remove StorageResource + remove mongo.py
severo 37889e9
fix: 🐛 fix details to make the CI pass
severo 66233a7
test: 💍 fix tests
severo 235d3cf
refactor: 💡 remove dead code
severo 16554f0
test: 💍 remove dead code
severo a4396c6
style: 💄 fix style
severo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright 2022 The HuggingFace Authors. | ||
|
||
DATABASE_MIGRATIONS_MONGOENGINE_ALIAS = "maintenance" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright 2022 The HuggingFace Authors. | ||
|
||
from dataclasses import dataclass, field | ||
|
||
from libcommon.resources import DatabaseResource | ||
|
||
from mongodb_migration.constants import DATABASE_MIGRATIONS_MONGOENGINE_ALIAS | ||
|
||
|
||
class MigrationsDatabaseConnectionFailure(Exception): | ||
pass | ||
|
||
|
||
@dataclass | ||
class MigrationsDatabaseResource(DatabaseResource): | ||
""" | ||
A resource that represents a connection to the migrations database. | ||
|
||
Args: | ||
database (:obj:`str`): The name of the database. | ||
host (:obj:`str`): The host of the database. It must start with ``mongodb://`` or ``mongodb+srv://``. | ||
""" | ||
|
||
mongoengine_alias: str = field(default=DATABASE_MIGRATIONS_MONGOENGINE_ALIAS, init=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,18 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright 2022 The HuggingFace Authors. | ||
|
||
from pytest import MonkeyPatch, fixture | ||
from environs import Env | ||
from pytest import fixture | ||
|
||
from mongodb_migration.config import JobConfig | ||
|
||
|
||
# see https://github.com/pytest-dev/pytest/issues/363#issuecomment-406536200 | ||
@fixture(scope="session") | ||
def monkeypatch_session(): | ||
monkeypatch_session = MonkeyPatch() | ||
monkeypatch_session.setenv("CACHE_MONGO_DATABASE", "datasets_server_cache_test") | ||
monkeypatch_session.setenv("QUEUE_MONGO_DATABASE", "datasets_server_queue_test") | ||
monkeypatch_session.setenv("MONGODB_MIGRATION_MONGO_DATABASE", "datasets_server_maintenance_test") | ||
yield monkeypatch_session | ||
monkeypatch_session.undo() | ||
def env() -> Env: | ||
return Env(expand_vars=True) | ||
|
||
|
||
@fixture(scope="session", autouse=True) | ||
def app_config(monkeypatch_session: MonkeyPatch) -> JobConfig: | ||
job_config = JobConfig.from_env() | ||
if ( | ||
"test" not in job_config.cache.mongo_database | ||
or "test" not in job_config.queue.mongo_database | ||
or "test" not in job_config.mongodb_migration.mongo_database | ||
): | ||
raise ValueError("Test must be launched on a test mongo database") | ||
return job_config | ||
@fixture(scope="session") | ||
def mongo_host(env: Env) -> str: | ||
try: | ||
return env.str(name="MONGODB_MIGRATION_MONGO_URL") | ||
except Exception as e: | ||
raise ValueError("MONGODB_MIGRATION_MONGO_URL is not set") from e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright 2022 The HuggingFace Authors. | ||
|
||
from mongoengine import Document | ||
from mongoengine.fields import StringField | ||
|
||
from mongodb_migration.resources import MigrationsDatabaseResource | ||
|
||
|
||
def test_cache_database(mongo_host: str) -> None: | ||
resource = MigrationsDatabaseResource(database="test_migrations_database", host=mongo_host) | ||
|
||
class User(Document): | ||
name = StringField() | ||
meta = {"db_alias": resource.mongo_connection.mongoengine_alias} | ||
|
||
assert len(User.objects()) == 0 # type: ignore | ||
# clean | ||
User.drop_collection() # type: ignore | ||
resource.release() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the defined class at line 14, It might be DATABASE_MIGRATION_MONGO_URL value
And also attribute name at line 9 could be DATABASE_MIGRATION_MONGO_DATABASE
Just to keep the consistency between NAMESPACE_ -> Class Config as it is done in other configurations classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks