private async Task UpdateFileMetadataAsync() { bool restartTimer = false; this.updateFileMetadataTimer.Stop(); this.isUpdatingFileMetadata = true; var filesToSync = new List <FileMetadata>(); await Task.Run(() => { lock (lockObject) { int numberToProcess = this.fileMetadataDictionary.Count; if (numberToProcess == 0) { return; } while (numberToProcess > 0) { FileMetadata fmd = this.fileMetadataDictionary.First().Value; numberToProcess--; try { fmd.Save(); filesToSync.Add(fmd); this.fileMetadataDictionary.Remove(fmd.SafePath); } catch (Exception ex) { LogClient.Error("Unable to save metadata to the file for Track '{0}'. Exception: {1}", fmd.SafePath, ex.Message); } } // If there are still queued FileMetadata's, start the timer. if (this.fileMetadataDictionary.Count > 0) { restartTimer = true; } } }); // Sync file size and last modified date in the database foreach (FileMetadata fmd in filesToSync) { await this.trackRepository.UpdateTrackFileInformationAsync(fmd.SafePath); } this.updateFileMetadataTimer.Interval = this.updateFileMetadataLongTimeout; // The next time, wait longer. this.isUpdatingFileMetadata = false; if (restartTimer) { this.updateFileMetadataTimer.Start(); } }
public async Task UpdateFilemetadataAsync() { this.updateFileMetadataTimer.Stop(); this.isUpdatingFileMetadata = true; var filesToSync = new List <FileMetadata>(); await Task.Run(() => { lock (lockObject) { var failedFileMetadatas = new Queue <FileMetadata>(); while (this.fileMetadatas.Count > 0) { FileMetadata fmd = this.fileMetadatas.Dequeue(); try { fmd.Save(); filesToSync.Add(fmd); } catch (Exception ex) { LogClient.Instance.Logger.Error("Unable to save metadata to the file for Track '{0}'. Exception: {1}", fmd.FileName, ex.Message); failedFileMetadatas.Enqueue(fmd); } } // Make sure failed FileMetadata's are processed the next time the timer elapses if (failedFileMetadatas.Count > 0) { foreach (FileMetadata fmd in failedFileMetadatas) { this.fileMetadatas.Enqueue(fmd); } } } }); // Sync file size and last modified date in the database foreach (FileMetadata fmd in filesToSync) { await this.trackRepository.UpdateTrackFileInformationAsync(fmd.FileName); } this.updateFileMetadataTimer.Interval = this.updateFileMetadataLongTimeout; // The next time, wait longer. this.isUpdatingFileMetadata = false; this.updateFileMetadataTimer.Start(); }
private async Task UpdateFileMetadataAsync(bool canRetry) { bool mustStartTimer = false; this.updateFileMetadataTimer.Stop(); this.isUpdatingFileMetadata = true; var filesToSync = new List <FileMetadata>(); await Task.Run(() => { lock (fileMetadataToUpdateLock) { int numberToProcess = this.fileMetadataToUpdate.Count; if (numberToProcess == 0) { return; } while (numberToProcess > 0) { FileMetadata fmd = this.fileMetadataToUpdate.First().Value; numberToProcess--; try { fmd.Save(); filesToSync.Add(fmd); this.fileMetadataToUpdate.Remove(fmd.SafePath); } catch (IOException ex) { LogClient.Error("Unable to save metadata to the file for Track '{0}'. The file is probably playing. Trying again in {1} seconds. Exception: {2}", fmd.SafePath, this.updateMetadataLongTimeout / 1000, ex.Message); } catch (Exception ex) { LogClient.Error("Unable to save metadata to the file for Track '{0}'. Not trying again. Exception: {1}", fmd.SafePath, ex.Message); this.fileMetadataToUpdate.Remove(fmd.SafePath); } } // If there are still queued FileMetadata's, start the timer. if (this.fileMetadataToUpdate.Count > 0) { mustStartTimer = true; } } }); // Sync file size and last modified date in the database foreach (FileMetadata fmd in filesToSync) { await this.trackRepository.UpdateTrackFileInformationAsync(fmd.SafePath); } // The next time, wait longer. this.updateFileMetadataTimer.Interval = this.updateMetadataLongTimeout; this.isUpdatingFileMetadata = false; if (canRetry && mustStartTimer) { this.updateFileMetadataTimer.Start(); } }