-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py
More file actions
569 lines (489 loc) · 22.7 KB
/
Copy pathscheduler.py
File metadata and controls
569 lines (489 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
"""Background scheduler service for automated Zoom recording downloads."""
import logging
import logging.handlers
import re
import threading
import time
from datetime import datetime, date, timedelta
from pathlib import Path
from typing import Optional
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from config_manager import ConfigManager
from zoom_api import ZoomClient, ZoomAuthError, ZoomAPIError
from file_manager import FileManager, FileValidationError
logger = logging.getLogger(__name__)
JOB_ID = "daily_recording_download"
MAX_DOWNLOAD_RETRIES = 3
RETRY_WAIT_SECONDS = 5
LOG_MAX_BYTES = 10 * 1024 * 1024 # 10 MB per log file
LOG_BACKUP_COUNT = 10 # Keep 10 rotated log files
def sanitize_topic(topic: str) -> str:
"""Sanitize a meeting topic for safe logging.
Truncates long topics and redacts anything that looks like it could
be an email, phone number, or SSN-like pattern.
"""
if not topic:
return "Untitled"
# Redact email addresses
sanitized = re.sub(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}',
'[EMAIL]', topic)
# Redact phone number patterns
sanitized = re.sub(r'\b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b', '[PHONE]', sanitized)
# Redact SSN-like patterns
sanitized = re.sub(r'\b\d{3}-\d{2}-\d{4}\b', '[REDACTED]', sanitized)
# Truncate
if len(sanitized) > 80:
sanitized = sanitized[:80] + "..."
return sanitized
class DownloadResult:
"""Tracks the outcome of a download job run."""
def __init__(self):
self.start_time: Optional[datetime] = None
self.end_time: Optional[datetime] = None
self.accounts_processed: int = 0
self.recordings_downloaded: int = 0
self.recordings_skipped: int = 0
self.errors: list[dict] = []
self.success: bool = False
def to_dict(self) -> dict:
return {
"start_time": self.start_time.isoformat() if self.start_time else None,
"end_time": self.end_time.isoformat() if self.end_time else None,
"accounts_processed": self.accounts_processed,
"recordings_downloaded": self.recordings_downloaded,
"recordings_skipped": self.recordings_skipped,
"errors": self.errors,
"success": self.success,
}
class SchedulerService:
"""Manages the APScheduler BackgroundScheduler and download job logic."""
def __init__(self, config_manager: ConfigManager):
self._config = config_manager
self._scheduler = BackgroundScheduler(daemon=True)
self._status_lock = threading.Lock()
self._status = {
"is_running": False,
"scheduler_active": False,
"last_run": None,
"next_run_time": None,
"current_account": None,
"current_operation": None,
}
def start(self) -> None:
"""Read run_time from config, schedule daily job, start scheduler."""
settings = self._config.get_settings()
run_time = settings.get("run_time", "13:00")
hour, minute = self._parse_time(run_time)
self._scheduler.add_job(
self._execute_download_job,
CronTrigger(hour=hour, minute=minute),
id=JOB_ID,
replace_existing=True,
)
self._scheduler.start()
with self._status_lock:
self._status["scheduler_active"] = True
self._update_next_run_time()
logger.info("Scheduler started. Daily job at %s", run_time)
def stop(self) -> None:
"""Shutdown scheduler gracefully."""
if self._scheduler.running:
self._scheduler.shutdown(wait=False)
with self._status_lock:
self._status["scheduler_active"] = False
self._status["next_run_time"] = None
logger.info("Scheduler stopped")
def reschedule(self, new_time: str) -> None:
"""Update the daily job schedule with a new time (HH:MM)."""
hour, minute = self._parse_time(new_time)
try:
self._scheduler.reschedule_job(
JOB_ID,
trigger=CronTrigger(hour=hour, minute=minute),
)
with self._status_lock:
self._update_next_run_time()
logger.info("Rescheduled daily job to %s", new_time)
except Exception as e:
logger.error("Failed to reschedule: %s", e)
def get_status(self) -> dict:
"""Thread-safe read of status dict."""
with self._status_lock:
status = self._status.copy()
if status.get("last_run") and isinstance(status["last_run"], DownloadResult):
status["last_run"] = status["last_run"].to_dict()
self._update_next_run_time()
status["next_run_time"] = self._status["next_run_time"]
return status
def run_now(self, target_date: Optional[date] = None,
account_ids: Optional[list[str]] = None,
download_all: bool = False) -> None:
"""Trigger an immediate run in a background thread.
If download_all is True, downloads all recordings currently in
each account (iterates month-by-month until no more are found).
Raises RuntimeError if a job is already running.
"""
with self._status_lock:
if self._status["is_running"]:
raise RuntimeError("A download job is already running")
thread = threading.Thread(
target=self._execute_download_job,
args=(target_date, account_ids, download_all),
daemon=True,
)
thread.start()
def _execute_download_job(self, target_date: Optional[date] = None,
account_ids: Optional[list[str]] = None,
download_all: bool = False) -> None:
"""Core download logic that processes all enabled accounts.
If download_all is True, queries month-by-month to retrieve all
recordings in the account, organizing each by its actual recording
date. Stops after 2 consecutive empty months.
"""
result = DownloadResult()
result.start_time = datetime.now()
with self._status_lock:
self._status["is_running"] = True
self._status["current_operation"] = "starting"
# Session-level set of downloaded recording IDs (for co-hosted dedup)
session_downloaded_ids: set[str] = set()
try:
settings = self._config.get_settings()
base_path = settings.get("base_archive_path", "")
log_path = settings.get("log_path", "")
if not base_path:
logger.error("Base archive path not configured")
result.errors.append({
"account": "System",
"error": "Base archive path not configured",
"type": "PATH_ERROR",
})
return
if not download_all and target_date is None:
target_date = date.today() - timedelta(days=1)
# Build date ranges to query
if download_all:
date_ranges = self._build_all_recordings_date_ranges()
else:
date_ranges = [(target_date, target_date)]
file_mgr = FileManager(base_path)
# Get accounts to process (use get_accounts() to decrypt secrets)
accounts = self._config.get_accounts()
if account_ids:
accounts = [a for a in accounts if a["id"] in account_ids]
else:
accounts = [a for a in accounts if a.get("enabled", False)]
mode_desc = "all recordings" if download_all else str(target_date)
logger.info("Starting download job for %s. Processing %d accounts.",
mode_desc, len(accounts))
for account in accounts:
acct_name = account.get("custom_name", "Unknown")
acct_id = account.get("id", "")
with self._status_lock:
self._status["current_account"] = acct_name
client = None
try:
# Authenticate
with self._status_lock:
self._status["current_operation"] = "authenticating"
client = ZoomClient(
account_id=account["account_id"],
client_id=account["client_id"],
client_secret=account["client_secret"],
account_name=acct_name,
)
client.authenticate()
# Collect audio files across all date ranges
all_audio_files = []
consecutive_empty = 0
for from_date, to_date in date_ranges:
with self._status_lock:
if download_all:
self._status["current_operation"] = (
f"listing recordings "
f"({from_date.strftime('%b %Y')})"
)
else:
self._status["current_operation"] = (
"listing recordings"
)
audio_files = client.get_audio_recordings(
from_date, to_date
)
all_audio_files.extend(audio_files)
if download_all and not audio_files:
consecutive_empty += 1
logger.info(
"No recordings for %s in %s - %s "
"(%d consecutive empty months)",
acct_name, from_date, to_date,
consecutive_empty,
)
if consecutive_empty >= 2:
logger.info(
"Stopping lookback for %s after %d "
"consecutive empty months",
acct_name, consecutive_empty,
)
break
elif download_all:
consecutive_empty = 0
if not all_audio_files:
logger.info("No recordings found for %s",
acct_name)
result.accounts_processed += 1
continue
# Download each recording
for rec in all_audio_files:
rec_id = rec["recording_id"]
# Skip if already downloaded this session (co-hosted dedup)
if rec_id in session_downloaded_ids:
logger.info("Skipping duplicate recording %s", rec_id)
result.recordings_skipped += 1
continue
# Skip if already downloaded previously
if self._config.is_recording_downloaded(rec_id):
logger.info("Skipping already-downloaded recording %s",
rec_id)
result.recordings_skipped += 1
session_downloaded_ids.add(rec_id)
continue
with self._status_lock:
self._status["current_operation"] = (
f"downloading: {sanitize_topic(rec['meeting_topic'])}"
)
# Determine recording date for file organization
rec_date = self._parse_recording_date(
rec.get("recording_start"), target_date
)
target_path = file_mgr.get_target_path(
rec_date, acct_name, rec["meeting_topic"]
)
# Check if file already exists with matching size
if file_mgr.file_exists_with_size(target_path,
rec.get("file_size")):
logger.info("File already exists: %s", target_path)
session_downloaded_ids.add(rec_id)
result.recordings_skipped += 1
continue
# Download with retries
downloaded = False
for attempt in range(MAX_DOWNLOAD_RETRIES):
try:
client.download_recording(
rec["download_url"],
str(target_path),
rec.get("file_size"),
)
# Validate M4A
try:
file_mgr.validate_m4a(target_path)
except FileValidationError as ve:
logger.error(
"Validation failed for %s: %s",
target_path, ve,
)
file_mgr.delete_file(target_path)
result.errors.append({
"account": acct_name,
"meeting": rec["meeting_topic"],
"error": str(ve),
"type": "VALIDATION_FAILURE",
})
# Don't retry validation failures
break
# Success
self._config.mark_recording_downloaded(
rec_id, acct_id, str(target_path)
)
session_downloaded_ids.add(rec_id)
result.recordings_downloaded += 1
downloaded = True
break
except (ZoomAPIError, OSError) as e:
logger.warning(
"Download attempt %d/%d failed for %s: %s",
attempt + 1, MAX_DOWNLOAD_RETRIES,
rec["meeting_topic"], e,
)
# Clean up partial file
file_mgr.delete_file(target_path)
if attempt < MAX_DOWNLOAD_RETRIES - 1:
time.sleep(RETRY_WAIT_SECONDS)
if not downloaded and not any(
err.get("type") == "VALIDATION_FAILURE"
and err.get("meeting") == rec["meeting_topic"]
for err in result.errors
):
result.errors.append({
"account": acct_name,
"meeting": rec["meeting_topic"],
"error": f"Failed after {MAX_DOWNLOAD_RETRIES} attempts",
"type": "DOWNLOAD_FAILURE",
})
result.accounts_processed += 1
self._config.update_account_last_run(acct_id)
except ZoomAuthError as e:
logger.error("Auth failed for %s: %s", acct_name, e)
result.errors.append({
"account": acct_name,
"error": str(e),
"type": "AUTH_FAILURE",
})
result.accounts_processed += 1
except Exception as e:
logger.error("Unexpected error for %s: %s", acct_name, e)
result.errors.append({
"account": acct_name,
"error": str(e),
"type": "UNKNOWN_ERROR",
})
result.accounts_processed += 1
finally:
if client:
client.close()
result.success = len(result.errors) == 0
except Exception as e:
logger.error("Fatal error in download job: %s", e)
result.errors.append({
"account": "System",
"error": str(e),
"type": "SYSTEM_ERROR",
})
finally:
result.end_time = datetime.now()
with self._status_lock:
self._status["is_running"] = False
self._status["last_run"] = result
self._status["current_account"] = None
self._status["current_operation"] = None
# Write log file
log_date = target_date if not download_all else None
self._write_log_file(result, log_date)
logger.info(
"Download job complete. %d downloaded, %d skipped, %d errors.",
result.recordings_downloaded, result.recordings_skipped,
len(result.errors),
)
@staticmethod
def _build_all_recordings_date_ranges() -> list[tuple[date, date]]:
"""Build a list of (from_date, to_date) tuples going back month-by-month.
The Zoom API limits date ranges to 1 month per request.
Returns ranges from most recent to oldest, covering all available
recordings without an arbitrary time limit. The caller uses
early-stop logic (consecutive empty months) to avoid unnecessary
API calls once we've gone past the oldest recording.
A safety cap of 120 months (10 years) prevents runaway iteration.
"""
MAX_MONTHS = 120
ranges = []
today = date.today()
for i in range(MAX_MONTHS):
# End of range: today for first iteration, then last day of prev month
if i == 0:
to_date = today
else:
# First day of current chunk's next month, minus 1 day
ref_month = today.month - i
ref_year = today.year
while ref_month <= 0:
ref_month += 12
ref_year -= 1
# Last day of that month = first day of next month - 1
next_month = ref_month + 1
next_year = ref_year
if next_month > 12:
next_month = 1
next_year += 1
to_date = date(next_year, next_month, 1) - timedelta(days=1)
# Start of range: first of the month
from_month = today.month - i
from_year = today.year
while from_month <= 0:
from_month += 12
from_year -= 1
from_date = date(from_year, from_month, 1)
ranges.append((from_date, to_date))
return ranges
@staticmethod
def _parse_recording_date(
recording_start: Optional[str],
fallback_date: Optional[date] = None,
) -> date:
"""Extract the date from a recording's start_time ISO string.
Falls back to fallback_date or yesterday if parsing fails.
"""
if recording_start:
try:
return datetime.fromisoformat(
recording_start.replace("Z", "+00:00")
).date()
except (ValueError, AttributeError):
pass
if fallback_date:
return fallback_date
return date.today() - timedelta(days=1)
def _write_log_file(self, result: DownloadResult,
target_date: Optional[date]) -> None:
"""Write a summary log file for the run using rotating file handler."""
settings = self._config.get_settings()
log_path = settings.get("log_path", "")
if not log_path:
return
log_dir = Path(log_path)
log_dir.mkdir(parents=True, exist_ok=True)
date_str = datetime.now().strftime("%Y%m%d")
log_file = log_dir / f"zoom_archival_{date_str}.log"
# Use a rotating file handler to prevent unbounded log growth
run_logger = logging.getLogger(f"zoom_capture.run.{date_str}")
if not run_logger.handlers:
handler = logging.handlers.RotatingFileHandler(
str(log_file),
maxBytes=LOG_MAX_BYTES,
backupCount=LOG_BACKUP_COUNT,
encoding="utf-8",
)
handler.setFormatter(logging.Formatter("%(message)s"))
run_logger.addHandler(handler)
run_logger.setLevel(logging.INFO)
if target_date:
date_desc = f"On {target_date.strftime('%B %d, %Y')}"
else:
date_desc = "Download All Recordings run"
run_logger.info(
"%s we downloaded %d Zoom files across %d accounts.",
date_desc, result.recordings_downloaded,
result.accounts_processed,
)
for error in result.errors:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
acct = error.get("account", "Unknown")
meeting = sanitize_topic(error.get("meeting", "N/A"))
err_type = error.get("type", "UNKNOWN")
run_logger.error(
"[%s] ERROR - Account: %s - Meeting: %s - %s",
timestamp, acct, meeting, err_type,
)
logger.info("Log written to %s", log_file)
def _update_next_run_time(self) -> None:
"""Update next_run_time in status from the scheduler job."""
try:
job = self._scheduler.get_job(JOB_ID)
if job and job.next_run_time:
self._status["next_run_time"] = (
job.next_run_time.isoformat()
)
else:
self._status["next_run_time"] = None
except Exception:
self._status["next_run_time"] = None
@staticmethod
def _parse_time(time_str: str) -> tuple[int, int]:
"""Parse 'HH:MM' string into (hour, minute) tuple."""
parts = time_str.strip().split(":")
hour = int(parts[0])
minute = int(parts[1]) if len(parts) > 1 else 0
if not (0 <= hour <= 23 and 0 <= minute <= 59):
raise ValueError(f"Invalid time: {time_str}")
return hour, minute