Skip to content

Commit 5bd00eb

Browse files
chaenfstagni
authored andcommitted
feat (TS): clean TransformationAgent cache upon every loop
1 parent f6d1af3 commit 5bd00eb

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/DIRAC/TransformationSystem/Agent/TransformationAgent.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" TransformationAgent processes transformations found in the transformation database.
1+
"""TransformationAgent processes transformations found in the transformation database.
22
33
The following options can be set for the TransformationAgent.
44
@@ -8,13 +8,15 @@
88
:dedent: 2
99
:caption: TransformationAgent options
1010
"""
11+
1112
from importlib import import_module
1213

1314
import time
1415
import os
1516
import datetime
1617
import pickle
1718
import concurrent.futures
19+
from pathlib import Path
1820

1921
from DIRAC import S_OK, S_ERROR
2022
from DIRAC.ConfigurationSystem.Client.Helpers.Operations import Operations
@@ -127,6 +129,9 @@ def execute(self):
127129
if not res["OK"]:
128130
self._logError("Failed to obtain transformations:", res["Message"])
129131
return S_OK()
132+
133+
active_trans_ids = [t["TransformationID"] for t in res["Value"]]
134+
self.cleanOldTransformationCache(active_trans_ids)
130135
# Process the transformations
131136
count = 0
132137
future_to_transID = {}
@@ -164,6 +169,22 @@ def execute(self):
164169

165170
return S_OK()
166171

172+
def cleanOldTransformationCache(self, active_trans_ids: list[int]):
173+
cache_filenames = {Path(self.__cacheFile(tid)) for tid in active_trans_ids}
174+
existing_caches = set(Path(self.workDirectory).glob("*.pkl"))
175+
useless_cache_files = existing_caches - cache_filenames
176+
177+
if useless_cache_files:
178+
self._logInfo(f"Found potentially {len(useless_cache_files)} useless cache files")
179+
180+
# Since idle transformations aren't in active_trans_ids, let's filter it more
181+
# and take only files that haven't been touched for 2 month
182+
last_update_threshold = (datetime.datetime.utcnow() - datetime.timedelta(days=60)).timestamp()
183+
184+
for cache_file in useless_cache_files:
185+
if Path(cache_file).stat().st_mtime < last_update_threshold:
186+
cache_file.unlink()
187+
167188
def getTransformations(self):
168189
"""Obtain the transformations to be executed - this is executed at the start of every loop (it's really the
169190
only real thing in the execute()

0 commit comments

Comments
 (0)