()
| 54 | |
| 55 | @shared_task |
| 56 | def scan_and_process_files(): |
| 57 | global _first_scan_completed |
| 58 | redis_client = RedisClient.get_client() |
| 59 | now = time.time() |
| 60 | |
| 61 | # Check if directories exist |
| 62 | dirs_exist = all(os.path.exists(d) for d in [M3U_WATCH_DIR, EPG_WATCH_DIR, LOGO_WATCH_DIR]) |
| 63 | if not dirs_exist: |
| 64 | throttled_log(logger.warning, f"Watch directories missing: M3U ({os.path.exists(M3U_WATCH_DIR)}), EPG ({os.path.exists(EPG_WATCH_DIR)}), LOGO ({os.path.exists(LOGO_WATCH_DIR)})", "watch_dirs_missing") |
| 65 | |
| 66 | # Process M3U files |
| 67 | m3u_files = [f for f in os.listdir(M3U_WATCH_DIR) |
| 68 | if os.path.isfile(os.path.join(M3U_WATCH_DIR, f)) and |
| 69 | (f.endswith('.m3u') or f.endswith('.m3u8'))] |
| 70 | |
| 71 | m3u_processed = 0 |
| 72 | m3u_skipped = 0 |
| 73 | |
| 74 | for filename in m3u_files: |
| 75 | filepath = os.path.join(M3U_WATCH_DIR, filename) |
| 76 | mtime = os.path.getmtime(filepath) |
| 77 | age = now - mtime |
| 78 | redis_key = REDIS_PREFIX + filepath |
| 79 | stored_mtime = redis_client.get(redis_key) |
| 80 | |
| 81 | # Instead of assuming old files were processed, check if they exist in the database |
| 82 | if not stored_mtime and age > STARTUP_SKIP_AGE: |
| 83 | # Check if this file is already in the database |
| 84 | existing_m3u = M3UAccount.objects.filter(file_path=filepath).exists() |
| 85 | if existing_m3u: |
| 86 | # Use trace level if not first scan |
| 87 | if _first_scan_completed: |
| 88 | logger.trace(f"Skipping {filename}: Already exists in database") |
| 89 | else: |
| 90 | logger.debug(f"Skipping {filename}: Already exists in database") |
| 91 | redis_client.set(redis_key, mtime, ex=REDIS_TTL) |
| 92 | m3u_skipped += 1 |
| 93 | continue |
| 94 | else: |
| 95 | logger.debug(f"Processing {filename} despite age: Not found in database") |
| 96 | # Continue processing this file even though it's old |
| 97 | |
| 98 | # File too new — probably still being written |
| 99 | if age < MIN_AGE_SECONDS: |
| 100 | logger.debug(f"Skipping {filename}: Too new (age={age}s)") |
| 101 | m3u_skipped += 1 |
| 102 | continue |
| 103 | |
| 104 | # Skip if we've already processed this mtime |
| 105 | if stored_mtime and float(stored_mtime) >= mtime: |
| 106 | # Use trace level if not first scan |
| 107 | if _first_scan_completed: |
| 108 | logger.trace(f"Skipping {filename}: Already processed this version") |
| 109 | else: |
| 110 | logger.debug(f"Skipping {filename}: Already processed this version") |
| 111 | m3u_skipped += 1 |
| 112 | continue |
| 113 |
no test coverage detected