33import os
44import sys
55import shutil
6- from datetime import datetime , timedelta
6+ from datetime import datetime , timedelta , timezone
7+ import sqlite3
78"""
89 clean up com/ stmp/ logs/ directoris mostly for realtime runs
910 but it can also be used for offline clean up at the command line
@@ -18,8 +19,41 @@ def is_directory_empty(directory_path):
1819#
1920
2021
22+ def is_cycle_done (EXPDIR , CDATE ):
23+ is_done = False
24+ db = f"{ EXPDIR } /rrfs.db"
25+ conn = sqlite3 .connect (db )
26+ cur = conn .cursor ()
27+ cur .execute (f"SELECT * FROM cycles" )
28+ rows = cur .fetchall () # id, cycle, activated, expired, done, draining
29+ for r in rows :
30+ dt = datetime .fromtimestamp (r [1 ], tz = timezone .utc )
31+ rcycle = dt .strftime ("%Y%m%d%H%M" )
32+ if rcycle == f'{ CDATE } 00' :
33+ if r [4 ] != 0 : # non-zero means "done"
34+ is_done = True
35+ break
36+ # ~~~~~~~~
37+ return is_done
38+ #
39+ # ----------------------------------------------------------------------
40+ #
41+
42+
2143def day_clean (srcPath , cyc1 , cyc2 , srcType , WGF ):
44+ check_is_cyc_done = os .getenv ("CHECK_IS_CYC_DONE" , "FALSE" ).upper () == "TRUE"
45+ if check_is_cyc_done : # mainly for retros. Not needed by realtime runs as we want to remove all old cycles including expired ones
46+ EXPDIR = os .getenv ("EXPDIR" , "EXPDIR_not_defined" )
47+ STMP_KEPT_TASKS = os .getenv ("STMP_KEPT_TASKS" , "" ).strip ()
48+ RUN = os .getenv ("RUN" , "" )
49+
2250 for i in range (cyc1 , cyc2 + 1 ):
51+ if check_is_cyc_done :
52+ CDATE = srcPath .rstrip ("/" )[- 8 :] + f'{ i :02} '
53+ if not is_cycle_done (EXPDIR , CDATE ): # skip the clean process for cycles not done yet
54+ print (f'{ CDATE } NOT done yet, no cleaning' )
55+ continue
56+
2357 if srcType == "log" :
2458 pattern = f'{ i :02} /{ WGF } '
2559 elif srcType == "stmp" :
@@ -28,20 +62,29 @@ def day_clean(srcPath, cyc1, cyc2, srcType, WGF):
2862 pattern = f'{ i :02} /lbc/{ WGF } '
2963 else : # com
3064 pattern = f'{ i :02} /*/{ WGF } '
65+
3166 pathlist = list (Path (srcPath ).glob (pattern ))
67+ if srcType == "stmp" and STMP_KEPT_TASKS != "" : # exclude STMP_KEPT_TASKS from pathlist
68+ kept_tasks = STMP_KEPT_TASKS .split ("," )
69+ excludes = []
70+ for task in kept_tasks :
71+ excludes .append (f'{ RUN } _{ task } _{ i :02} _' )
72+ pathlist = [p for p in pathlist if not any (ex in str (p ) for ex in excludes )]
73+
3274 for mypath in pathlist :
33- sys .stdout .write (f'purge { mypath } ......' )
34- try :
35- shutil .rmtree (mypath )
36- sys .stdout .write (f'done!\n ' )
37- except Exception as e :
38- sys .stdout .write (f'\n An error occurred: { e } ' )
75+ if os .path .exists (mypath ):
76+ sys .stdout .write (f'purge { mypath } ......' )
77+ try :
78+ shutil .rmtree (mypath )
79+ sys .stdout .write (f'done!\n ' )
80+ except Exception as e :
81+ sys .stdout .write (f'\n An error occurred: { e } ' )
3982 # ~~~~~~~~~~~~
4083 # remove empty directories
4184 #
4285 pathlist = list (Path (srcPath ).glob (pattern .rstrip (f'/{ WGF } ' )))
4386 for mypath in pathlist :
44- if is_directory_empty (mypath ):
87+ if os . path . exists ( mypath ) and is_directory_empty (mypath ):
4588 os .rmdir (mypath )
4689 print (f'remove empty directory: { mypath } ' )
4790 # ~~~~~~~~~~~~
@@ -136,23 +179,24 @@ def group_clean(cdate, retention_cycs, srcBase, srcType, NET, RUN, WGF, rrfs_ver
136179# remove data based on clean-realted environmental variables
137180#
138181cdate = datetime .strptime (f'{ PDY } { cyc } ' , "%Y%m%d%H" )
182+ cdate = cdate .replace (tzinfo = timezone .utc ) # make it UTC-aware
139183print (f'cdate={ cdate } ' )
140184print (f'stmp_retention_cycs={ stmp_retention_cycs } ' )
141185print (f'com_retention_cycs={ com_retention_cycs } ' )
142186print (f'com_lbc_retention_cycs={ com_lbc_retention_cycs } ' )
143187print (f'log_retention_cycs={ log_retention_cycs } ' )
144188print (f'clean_back_days={ clean_back_days } ' )
145189
146- print (f'\n clean stmp: { os .path .dirname (DATAROOT )} ' )
190+ print (f'\n Try to clean stmp: { os .path .dirname (DATAROOT )} ' )
147191group_clean (cdate , stmp_retention_cycs , DATAROOT , 'stmp' , NET , RUN , WGF , rrfs_ver )
148192
149- print (f'\n clean com: { COMROOT } ' )
193+ print (f'\n Try to clean com: { COMROOT } ' )
150194group_clean (cdate , com_retention_cycs , COMROOT , 'com' , NET , RUN , WGF , rrfs_ver )
151195
152- print (f'\n clean com_lbc: { COMROOT } ' )
196+ print (f'\n Try to clean com_lbc: { COMROOT } ' )
153197group_clean (cdate , com_lbc_retention_cycs , COMROOT , 'com_lbc' , NET , RUN , WGF , rrfs_ver )
154198
155- print ('\n clean log: ' + COMROOT .rstrip ('/' ) + f'{ NET } /{ rrfs_ver } /logs' )
199+ print ('\n Try to clean log: ' + COMROOT .rstrip ('/' ) + f'{ NET } /{ rrfs_ver } /logs' )
156200group_clean (cdate , log_retention_cycs , COMROOT , 'log' , NET , RUN , WGF , rrfs_ver )
157201
158202print ('\n Done!' )
0 commit comments