protected override async Task OnRun() { var position = 0; using (var task = new SingletonReentrantTask(this, ComponentSlots.Database, SingletonReentrantTask.PRIORITY_LOW, async cancellationToken => { var metaDataSource = this.MetaDataSourceFactory.Create(); foreach (var libraryItem in this.LibraryItems) { this.Description = Path.GetFileName(libraryItem.FileName); this.Position = position; if (!File.Exists(libraryItem.FileName)) { Logger.Write(this, LogLevel.Debug, "File \"{0}\" no longer exists: Cannot refresh.", libraryItem.FileName); continue; } var metaDatas = await metaDataSource.GetMetaData(libraryItem.FileName).ConfigureAwait(false); MetaDataItem.Update(metaDatas, libraryItem.MetaDatas, null); await this.WriteLibraryMetaData(libraryItem).ConfigureAwait(false); libraryItem.Status = LibraryItemStatus.Import; await LibraryTaskBase.UpdateLibraryItem(this.Database, libraryItem).ConfigureAwait(false); position++; } })) { await task.Run().ConfigureAwait(false); } }
private async Task WriteLibraryMetaData(LibraryItem libraryItem) { using (var transaction = this.Database.BeginTransaction(this.Database.PreferredIsolationLevel)) { await this.Database.ExecuteAsync(this.Database.Queries.ClearLibraryMetaDataItems, (parameters, phase) => { switch (phase) { case DatabaseParameterPhase.Fetch: parameters["itemId"] = libraryItem.Id; parameters["type"] = META_DATA_TYPE; break; } }, transaction).ConfigureAwait(false); using (var writer = new MetaDataWriter(this.Database, this.Database.Queries.AddLibraryMetaDataItem, transaction)) { await writer.Write( libraryItem.Id, libraryItem.MetaDatas, metaDataItem => META_DATA_TYPE.HasFlag(metaDataItem.Type) ).ConfigureAwait(false); } libraryItem.ImportDate = DateTimeHelper.ToString(DateTime.UtcNow.AddSeconds(30)); await LibraryTaskBase.UpdateLibraryItem(this.Database, libraryItem, transaction).ConfigureAwait(false); if (transaction.HasTransaction) { transaction.Commit(); } } }
protected override async Task OnRun() { var position = 0; using (var task = new SingletonReentrantTask(this, ComponentSlots.Database, SingletonReentrantTask.PRIORITY_LOW, async cancellationToken => { var metaDataSource = this.MetaDataSourceFactory.Create(); foreach (var libraryItem in this.LibraryItems) { await this.SetDescription(new FileInfo(libraryItem.FileName).Name).ConfigureAwait(false); await this.SetPosition(position).ConfigureAwait(false); if (!File.Exists(libraryItem.FileName)) { Logger.Write(this, LogLevel.Debug, "File \"{0}\" no longer exists: Cannot refresh.", libraryItem.FileName); continue; } libraryItem.MetaDatas = new ObservableCollection <MetaDataItem>( await metaDataSource.GetMetaData(libraryItem.FileName) .ConfigureAwait(false)); await this.WriteLibraryMetaData(libraryItem).ConfigureAwait(false); await LibraryTaskBase.SetLibraryItemStatus(this.Database, libraryItem.Id, LibraryItemStatus.Import).ConfigureAwait(false); position++; } })) { await task.Run().ConfigureAwait(false); } }
protected override async Task OnRun() { var position = 0; using (var task = new SingletonReentrantTask(this, ComponentSlots.Database, SingletonReentrantTask.PRIORITY_LOW, async cancellationToken => { var metaDataSource = this.MetaDataSourceFactory.Create(); foreach (var libraryItem in this.LibraryItems) { if (this.IsCancellationRequested) { break; } await this.SetDescription(new FileInfo(libraryItem.FileName).Name).ConfigureAwait(false); await this.SetPosition(position).ConfigureAwait(false); if (!File.Exists(libraryItem.FileName)) { Logger.Write(this, LogLevel.Debug, "File \"{0}\" no longer exists: Cannot update.", libraryItem.FileName); this.Errors.Add(libraryItem, new FileNotFoundException(string.Format("File \"{0}\" no longer exists: Cannot update.", libraryItem.FileName))); position++; continue; } try { await metaDataSource.SetMetaData( libraryItem.FileName, libraryItem.MetaDatas, metaDataItem => this.Names == null || !this.Names.Any() || this.Names.Contains(metaDataItem.Name, true) ).ConfigureAwait(false); } catch (Exception e) { this.Errors.Add(libraryItem, e); position++; continue; } foreach (var metaDataItem in libraryItem.MetaDatas.ToArray()) { if (!string.IsNullOrEmpty(metaDataItem.Value)) { continue; } libraryItem.MetaDatas.Remove(metaDataItem); } await this.WriteLibraryMetaData(libraryItem).ConfigureAwait(false); await LibraryTaskBase.SetLibraryItemStatus(this.Database, libraryItem.Id, LibraryItemStatus.Import).ConfigureAwait(false); position++; } })) { await task.Run().ConfigureAwait(false); } }
protected override async Task OnCompleted() { await base.OnCompleted().ConfigureAwait(false); //We don't need a lock for this so not performing in OnRun(). var names = new HashSet <string>(StringComparer.OrdinalIgnoreCase); names.AddRange(LibraryTaskBase.UpdateLibraryCache(this.LibraryCache, this.LibraryItems, null)); names.AddRange(LibraryTaskBase.UpdatePlaylistCache(this.PlaylistCache, this.LibraryItems, null)); await this.SignalEmitter.Send(new Signal(this, CommonSignals.MetaDataUpdated, names)).ConfigureAwait(false); }
protected virtual Task Schedule(PlaylistItem playlistItem) { playlistItem.Flags |= PlaylistItemFlags.Export; if (playlistItem.LibraryItem_Id.HasValue) { return(LibraryTaskBase.UpdateLibraryItem( this.Database, playlistItem.LibraryItem_Id.Value, libraryItem => libraryItem.Flags |= LibraryItemFlags.Export )); } return(PlaylistTaskBase.UpdatePlaylistItem(this.Database, playlistItem)); }
protected override async Task OnRun() { var position = 0; using (var task = new SingletonReentrantTask(this, ComponentSlots.Database, SingletonReentrantTask.PRIORITY_HIGH, async cancellationToken => { foreach (var playlistItem in this.PlaylistItems) { if (this.IsCancellationRequested) { break; } if (this.Visible) { this.Description = Path.GetFileName(playlistItem.FileName); this.Position = position; } if (playlistItem.LibraryItem_Id.HasValue) { await this.WriteLibraryMetaData(playlistItem).ConfigureAwait(false); await LibraryTaskBase.UpdateLibraryItem( this.Database, playlistItem.LibraryItem_Id.Value, libraryItem => libraryItem.Status = LibraryItemStatus.Import ).ConfigureAwait(false); } else { await this.WritePlaylistMetaData(playlistItem).ConfigureAwait(false); } if (this.WriteToFiles) { if (!await this.MetaDataManager.Synchronize(new[] { playlistItem }, this.Names.ToArray()).ConfigureAwait(false)) { this.AddError(playlistItem, string.Format("Failed to write meta data to file \"{0}\". We will try again later.", playlistItem.FileName)); } } position++; } })) { await task.Run().ConfigureAwait(false); } }
protected override async Task OnStarted() { if (this.Visible) { this.Name = "Saving meta data"; this.Position = 0; this.Count = this.LibraryItems.Count(); } await base.OnStarted().ConfigureAwait(false); //We don't need a lock for this so not waiting for OnRun(). var names = new HashSet <string>(StringComparer.OrdinalIgnoreCase); names.AddRange(LibraryTaskBase.UpdateLibraryCache(this.LibraryCache, this.LibraryItems, this.Names)); names.AddRange(LibraryTaskBase.UpdatePlaylistCache(this.PlaylistCache, this.LibraryItems, this.Names)); }
protected virtual async Task Deschedule(PlaylistItem playlistItem) { playlistItem.Flags &= ~PlaylistItemFlags.Export; if (playlistItem.LibraryItem_Id.HasValue) { await LibraryTaskBase.UpdateLibraryItem( this.Database, playlistItem.LibraryItem_Id.Value, libraryItem => { libraryItem.ImportDate = DateTimeHelper.ToString(DateTime.UtcNow.AddSeconds(30)); libraryItem.Flags &= ~LibraryItemFlags.Export; } ).ConfigureAwait(false); } await PlaylistTaskBase.UpdatePlaylistItem(this.Database, playlistItem).ConfigureAwait(false); }
public void InitializeDatabase(IDatabaseComponent database, DatabaseInitializeType type) { //IMPORTANT: When editing this function remember to change the checksum. if (!type.HasFlag(DatabaseInitializeType.Library)) { return; } var scriptingRuntime = ComponentRegistry.Instance.GetComponent <IScriptingRuntime>(); if (scriptingRuntime == null) { return; } using (var transaction = database.BeginTransaction(database.PreferredIsolationLevel)) { var set = database.Set <LibraryHierarchy>(transaction); foreach (var libraryHierarchy in set) { //TODO: Bad .Wait() LibraryTaskBase.RemoveHierarchies(database, libraryHierarchy, null, transaction).Wait(); } set.Clear(); set.Add(new LibraryHierarchy() { Name = "Artist/Album/Title", Type = LibraryHierarchyType.Script, Sequence = 0, Enabled = true, Levels = new ObservableCollection <LibraryHierarchyLevel>() { new LibraryHierarchyLevel() { Sequence = 0, Script = scriptingRuntime.CoreScripts.Artist }, new LibraryHierarchyLevel() { Sequence = 1, Script = scriptingRuntime.CoreScripts.Year_Album }, new LibraryHierarchyLevel() { Sequence = 2, Script = scriptingRuntime.CoreScripts.Disk_Track_Title } } }); set.Add(new LibraryHierarchy() { Name = "Genre/Album/Title", Type = LibraryHierarchyType.Script, Sequence = 1, Enabled = false, Levels = new ObservableCollection <LibraryHierarchyLevel>() { new LibraryHierarchyLevel() { Sequence = 0, Script = scriptingRuntime.CoreScripts.Genre }, new LibraryHierarchyLevel() { Sequence = 1, Script = scriptingRuntime.CoreScripts.Year_Album }, new LibraryHierarchyLevel() { Sequence = 2, Script = scriptingRuntime.CoreScripts.Disk_Track_Title } } }); set.Add(new LibraryHierarchy() { Name = "Genre/Rating/Artist - Title [BPM]", Type = LibraryHierarchyType.Script, Sequence = 2, Enabled = false, Levels = new ObservableCollection <LibraryHierarchyLevel>() { new LibraryHierarchyLevel() { Sequence = 0, Script = scriptingRuntime.CoreScripts.Genre }, new LibraryHierarchyLevel() { Sequence = 1, Script = scriptingRuntime.CoreScripts.Rating }, new LibraryHierarchyLevel() { Sequence = 2, Script = scriptingRuntime.CoreScripts.Artist_Title_BPM } } }); set.Add(new LibraryHierarchy() { Name = "Folder Structure", Type = LibraryHierarchyType.FileSystem, Sequence = 3, Enabled = false }); transaction.Commit(); } }
protected virtual Task Deschedule(LibraryItem libraryItem) { libraryItem.ImportDate = DateTimeHelper.ToString(DateTime.UtcNow.AddSeconds(30)); libraryItem.Flags &= ~LibraryItemFlags.Export; return(LibraryTaskBase.UpdateLibraryItem(this.Database, libraryItem)); }
protected virtual Task Schedule(LibraryItem libraryItem) { libraryItem.Flags |= LibraryItemFlags.Export; return(LibraryTaskBase.UpdateLibraryItem(this.Database, libraryItem)); }