From 85cd877b0c10e8dbb3bc17842e5a4f9abacc42cf Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 10 May 2013 22:59:42 -0700 Subject: [PATCH] fixed some issues here and there. --- NzbDrone.Api/REST/RestModule.cs | 1 + .../SignalR/BasicResourceConnection.cs | 10 +---- NzbDrone.Common/PathExtensions.cs | 6 +++ .../Datastore/ObjectDatabaseFixture.cs | 10 ++++- .../Converters/UtcDateTimeConverter.cs | 40 +++++++++++++++++++ NzbDrone.Core/Datastore/TableMapping.cs | 1 + NzbDrone.Core/Jobs/JobRepository.cs | 7 ---- NzbDrone.Core/Jobs/Scheduler.cs | 4 +- NzbDrone.Core/Jobs/TaskManager.cs | 2 +- NzbDrone.Core/NzbDrone.Core.csproj | 1 + NzbDrone.Core/Parser/Parser.cs | 2 +- NzbDrone/NLog.config | 2 +- 12 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 NzbDrone.Core/Datastore/Converters/UtcDateTimeConverter.cs diff --git a/NzbDrone.Api/REST/RestModule.cs b/NzbDrone.Api/REST/RestModule.cs index fdab8bf21..79e0f4b8c 100644 --- a/NzbDrone.Api/REST/RestModule.cs +++ b/NzbDrone.Api/REST/RestModule.cs @@ -135,6 +135,7 @@ namespace NzbDrone.Api.REST private TResource ReadFromRequest() { + //TODO: handle when request is null var resource = Request.Body.FromJson(); var errors = SharedValidator.Validate(resource).Errors.ToList(); diff --git a/NzbDrone.Api/SignalR/BasicResourceConnection.cs b/NzbDrone.Api/SignalR/BasicResourceConnection.cs index 31c6b2e53..e56ae4fd2 100644 --- a/NzbDrone.Api/SignalR/BasicResourceConnection.cs +++ b/NzbDrone.Api/SignalR/BasicResourceConnection.cs @@ -23,19 +23,13 @@ namespace NzbDrone.Api.SignalR protected override Task OnConnected(IRequest request, string connectionId) { - _logger.Debug("SignalR client connected. ID:{0}", connectionId); + _logger.Trace("SignalR client connected. ID:{0}", connectionId); return base.OnConnected(request, connectionId); } - public override Task ProcessRequest(Microsoft.AspNet.SignalR.Hosting.HostContext context) - { - _logger.Debug("Request: {0}", context); - return base.ProcessRequest(context); - } - public void HandleAsync(ModelEvent message) { - var context =((ConnectionManager)GlobalHost.ConnectionManager).GetConnection(GetType()); + var context = ((ConnectionManager)GlobalHost.ConnectionManager).GetConnection(GetType()); context.Connection.Broadcast(message); } } diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index 8ab70825b..b1feeab51 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -31,6 +31,12 @@ namespace NzbDrone.Common return info.FullName.TrimEnd('/').Trim('\\', ' '); } + + public static bool ContainsInvalidPathChars(this string text) + { + return text.IndexOfAny(Path.GetInvalidPathChars()) >= 0; + } + private static string GetProperDirectoryCapitalization(DirectoryInfo dirInfo) { var parentDirInfo = dirInfo.Parent; diff --git a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs index 588adb5fb..9abb24a99 100644 --- a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs +++ b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs @@ -88,10 +88,18 @@ namespace NzbDrone.Core.Test.Datastore } + [Test] + public void should_read_and_write_in_utc() + { + var storedTime = DateTime.UtcNow; + _sampleType.LastExecution =storedTime; + Subject.Insert(_sampleType); - + StoredModel.LastExecution.Kind.Should().Be(DateTimeKind.Utc); + StoredModel.LastExecution.ToLongTimeString().Should().Be(storedTime.ToLongTimeString()); + } [Test] public void should_have_id_when_returned_from_database() diff --git a/NzbDrone.Core/Datastore/Converters/UtcDateTimeConverter.cs b/NzbDrone.Core/Datastore/Converters/UtcDateTimeConverter.cs new file mode 100644 index 000000000..df738d5a5 --- /dev/null +++ b/NzbDrone.Core/Datastore/Converters/UtcDateTimeConverter.cs @@ -0,0 +1,40 @@ +using System; +using System.Globalization; +using Marr.Data.Converters; +using Marr.Data.Mapping; + +namespace NzbDrone.Core.Datastore.Converters +{ + public class UtcDateTimeConverter : IConverter + { + public Type DbType + { + get + { + return typeof(DateTime); + } + } + + public object FromDB(ColumnMap map, object dbValue) + { + if (dbValue != null && dbValue != DBNull.Value) + { + var dateTime = (DateTime)dbValue; + dateTime = new DateTime(dateTime.Ticks, DateTimeKind.Local); + return dateTime.ToUniversalTime(); + } + + return null; + } + + public object ToDB(object clrValue) + { + if (clrValue != null) + { + return ((DateTime)clrValue).ToUniversalTime(); + } + + return DBNull.Value; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/TableMapping.cs b/NzbDrone.Core/Datastore/TableMapping.cs index 5b19de082..2698ef354 100644 --- a/NzbDrone.Core/Datastore/TableMapping.cs +++ b/NzbDrone.Core/Datastore/TableMapping.cs @@ -78,6 +78,7 @@ namespace NzbDrone.Core.Datastore MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(Enum), new EnumIntConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(Quality), new QualityIntConverter()); + MapRepository.Instance.RegisterTypeConverter(typeof(DateTime), new UtcDateTimeConverter()); } private static void RegisterEmbeddedConverter() diff --git a/NzbDrone.Core/Jobs/JobRepository.cs b/NzbDrone.Core/Jobs/JobRepository.cs index 7c0f5a273..8bb721f7f 100644 --- a/NzbDrone.Core/Jobs/JobRepository.cs +++ b/NzbDrone.Core/Jobs/JobRepository.cs @@ -8,7 +8,6 @@ namespace NzbDrone.Core.Jobs { public interface IScheduledTaskRepository : IBasicRepository { - IList GetPendingJobs(); ScheduledTask GetDefinition(Type type); void SetLastExecutionTime(int id, DateTime executionTime); } @@ -27,12 +26,6 @@ namespace NzbDrone.Core.Jobs return Query.Single(c => c.TypeName == type.FullName); } - - public IList GetPendingJobs() - { - return Query.Where(c => c.Interval != 0).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList(); - } - public void SetLastExecutionTime(int id, DateTime executionTime) { var task = new ScheduledTask diff --git a/NzbDrone.Core/Jobs/Scheduler.cs b/NzbDrone.Core/Jobs/Scheduler.cs index 44d97c9dc..e5e79e326 100644 --- a/NzbDrone.Core/Jobs/Scheduler.cs +++ b/NzbDrone.Core/Jobs/Scheduler.cs @@ -35,6 +35,8 @@ namespace NzbDrone.Core.Jobs { var tasks = _taskManager.GetPending(); + _logger.Trace("Pending Tasks: {0}", tasks.Count); + foreach (var task in tasks) { try @@ -46,7 +48,7 @@ namespace NzbDrone.Core.Jobs } catch (Exception e) { - _logger.ErrorException("Error occured while execution task " + task.TypeName, e); + _logger.ErrorException("Error occurred while execution task " + task.TypeName, e); } finally { diff --git a/NzbDrone.Core/Jobs/TaskManager.cs b/NzbDrone.Core/Jobs/TaskManager.cs index 70cb49bc1..d2985fa0a 100644 --- a/NzbDrone.Core/Jobs/TaskManager.cs +++ b/NzbDrone.Core/Jobs/TaskManager.cs @@ -29,7 +29,7 @@ namespace NzbDrone.Core.Jobs public IList GetPending() { - return _scheduledTaskRepository.GetPendingJobs(); + return _scheduledTaskRepository.All().Where(c => c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow).ToList(); } public void SetLastExecutionTime(int taskId) diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index df8204e74..0de65433b 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -192,6 +192,7 @@ + diff --git a/NzbDrone.Core/Parser/Parser.cs b/NzbDrone.Core/Parser/Parser.cs index a87f66ca0..a9d156c69 100644 --- a/NzbDrone.Core/Parser/Parser.cs +++ b/NzbDrone.Core/Parser/Parser.cs @@ -322,7 +322,7 @@ namespace NzbDrone.Core.Parser } //Based on extension - if (result.Quality == Quality.Unknown) + if (result.Quality == Quality.Unknown && !name.ContainsInvalidPathChars()) { try { diff --git a/NzbDrone/NLog.config b/NzbDrone/NLog.config index 23f2d521e..6ed2d7f88 100644 --- a/NzbDrone/NLog.config +++ b/NzbDrone/NLog.config @@ -25,7 +25,7 @@ layout="${date:format=yy-M-d HH\:mm\:ss.f}|${logger}}|${level}|${message}|${exception:format=ToString}"/> - +