Skip to content

Commit 20328a8

Browse files
authored
Survive bad json, more statistics (#155)
1 parent 4f172db commit 20328a8

4 files changed

Lines changed: 26 additions & 9 deletions

File tree

Framework/script/RepoCleaner/1_per_hour.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from Ccdb import Ccdb, ObjectVersion
66

7+
78
def process(ccdb: Ccdb, object_path: str, delay: int):
89
'''
910
Process this deletion rule on the object. We use the CCDB passed by argument.
@@ -15,6 +16,7 @@ def process(ccdb: Ccdb, object_path: str, delay: int):
1516
:param ccdb: the ccdb in which objects are cleaned up.
1617
:param object_path: path to the object, or pattern, to which a rule will apply.
1718
:param delay: the grace period during which a new object is never deleted.
19+
:return a dictionary with the number of deleted, preserved and updated versions. Total = deleted+preserved.
1820
'''
1921

2022
logging.debug(f"Plugin 1_per_hour processing {object_path}")
@@ -24,11 +26,13 @@ def process(ccdb: Ccdb, object_path: str, delay: int):
2426
last_preserved: ObjectVersion = None
2527
preservation_list: List[ObjectVersion] = []
2628
deletion_list: List[ObjectVersion] = []
29+
update_list: List[ObjectVersion] = []
2730
for v in versions:
2831
if last_preserved == None or last_preserved.validFromAsDatetime < v.validFromAsDatetime - timedelta(hours=1):
2932
# first extend validity of the previous preserved (should we take into account the run ?)
3033
if last_preserved != None:
3134
ccdb.updateValidity(last_preserved, last_preserved.validFrom, str(int(v.validFrom) - 1))
35+
update_list.append(last_preserved)
3236
last_preserved = v
3337
preservation_list.append(v)
3438
else:
@@ -45,9 +49,17 @@ def process(ccdb: Ccdb, object_path: str, delay: int):
4549
for v in preservation_list:
4650
logging.debug(f" {v}")
4751

52+
logging.debug("updated : ")
53+
for v in update_list:
54+
logging.debug(f" {v}")
55+
56+
return {"deleted" : len(deletion_list), "preserved": len(preservation_list), "updated" : len(update_list)}
57+
58+
4859
def main():
4960
ccdb = Ccdb('http://ccdb-test.cern.ch:8080')
5061
process(ccdb, "asdfasdf/example", 60)
5162

52-
if __name__== "__main__": # to be able to run the test code above when not imported.
63+
64+
if __name__ == "__main__": # to be able to run the test code above when not imported.
5365
main()

Framework/script/RepoCleaner/Ccdb.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ def getVersionsList(self, object_path: str) -> List[ObjectVersion]:
7474
headers = {'Accept':'application/json'}
7575
r = requests.get(url_browse_all_versions, headers=headers)
7676
r.raise_for_status()
77-
json = r.json()
77+
try:
78+
json_result = r.json(strict=False) # to survive bad characters in the strings of the json
79+
except ValueError as e:
80+
print(f"Error while reading json for object {object_path} from CCDB: {e}")
81+
exit(1)
7882
versions = []
79-
for object_path in json['objects']:
83+
for object_path in json_result['objects']:
8084
version = ObjectVersion(path=object_path['path'], uuid=object_path['id'], validFrom=object_path['validFrom'], validTo=object_path['validUntil'])
8185
versions.insert(0, version)
8286
return versions
@@ -87,9 +91,8 @@ def deleteVersion(self, version: ObjectVersion):
8791
Delete the specified version of an object.
8892
:param version: The version of the object to delete, as an instance of ObjectVersion.
8993
'''
90-
logging.debug(f"Delete version: {version}")
9194
url_delete = self.url + '/' + version.path + '/' + version.validFrom + '/' + version.uuid
92-
logging.info(f"Delete version at url {url_delete}")
95+
logging.debug(f"Delete version at url {url_delete}")
9396
try:
9497
r = requests.delete(url_delete)
9598
r.raise_for_status()
@@ -110,7 +113,7 @@ def updateValidity(self, version: ObjectVersion, validFrom: str, validTo: str):
110113
logging.debug("The new timestamp for validTo is identical to the existing one. Skipping.")
111114
return
112115
url_update_validity = self.url + '/' + version.path + '/' + validFrom + '/' + validTo
113-
logging.info(f"Update end limit validity of {version.path} from {version.validTo} to {validTo}")
116+
logging.debug(f"Update end limit validity of {version.path} from {version.validTo} to {validTo}")
114117
try:
115118
r = requests.put(url_update_validity)
116119
r.raise_for_status()

Framework/script/RepoCleaner/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Rules:
2-
- object_path: asdfasdf/.* # Path in the CCDB to a certain object
2+
- object_path: .* # Path in the CCDB to a certain object
33
delay: 1440 # Delay in minutes during which a new object is not touched. (1 day)
44
policy: 1_per_hour # name of the policy to apply, must correspond to a python script.
55
# - object_path: QcTask/example

Framework/script/RepoCleaner/repoCleaner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,18 @@ def main():
127127
# For each object call the first matching rule
128128
logging.info("Loop through the objects and apply first matching rule.")
129129
for object_path in paths:
130+
logging.info(f"Processing {object_path}")
130131
# Take the first matching rule, if any
131132
rule = findMatchingRule(rules, object_path);
132133
if rule == None:
133134
continue
134135

135136
# Apply rule on object (find the plug-in script and apply)
136137
module = __import__(rule.policy)
137-
module.process(ccdb, object_path, int(rule.delay))
138+
stats = module.process(ccdb, object_path, int(rule.delay))
139+
logging.info(f"{rule.policy} applied on {object_path}: {stats}")
138140

139-
logging.info(f"done (deleted: {ccdb.counter_deleted}, updated: {ccdb.counter_validity_updated})")
141+
logging.info(f" *** DONE *** (total deleted: {ccdb.counter_deleted}, total updated: {ccdb.counter_validity_updated})")
140142

141143
if __name__ == "__main__": # to be able to run the test code above when not imported.
142144
main()

0 commit comments

Comments
 (0)