From 64818ebd223880d1ef7d7173b10968077d2378b0 Mon Sep 17 00:00:00 2001
From: Luke Pulverenti <luke.pulverenti@gmail.com>
Date: Tue, 26 Nov 2013 21:38:11 -0500
Subject: [PATCH] fix directory watchers not picking up changes

---
 .../Library/LibraryStructureService.cs        | 33 ++++++++++++++++---
 MediaBrowser.Api/TvShowsService.cs            | 12 +------
 .../LiveTv/ILiveTvManager.cs                  |  4 +--
 MediaBrowser.Model/LiveTv/ProgramInfoDto.cs   | 12 +++++++
 .../TV/EpisodeProviderFromXml.cs              |  1 -
 .../IO/DirectoryWatchers.cs                   |  2 +-
 .../Library/LibraryManager.cs                 | 14 ++++++++
 .../LiveTv/RefreshChannelsScheduledTask.cs    |  2 +-
 .../ApplicationHost.cs                        |  2 --
 9 files changed, 59 insertions(+), 23 deletions(-)

diff --git a/MediaBrowser.Api/Library/LibraryStructureService.cs b/MediaBrowser.Api/Library/LibraryStructureService.cs
index f3306bb63c..198bec1a0d 100644
--- a/MediaBrowser.Api/Library/LibraryStructureService.cs
+++ b/MediaBrowser.Api/Library/LibraryStructureService.cs
@@ -286,7 +286,12 @@ namespace MediaBrowser.Api.Library
             }
             finally
             {
-                _directoryWatchers.Start();
+                // No need to start if scanning the library because it will handle it
+                if (!request.RefreshLibrary)
+                {
+                    _directoryWatchers.Start();
+                }
+
                 _directoryWatchers.RemoveTempIgnore(virtualFolderPath);
             }
 
@@ -353,7 +358,12 @@ namespace MediaBrowser.Api.Library
             }
             finally
             {
-                _directoryWatchers.Start();
+                // No need to start if scanning the library because it will handle it
+                if (!request.RefreshLibrary)
+                {
+                    _directoryWatchers.Start();
+                }
+
                 _directoryWatchers.RemoveTempIgnore(currentPath);
                 _directoryWatchers.RemoveTempIgnore(newPath);
             }
@@ -404,7 +414,12 @@ namespace MediaBrowser.Api.Library
             }
             finally
             {
-                _directoryWatchers.Start();
+                // No need to start if scanning the library because it will handle it
+                if (!request.RefreshLibrary)
+                {
+                    _directoryWatchers.Start();
+                }
+
                 _directoryWatchers.RemoveTempIgnore(path);
             }
 
@@ -442,7 +457,11 @@ namespace MediaBrowser.Api.Library
             }
             finally
             {
-                _directoryWatchers.Start();
+                // No need to start if scanning the library because it will handle it
+                if (!request.RefreshLibrary)
+                {
+                    _directoryWatchers.Start();
+                }
             }
 
             if (request.RefreshLibrary)
@@ -479,7 +498,11 @@ namespace MediaBrowser.Api.Library
             }
             finally
             {
-                _directoryWatchers.Start();
+                // No need to start if scanning the library because it will handle it
+                if (!request.RefreshLibrary)
+                {
+                    _directoryWatchers.Start();
+                }
             }
 
             if (request.RefreshLibrary)
diff --git a/MediaBrowser.Api/TvShowsService.cs b/MediaBrowser.Api/TvShowsService.cs
index 1774f1a8ed..b36005c1aa 100644
--- a/MediaBrowser.Api/TvShowsService.cs
+++ b/MediaBrowser.Api/TvShowsService.cs
@@ -372,22 +372,12 @@ namespace MediaBrowser.Api
                 return episodes.Where(i => (i.PhysicalSeasonNumber ?? -1) == seasonNumber);
             }
 
