Skip to content

Commit

Permalink
Fix database_maintenance (#2595)
Browse files Browse the repository at this point in the history
Fix database_maintenance

Should fix #2427

Opened just one db session to speed up the operation that was taking too long
Used sql.null instead of python None -> python None was not making the field null but was inserting a Null string into the field.

Reviewed-by: Laura Barcziová
  • Loading branch information
softwarefactory-project-zuul[bot] authored Oct 23, 2024
2 parents 0ee8ec9 + 5332c05 commit 772f124
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
20 changes: 15 additions & 5 deletions packit_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1797,20 +1797,30 @@ def get_by_id(cls, id_: int) -> Optional["ProjectEventModel"]:
return session.query(ProjectEventModel).filter_by(id=id_).first()

@classmethod
def get_older_than_with_packages_config(
def get_and_reset_older_than_with_packages_config(
cls,
delta: timedelta,
) -> Iterable["ProjectEventModel"]:
"""Return project events with all runs older than delta that store packages config."""
"""Return project events with all runs older than delta
and set to null their stored packages config.
Cleanup project events here to speed up the process."""
delta_ago = datetime.now(timezone.utc) - delta
with sa_session_transaction() as session:
return (
with sa_session_transaction(commit=True) as session:
events = (
session.query(ProjectEventModel)
.filter(ProjectEventModel.packages_config.isnot(None))
.filter(ProjectEventModel.packages_config.isnot(null()))
.filter(
~ProjectEventModel.runs.any(PipelineModel.datetime >= delta_ago),
)
)
# After we reset the packages config
# the query will be empty.
# Store the query result in a new list
events_list = list(events)
for event in events:
event.packages_config = null()
session.add(event)
return events_list

def set_packages_config(self, packages_config: dict):
with sa_session_transaction(commit=True) as session:
Expand Down
13 changes: 7 additions & 6 deletions packit_service/worker/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ def discard_old_package_configs():
PACKAGE_CONFIGS_OUTDATED_AFTER_DAYS,
)
ago = timedelta(days=int(outdated_after_days))
for event in ProjectEventModel.get_older_than_with_packages_config(ago):
logger.debug(
f"ProjectEventModel {event.id} has all runs older than '{ago}'. "
"Discarding package config.",
)
event.set_packages_config(None)
events = ProjectEventModel.get_and_reset_older_than_with_packages_config(ago)
event_ids = "".join([" " + str(event.id) for event in events])

logger.debug(
f"ProjectEventModels with ids [{event_ids}] have all runs older than '{ago}'. "
"Discarded package configs.",
)


def gzip_file(file: Path) -> Path:
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def test_cleanup_old_srpm_build_logs():


def test_discard_old_package_configs():
event_model = flexmock(id=1)
flexmock(event_model).should_receive("set_packages_config").with_args(None).once()
event_model1 = flexmock(id=1)
event_model2 = flexmock(id=2)
flexmock(ProjectEventModel).should_receive(
"get_older_than_with_packages_config",
).and_return([event_model]).once()
"get_and_reset_older_than_with_packages_config",
).and_return([event_model1, event_model2]).once()
database.discard_old_package_configs()


Expand Down
6 changes: 3 additions & 3 deletions tests_openshift/database/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ def test_get_all_downstream_projects(clean_before_and_after, propose_model_submi
assert projects.pop().project_url == SampleValues.downstream_project_url


def test_project_event_get_older_than_with_packages_config(
def test_project_event_get_and_reset_older_than_with_packages_config(
clean_before_and_after,
branch_project_event_model,
):
Expand All @@ -1117,7 +1117,7 @@ def test_project_event_get_older_than_with_packages_config(
assert (
len(
list(
ProjectEventModel.get_older_than_with_packages_config(
ProjectEventModel.get_and_reset_older_than_with_packages_config(
timedelta(days=1),
),
),
Expand All @@ -1131,7 +1131,7 @@ def test_project_event_get_older_than_with_packages_config(
assert (
len(
list(
ProjectEventModel.get_older_than_with_packages_config(
ProjectEventModel.get_and_reset_older_than_with_packages_config(
timedelta(days=1),
),
),
Expand Down

0 comments on commit 772f124

Please sign in to comment.