static void ProcessNewFiles() { while (true) { var file = _fileQueue.Take(); _logger.Debug("Dequeuing {0}. {1} Remaining items", file, _fileQueue.Count); if (HandBrakeRunner.Run(_preset, file)) { _logger.Info("Successfully converted {0}, deleting original", file); try { File.Delete(file); _deleteLogger.Info("Deleted {0}", file); } catch { _deleteLogger.Info("Failed to delete {0}", file); } } else { _logger.Info("Failed to converted {0}", file); } _fileInFlight.Remove(file); } }
private static void OnTimedEvent(object source, ElapsedEventArgs e) { var mkvs = new List <FileInfo>(); var foundWork = false; foreach (var r in _roots) { mkvs.AddRange(Directory.EnumerateFiles(r, "*.mkv", SearchOption.AllDirectories).Select(f => new FileInfo(f))); } foreach (var f in mkvs.Where(a => !_fileInFlight.Contains(a.FullName))) { var match = _exclusions.FirstOrDefault(a => f.FullName.Contains(a, StringComparison.OrdinalIgnoreCase)); if (string.IsNullOrEmpty(match)) { if (File.Exists(HandBrakeRunner.GetNewFileName(f.FullName))) { _logger.Info("Found mkv and associated mp4. Deleting mkv {0}", f.FullName); foundWork = true; try { if (!IsFileLocked(f)) { File.Delete(f.FullName); _deleteLogger.Info("Deleted {0}", f.FullName); } else { _logger.Info("{0} is in use", f.FullName); } } catch { _deleteLogger.Info("Failed to delete {0}", f); } } else if (File.Exists(HandBrakeRunner.GetTempFileName(f.FullName))) { if (!_fileInFlight.Contains(f.FullName)) { foundWork = false; _logger.Info("Found temp file that is not being processed{0}", HandBrakeRunner.GetTempFileName(f.FullName)); try { File.Delete(HandBrakeRunner.GetTempFileName(f.FullName)); } catch (Exception ex) { _logger.Warn(ex, "Failed to delete temp file {0}", HandBrakeRunner.GetTempFileName(f.FullName)); } } } else { if (f.CreationTimeUtc > DateTime.UtcNow - TimeSpan.FromDays(14) && f.CreationTimeUtc < DateTime.UtcNow - TimeSpan.FromHours(2)) { if (!IsFileLocked(f)) { foundWork = true; _logger.Info("Found mkv without associated mp4. Queuing mkv {0}", f.FullName); QueueFile(f.FullName); } } else { foundWork = true; _logger.Info("Found old mkv without associated mp4. {0}", f.FullName); } } } else { if (!_excludedFiles.Contains(f.FullName)) { foundWork = true; _exclusionLogger.Info("Skipping file {0} because it matches exclusion \"{1}\"", f.FullName, match); _excludedFiles.Add(f.FullName); } } } foreach (var r in _roots) { foreach (var l in Directory.EnumerateFiles(r, "*480p*.mp4", SearchOption.AllDirectories).Select(f => new FileInfo(f))) { var other = Directory.EnumerateFiles(l.DirectoryName, GetNameBeforeRes(l.FullName), SearchOption.TopDirectoryOnly) .FirstOrDefault(a => !a.Contains("480p", StringComparison.OrdinalIgnoreCase)); if (!string.IsNullOrWhiteSpace(other)) { foundWork = true; _logger.Info("Found {0}, deleting {1}", other, l.FullName); try { if (!IsFileLocked(l)) { File.Delete(l.FullName); _deleteLogger.Info("Deleted {0}", l.FullName); } else { _logger.Info("{0} is in use", l.FullName); } } catch { _deleteLogger.Info("Failed to delete {0}", l.FullName); } } } } if (!foundWork) { _logger.Info("found no new work"); } }