-            var episodeList = episodes.ToList();
-
-            // We can only enforce the air date requirement if the episodes have air dates
-            var enforceAirDate = episodeList.Any(i => i.PremiereDate.HasValue);
-
-            return episodeList.Where(i =>
+            return episodes.Where(i =>
             {
                 var episode = i;
 
                 if (episode != null)
                 {
-                    if (enforceAirDate && !episode.PremiereDate.HasValue)
-                    {
-                        return false;
-                    }
-
                     var currentSeasonNumber = episode.AiredSeasonNumber;
 
                     return currentSeasonNumber.HasValue && currentSeasonNumber.Value == seasonNumber;
diff --git a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs
index a04072d28d..91634b4bfb 100644
--- a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs
+++ b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs
@@ -1,7 +1,7 @@
-using System.Threading;
-using MediaBrowser.Model.LiveTv;
+using MediaBrowser.Model.LiveTv;
 using MediaBrowser.Model.Querying;
 using System.Collections.Generic;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace MediaBrowser.Controller.LiveTv
diff --git a/MediaBrowser.Model/LiveTv/ProgramInfoDto.cs b/MediaBrowser.Model/LiveTv/ProgramInfoDto.cs
index 2cafd288df..26cfd3cf00 100644
--- a/MediaBrowser.Model/LiveTv/ProgramInfoDto.cs
+++ b/MediaBrowser.Model/LiveTv/ProgramInfoDto.cs
@@ -100,6 +100,18 @@ namespace MediaBrowser.Model.LiveTv
         /// </summary>
         /// <value>The recording status.</value>
         public RecordingStatus? RecordingStatus { get; set; }
+
+        /// <summary>
+        /// Gets or sets the timer identifier.
+        /// </summary>
+        /// <value>The timer identifier.</value>
+        public string TimerId { get; set; }
+
+        /// <summary>
+        /// Gets or sets the timer status.
+        /// </summary>
+        /// <value>The timer status.</value>
+        public RecordingStatus? TimerStatus { get; set; }
         
         public ProgramInfoDto()
         {
diff --git a/MediaBrowser.Providers/TV/EpisodeProviderFromXml.cs b/MediaBrowser.Providers/TV/EpisodeProviderFromXml.cs
index b6fdaaa831..7ddf421b9b 100644
--- a/MediaBrowser.Providers/TV/EpisodeProviderFromXml.cs
+++ b/MediaBrowser.Providers/TV/EpisodeProviderFromXml.cs
@@ -2,7 +2,6 @@
 using MediaBrowser.Controller.Configuration;
 using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Entities.TV;
-using MediaBrowser.Controller.IO;
 using MediaBrowser.Controller.Persistence;
 using MediaBrowser.Controller.Providers;
 using MediaBrowser.Model.Entities;
diff --git a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
index b24cc20635..870a14bd80 100644
--- a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
+++ b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
@@ -69,7 +69,7 @@ namespace MediaBrowser.Server.Implementations.IO
             // This is an arbitraty amount of time, but delay it because file system writes often trigger events after RemoveTempIgnore has been called. 
             // Seeing long delays in some situations, especially over the network.
             // Seeing delays up to 40 seconds, but not going to ignore changes for that long.
-            await Task.Delay(20000).ConfigureAwait(false);
+            await Task.Delay(1500).ConfigureAwait(false);
 
             string val;
             _tempIgnoredPaths.TryRemove(path, out val);
diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs
index 58a2fcd7ed..74c4f8b2a8 100644
--- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs
+++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs
@@ -905,6 +905,20 @@ namespace MediaBrowser.Server.Implementations.Library
         /// <param name="cancellationToken">The cancellation token.</param>
         /// <returns>Task.</returns>
         public async Task ValidateMediaLibraryInternal(IProgress<double> progress, CancellationToken cancellationToken)
+        {
+            _directoryWatchersFactory().Stop();
+
+            try
+            {
+                await PerformLibraryValidation(progress, cancellationToken).ConfigureAwait(false);
+            }
+            finally
+            {
+                _directoryWatchersFactory().Start();
+            }
+        }
+
+        private async Task PerformLibraryValidation(IProgress<double> progress, CancellationToken cancellationToken)
         {
             _logger.Info("Validating media library");
 
diff --git a/MediaBrowser.Server.Implementations/LiveTv/RefreshChannelsScheduledTask.cs b/MediaBrowser.Server.Implementations/LiveTv/RefreshChannelsScheduledTask.cs
index c3803d9bbc..3e7feeff79 100644
--- a/MediaBrowser.Server.Implementations/LiveTv/RefreshChannelsScheduledTask.cs
+++ b/MediaBrowser.Server.Implementations/LiveTv/RefreshChannelsScheduledTask.cs
@@ -7,7 +7,7 @@ using System.Threading.Tasks;
 
 namespace MediaBrowser.Server.Implementations.LiveTv
 {
-    class RefreshChannelsScheduledTask : IScheduledTask
+    class RefreshChannelsScheduledTask 
     {
         private readonly ILiveTvManager _liveTvManager;
 
diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs
index c53277d778..fcd7d299cf 100644
--- a/MediaBrowser.ServerApplication/ApplicationHost.cs
+++ b/MediaBrowser.ServerApplication/ApplicationHost.cs
@@ -199,8 +199,6 @@ namespace MediaBrowser.ServerApplication
         {
             await base.RunStartupTasks().ConfigureAwait(false);
 
-            DirectoryWatchers.Start();
-
             Logger.Info("Core startup complete");
 
             Parallel.ForEach(GetExports<IServerEntryPoint>(), entryPoint =>