예제 #1
0
    /// <summary>
    /// Processor to drain the folder queue and update the DB. This
    ///
    /// </summary>
    /// <returns></returns>
    private async Task FolderQueueProcessor()
    {
        // Process the queue every 30s
        var timer = new PeriodicTimer(TimeSpan.FromSeconds(30));

        while (true)
        {
            // Wait until the next iteration
            await timer.WaitForNextTickAsync();

            // First, take all the queued folder changes and persist them to the DB
            // by setting the FolderScanDate to null.
            var folders = new List <string>();

            while (folderQueue.TryDequeue(out var folder))
            {
                Logging.Log($"Flagging change for folder: {folder}");
                folders.Add(folder);
            }

            if (_indexingService != null && folders.Any())
            {
                using var db = new ImageContext();

                var uniqueFolders  = folders.Distinct(StringComparer.OrdinalIgnoreCase);
                var pendingFolders = db.Folders.Where(f => uniqueFolders.Contains(f.Path)).ToList();

                // Call this method synchronously, we don't want to continue otherwise
                // we'll end up with race conditions as the timer triggers while
                // the method is completing.
                _indexingService.MarkFoldersForScan(pendingFolders).Wait();
            }
        }
    }