-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automatic archival service (#1045)
- Loading branch information
1 parent
05d0f5b
commit 69010dc
Showing
12 changed files
with
220 additions
and
8 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
alembic/versions/e65e0c04606b_add_last_access_study_prop.py
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,34 @@ | ||
"""add_last_access_study_prop | ||
Revision ID: e65e0c04606b | ||
Revises: 26c50ef2a0e1 | ||
Create Date: 2022-08-09 12:48:55.860099 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'e65e0c04606b' | ||
down_revision = '26c50ef2a0e1' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('study', schema=None) as batch_op: | ||
batch_op.add_column(sa.Column('last_access', sa.DateTime(), nullable=True)) | ||
|
||
|
||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('study', schema=None) as batch_op: | ||
batch_op.drop_column('last_access') | ||
|
||
# ### end Alembic commands ### |
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,57 @@ | ||
import datetime | ||
import logging | ||
import time | ||
from typing import List | ||
|
||
from antarest.core.config import Config | ||
from antarest.core.interfaces.service import IService | ||
from antarest.core.jwt import DEFAULT_ADMIN_USER | ||
from antarest.core.requests import RequestParameters | ||
from antarest.study.model import Study, RawStudy | ||
from antarest.study.service import StudyService | ||
from antarest.study.storage.utils import is_managed | ||
from antarest.study.storage.variantstudy.model.dbmodel import VariantStudy | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AutoArchiveService(IService): | ||
def __init__(self, study_service: StudyService, config: Config): | ||
super(AutoArchiveService, self).__init__() | ||
self.study_service = study_service | ||
self.config = config | ||
|
||
def _try_archive_studies(self) -> None: | ||
now = datetime.datetime.utcnow() | ||
studies: List[Study] = self.study_service.repository.get_all() | ||
for study in studies: | ||
if is_managed( | ||
study | ||
) and study.updated_at < now - datetime.timedelta( | ||
days=self.config.storage.auto_archive_threshold_days | ||
): | ||
if isinstance(study, RawStudy) and not study.archived: | ||
self.study_service.archive( | ||
study.id, | ||
params=RequestParameters(DEFAULT_ADMIN_USER), | ||
) | ||
elif isinstance(study, VariantStudy): | ||
self.study_service.storage_service.variant_study_service.clear_snapshot( | ||
study | ||
) | ||
self.study_service.archive_outputs( | ||
study.id, | ||
params=RequestParameters(DEFAULT_ADMIN_USER), | ||
) | ||
|
||
def _loop(self) -> None: | ||
while True: | ||
try: | ||
self._try_archive_studies() | ||
except Exception as e: | ||
logger.error( | ||
"Unexpected error happened when processing auto archive service loop", | ||
exc_info=e, | ||
) | ||
finally: | ||
time.sleep(2) |
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,67 @@ | ||
import datetime | ||
from pathlib import Path | ||
from unittest.mock import Mock, call | ||
|
||
from antarest.core.config import Config, StorageConfig, WorkspaceConfig | ||
from antarest.core.jwt import DEFAULT_ADMIN_USER | ||
from antarest.core.requests import RequestParameters | ||
from antarest.study.model import RawStudy, DEFAULT_WORKSPACE_NAME | ||
from antarest.study.storage.auto_archive_service import AutoArchiveService | ||
from antarest.study.storage.variantstudy.model.dbmodel import VariantStudy | ||
|
||
|
||
def test_auto_archival(tmp_path: Path): | ||
workspace_path = tmp_path / "workspace_test" | ||
auto_archive_service = AutoArchiveService( | ||
Mock(), | ||
Config( | ||
storage=StorageConfig( | ||
workspaces={"test": WorkspaceConfig(path=workspace_path)} | ||
) | ||
), | ||
) | ||
|
||
now = datetime.datetime.now() | ||
|
||
auto_archive_service.study_service.repository = Mock() | ||
auto_archive_service.study_service.repository.get_all.return_value = [ | ||
RawStudy( | ||
id="a", | ||
workspace="not default", | ||
updated_at=now - datetime.timedelta(days=61), | ||
), | ||
RawStudy( | ||
id="b", | ||
workspace=DEFAULT_WORKSPACE_NAME, | ||
updated_at=now - datetime.timedelta(days=59), | ||
), | ||
RawStudy( | ||
id="c", | ||
workspace=DEFAULT_WORKSPACE_NAME, | ||
updated_at=now - datetime.timedelta(days=61), | ||
archived=True, | ||
), | ||
RawStudy( | ||
id="d", | ||
workspace=DEFAULT_WORKSPACE_NAME, | ||
updated_at=now - datetime.timedelta(days=61), | ||
archived=False, | ||
), | ||
VariantStudy(id="e", updated_at=now - datetime.timedelta(days=61)), | ||
] | ||
auto_archive_service.study_service.storage_service = Mock() | ||
auto_archive_service.study_service.storage_service.variant_study_service = ( | ||
Mock() | ||
) | ||
|
||
auto_archive_service._try_archive_studies() | ||
|
||
auto_archive_service.study_service.archive.assert_called_once_with( | ||
"d", params=RequestParameters(DEFAULT_ADMIN_USER) | ||
) | ||
auto_archive_service.study_service.storage_service.variant_study_service.clear_snapshot.assert_called_once_with( | ||
VariantStudy(id="e", updated_at=now - datetime.timedelta(days=61)) | ||
) | ||
auto_archive_service.study_service.archive_outputs.assert_called_once_with( | ||
"e", params=RequestParameters(DEFAULT_ADMIN_USER) | ||
) |
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