|
22 | 22 | import pyaudio |
23 | 23 | import cherrypy |
24 | 24 | import json |
| 25 | +import datetime |
| 26 | +from apscheduler.schedulers.background import BackgroundScheduler |
| 27 | +from apscheduler.triggers.interval import IntervalTrigger |
| 28 | + |
25 | 29 |
|
26 | 30 | try: |
27 | 31 | import webbrowser |
@@ -140,6 +144,14 @@ def __init__(self, initOptions): |
140 | 144 | if CONFIG.LAUNCH_BROWSER and not initOptions['nolaunch']: |
141 | 145 | launch_browser(CONFIG.HTTP_HOST, self.HTTP_PORT, CONFIG.HTTP_ROOT + 'manage') |
142 | 146 |
|
| 147 | + ################################################################################################### |
| 148 | + # Run cleanup of old logs, transcripts, and recordings and start a scheduler to run every 24 hours |
| 149 | + ################################################################################################### |
| 150 | + self.cleanup_files() |
| 151 | + self.scheduler = BackgroundScheduler() |
| 152 | + self.scheduler.add_job(self.cleanup_files, 'interval', hours=24) |
| 153 | + self.scheduler.start() |
| 154 | + |
143 | 155 | SpeakReader._INITIALIZED = True |
144 | 156 |
|
145 | 157 | @property |
@@ -213,6 +225,7 @@ def stopTranscribeEngine(self): |
213 | 225 | def shutdown(self, restart=False, update=False, checkout=False): |
214 | 226 | SpeakReader._INITIALIZED = False |
215 | 227 | self.transcribeEngine.shutdown() |
| 228 | + self.scheduler.shutdown() |
216 | 229 | CONFIG.write() |
217 | 230 |
|
218 | 231 | if not restart and not update and not checkout: |
@@ -279,6 +292,35 @@ def get_input_device_list(self): |
279 | 292 | return deviceList |
280 | 293 |
|
281 | 294 |
|
| 295 | + ################################################################################################### |
| 296 | + # Delete any files over the retention days |
| 297 | + ################################################################################################### |
| 298 | + def cleanup_files(self): |
| 299 | + logger.info("Running File Cleanup") |
| 300 | + def delete(path, days): |
| 301 | + try: |
| 302 | + days = int(days) |
| 303 | + except ValueError: |
| 304 | + return |
| 305 | + delete_date = datetime.datetime.now() - datetime.timedelta(days=days) |
| 306 | + with os.scandir(path=path) as files: |
| 307 | + for file in files: |
| 308 | + file_info = file.stat() |
| 309 | + if datetime.datetime.fromtimestamp(file_info.st_ctime) < delete_date: |
| 310 | + filename = os.path.join(path, file.name) |
| 311 | + logger.debug("Deleting: %s" % filename) |
| 312 | + os.remove(filename) |
| 313 | + |
| 314 | + if CONFIG.LOG_RETENTION_DAYS != "": |
| 315 | + delete(CONFIG.LOG_DIR, CONFIG.LOG_RETENTION_DAYS) |
| 316 | + |
| 317 | + if CONFIG.TRANSCRIPT_RETENTION_DAYS != "": |
| 318 | + delete(CONFIG.TRANSCRIPTS_FOLDER, CONFIG.TRANSCRIPT_RETENTION_DAYS) |
| 319 | + |
| 320 | + if CONFIG.RECORDING_RETENTION_DAYS != "": |
| 321 | + delete(CONFIG.RECORDINGS_FOLDER, CONFIG.RECORDING_RETENTION_DAYS) |
| 322 | + |
| 323 | + |
282 | 324 | def generate_uuid(): |
283 | 325 | return uuid.uuid4().hex |
284 | 326 |
|
|
0 commit comments