Skip to content

Commit

Permalink
Merge branch 'dev' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Feb 12, 2018
2 parents f507bd2 + 0f46245 commit bf717c1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/NuGet.Server.Core/Infrastructure/ServerPackageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ServerPackageRepository

private readonly bool _runBackgroundTasks;
private FileSystemWatcher _fileSystemWatcher;
private string _watchDirectory;
private bool _isFileSystemWatcherSuppressed;
private bool _needsRebuild;

Expand Down Expand Up @@ -548,6 +549,9 @@ private void RegisterFileSystemWatcher()
IncludeSubdirectories = true,
};

//Keep the normalized watch path.
_watchDirectory = Path.GetFullPath(_fileSystemWatcher.Path);

_fileSystemWatcher.Changed += FileSystemChangedAsync;
_fileSystemWatcher.Created += FileSystemChangedAsync;
_fileSystemWatcher.Deleted += FileSystemChangedAsync;
Expand Down Expand Up @@ -576,6 +580,8 @@ private void UnregisterFileSystemWatcher()

_logger.Log(LogLevel.Verbose, "Destroyed FileSystemWatcher - no longer monitoring {0}.", Source);
}

_watchDirectory = null;
}


Expand All @@ -593,16 +599,24 @@ private async void FileSystemChangedAsync(object sender, FileSystemEventArgs e)

_logger.Log(LogLevel.Verbose, "File system changed. File: {0} - Change: {1}", e.Name, e.ChangeType);

var changedDirectory = Path.GetDirectoryName(e.FullPath);
if (changedDirectory == null || _watchDirectory == null)
{
return;
}

changedDirectory = Path.GetFullPath(changedDirectory);

// 1) If a .nupkg is dropped in the root, add it as a package
if (string.Equals(Path.GetDirectoryName(e.FullPath), _fileSystemWatcher.Path, StringComparison.OrdinalIgnoreCase)
if (string.Equals(changedDirectory, _watchDirectory, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Path.GetExtension(e.Name), ".nupkg", StringComparison.OrdinalIgnoreCase))
{
// When a package is dropped into the server packages root folder, add it to the repository.
await AddPackagesFromDropFolderAsync(CancellationToken.None);
}

// 2) If a file is updated in a subdirectory, *or* a folder is deleted, invalidate the cache
if ((!string.Equals(Path.GetDirectoryName(e.FullPath), _fileSystemWatcher.Path, StringComparison.OrdinalIgnoreCase) && File.Exists(e.FullPath))
if ((!string.Equals(changedDirectory, _watchDirectory, StringComparison.OrdinalIgnoreCase) && File.Exists(e.FullPath))
|| e.ChangeType == WatcherChangeTypes.Deleted)
{
// TODO: invalidating *all* packages for every nupkg change under this folder seems more expensive than it should.
Expand Down
7 changes: 7 additions & 0 deletions src/NuGet.Server/App_Start/NuGetODataConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
using System.Web.Http.Routing;
using NuGet.Server.DataServices;
using NuGet.Server.Infrastructure;
using NuGet.Server.V2;

// The consuming project executes this logic with its own copy of this class. This is done with a .pp file that is
Expand All @@ -28,6 +30,11 @@ public static void Initialize(HttpConfiguration config, string controllerName)
{
NuGetV2WebApiEnabler.UseNuGetV2WebApiFeed(config, "NuGetDefault", "nuget", controllerName);

config.Services.Replace(typeof(IExceptionLogger), new TraceExceptionLogger());

// Trace.Listeners.Add(new TextWriterTraceListener(HostingEnvironment.MapPath("~/NuGet.Server.log")));
// Trace.AutoFlush = true;

config.Routes.MapHttpRoute(
name: "NuGetDefault_ClearCache",
routeTemplate: "nuget/clear-cache",
Expand Down
7 changes: 7 additions & 0 deletions src/NuGet.Server/App_Start/NuGetODataConfig.cs.pp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
using System.Web.Http.Routing;
using NuGet.Server;
using NuGet.Server.Infrastructure;
using NuGet.Server.V2;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof($rootnamespace$.App_Start.NuGetODataConfig), "Start")]
Expand All @@ -18,6 +20,11 @@

NuGetV2WebApiEnabler.UseNuGetV2WebApiFeed(config, "NuGetDefault", "nuget", "PackagesOData");

config.Services.Replace(typeof(IExceptionLogger), new TraceExceptionLogger());

// Trace.Listeners.Add(new TextWriterTraceListener(HostingEnvironment.MapPath("~/NuGet.Server.log")));
// Trace.AutoFlush = true;

config.Routes.MapHttpRoute(
name: "NuGetDefault_ClearCache",
routeTemplate: "nuget/clear-cache",
Expand Down
16 changes: 16 additions & 0 deletions src/NuGet.Server/Infrastructure/TraceExceptionLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics;
using System.Web.Http.ExceptionHandling;

namespace NuGet.Server.Infrastructure
{
public class TraceExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
Trace.TraceError(context.ExceptionContext.Exception.ToString());
}
}
}
1 change: 1 addition & 0 deletions src/NuGet.Server/NuGet.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="Core\ServiceResolver.cs" />
<Compile Include="Controllers\PackagesODataController.cs" />
<Compile Include="Core\ServiceResolverExtensions.cs" />
<Compile Include="Infrastructure\TraceExceptionLogger.cs" />
<Compile Include="Infrastructure\WebConfigSettingsProvider.cs" />
<Compile Include="Core\Helpers.cs" />
<Compile Include="Infrastructure\PackageAuthenticationService.cs" />
Expand Down

0 comments on commit bf717c1

Please sign in to comment.