From 84b7629831a1b5cc490f05b8ec3ca75197d59c39 Mon Sep 17 00:00:00 2001 From: anurse Date: Wed, 5 Mar 2014 11:56:01 -0800 Subject: [PATCH] Fix NuGet/NuGetGallery#1940 by adding search service jobs --- src/JobHost/Program.cs | 7 ++++ .../Jobs/Bases/SearchIndexJobHandlerBase.cs | 1 + .../Jobs/GenerateSearchRankingsJob.cs | 34 +++++++-------- .../Jobs/Models/SearchRankingReport.cs | 20 --------- .../Jobs/RebuildSearchIndexJob.cs | 42 +++++++++++++++++-- .../Jobs/UpdateSearchIndexJob.cs | 8 +++- .../Monitoring/EventSourceWriter.cs | 2 +- .../NuGet.Services.Work.csproj | 11 ++--- src/NuGet.Services.Work/WorkService.cs | 28 +++++++------ src/NuGet.Services.Work/packages.config | 2 +- 10 files changed, 90 insertions(+), 65 deletions(-) delete mode 100644 src/NuGet.Services.Work/Jobs/Models/SearchRankingReport.cs diff --git a/src/JobHost/Program.cs b/src/JobHost/Program.cs index 20421b4..3f38401 100644 --- a/src/JobHost/Program.cs +++ b/src/JobHost/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Diagnostics.Tracing; using System.Linq; using System.Text; @@ -13,6 +14,12 @@ class Program { static void Main(string[] args) { + if (args.Length > 0 && String.Equals(args[0], "dbg", StringComparison.OrdinalIgnoreCase)) + { + args = args.Skip(1).ToArray(); + Debugger.Launch(); + } + Arguments parsed; try { diff --git a/src/NuGet.Services.Work/Jobs/Bases/SearchIndexJobHandlerBase.cs b/src/NuGet.Services.Work/Jobs/Bases/SearchIndexJobHandlerBase.cs index f6e64ef..9a80efd 100644 --- a/src/NuGet.Services.Work/Jobs/Bases/SearchIndexJobHandlerBase.cs +++ b/src/NuGet.Services.Work/Jobs/Bases/SearchIndexJobHandlerBase.cs @@ -17,6 +17,7 @@ public abstract class SearchIndexJobHandlerBase : JobHandler public SqlConnectionStringBuilder PackageDatabase { get; set; } public CloudStorageAccount StorageAccount { get; set; } public string StorageContainerName { get; set; } + public string LocalIndexFolder { get; set; } protected ConfigurationHub Config { get; set; } diff --git a/src/NuGet.Services.Work/Jobs/GenerateSearchRankingsJob.cs b/src/NuGet.Services.Work/Jobs/GenerateSearchRankingsJob.cs index 6159741..8044282 100644 --- a/src/NuGet.Services.Work/Jobs/GenerateSearchRankingsJob.cs +++ b/src/NuGet.Services.Work/Jobs/GenerateSearchRankingsJob.cs @@ -9,6 +9,7 @@ using Dapper; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NuGet.Services.Client; using NuGet.Services.Configuration; @@ -56,8 +57,10 @@ protected internal override async Task Execute() } // Gather overall rankings + JObject report = new JObject(); Log.GatheringOverallRankings(WarehouseConnection.DataSource, WarehouseConnection.InitialCatalog); var overallData = await GatherOverallRankings(); + report.Add("Rank", overallData); Log.GatheredOverallRankings(overallData.Count); // Get project types @@ -66,27 +69,18 @@ protected internal override async Task Execute() Log.GotAvailableProjectTypes(projectTypes.Count); // Gather data by project type - IDictionary> byProjectType = new Dictionary>(); int count = 0; Log.GatheringProjectTypeRankings(WarehouseConnection.DataSource, WarehouseConnection.InitialCatalog); foreach (var projectType in projectTypes) { Log.GatheringProjectTypeRanking(WarehouseConnection.DataSource, WarehouseConnection.InitialCatalog, projectType); var data = await GatherProjectTypeRanking(projectType); + report.Add(projectType, data); Log.GatheredProjectTypeRanking(data.Count, projectType); count += data.Count; - - byProjectType.Add(projectType, data); } Log.GatheredProjectTypeRankings(count); - // Generate the report - var report = new SearchRankingReport() - { - Overall = overallData, - ByProjectType = byProjectType - }; - // Write the JSON blob if (!String.IsNullOrEmpty(OutputDirectory)) { @@ -99,7 +93,7 @@ protected internal override async Task Execute() } } - private async Task WriteToFile(SearchRankingReport report) + private async Task WriteToFile(JObject report) { string fullPath = Path.Combine(OutputDirectory, ReportName); string parentDir = Path.GetDirectoryName(fullPath); @@ -116,20 +110,20 @@ private async Task WriteToFile(SearchRankingReport report) } using (var writer = new StreamWriter(File.OpenWrite(fullPath))) { - await writer.WriteAsync(JsonFormat.Serialize(report)); + await writer.WriteAsync(report.ToString(Formatting.Indented)); } } Log.WroteReportBlob(fullPath); } - private async Task WriteToBlob(SearchRankingReport report) + private async Task WriteToBlob(JObject report) { var blob = DestinationContainer.GetBlockBlobReference(ReportName); Log.WritingReportBlob(blob.Uri.AbsoluteUri); if (!WhatIf) { blob.Properties.ContentType = MimeTypes.Json; - await blob.UploadTextAsync(JsonFormat.Serialize(report)); + await blob.UploadTextAsync(report.ToString(Formatting.Indented)); } Log.WroteReportBlob(blob.Uri.AbsoluteUri); } @@ -145,7 +139,7 @@ private void LoadDefaults() } } - private async Task> GatherOverallRankings() + private async Task GatherOverallRankings() { using (var connection = await WarehouseConnection.ConnectTo()) { @@ -153,7 +147,9 @@ private async Task> GatherOverallRankings() var script = await ResourceHelpers.ReadResourceFile("NuGet.Services.Work.Jobs.Scripts.SearchRanking_Overall.sql"); // Execute it and return the results - return (await connection.QueryAsync(script)).ToList(); + return new JArray( + (await connection.QueryAsync(script)) + .Select(e => e.PackageId)); } } @@ -166,7 +162,7 @@ private async Task> GetProjectTypes() } } - private async Task> GatherProjectTypeRanking(string projectType) + private async Task GatherProjectTypeRanking(string projectType) { using (var connection = await WarehouseConnection.ConnectTo()) { @@ -174,7 +170,9 @@ private async Task> GatherProjectTypeRanking(string pr var script = await ResourceHelpers.ReadResourceFile("NuGet.Services.Work.Jobs.Scripts.SearchRanking_ByProjectType.sql"); // Execute it and return the results - return (await connection.QueryAsync(script, new { ProjectGuid = projectType })).ToList(); + return new JArray( + (await connection.QueryAsync(script, new { ProjectGuid = projectType })) + .Select(e => e.PackageId)); } } } diff --git a/src/NuGet.Services.Work/Jobs/Models/SearchRankingReport.cs b/src/NuGet.Services.Work/Jobs/Models/SearchRankingReport.cs deleted file mode 100644 index fac82a9..0000000 --- a/src/NuGet.Services.Work/Jobs/Models/SearchRankingReport.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NuGet.Services.Work.Jobs.Models -{ - public class SearchRankingReport - { - public IList Overall { get; set; } - public IDictionary> ByProjectType { get; set; } - - public SearchRankingReport() - { - Overall = new List(); - ByProjectType = new Dictionary>(); - } - } -} diff --git a/src/NuGet.Services.Work/Jobs/RebuildSearchIndexJob.cs b/src/NuGet.Services.Work/Jobs/RebuildSearchIndexJob.cs index 6651c4e..bca4e29 100644 --- a/src/NuGet.Services.Work/Jobs/RebuildSearchIndexJob.cs +++ b/src/NuGet.Services.Work/Jobs/RebuildSearchIndexJob.cs @@ -18,29 +18,63 @@ public class RebuildSearchIndexJob : SearchIndexJobHandlerBaseFalse ..\..\packages\Nuget.Core.2.8.0\lib\net40-Client\NuGet.Core.dll - - ..\..\packages\NuGet.Indexing.3.0.2-alpha-4\lib\net45\NuGet.Indexing.dll + + False + ..\..\packages\NuGet.Indexing.3.0.2-alpha-9\lib\net45\NuGet.Indexing.dll False @@ -181,9 +182,6 @@ ..\..\packages\Rx-Linq.2.2.2\lib\net45\System.Reactive.Linq.dll - - ..\..\packages\Rx-PlatformServices.2.2.2\lib\net45\System.Reactive.PlatformServices.dll - @@ -252,7 +250,6 @@ - @@ -336,4 +333,4 @@ --> - + \ No newline at end of file diff --git a/src/NuGet.Services.Work/WorkService.cs b/src/NuGet.Services.Work/WorkService.cs index 6c8fca2..ab1df46 100644 --- a/src/NuGet.Services.Work/WorkService.cs +++ b/src/NuGet.Services.Work/WorkService.cs @@ -210,22 +210,24 @@ public IObservable RunJob(string job, string payload) QueuedAt = DateTime.UtcNow, NextVisibleAt = DateTime.UtcNow + TimeSpan.FromMinutes(5) }); - var buffer = new ReplaySubject(); - var capture = new InvocationLogCapture(invocation); - capture.Subscribe(buffer.OnNext, buffer.OnError); - runner.Dispatch(invocation, capture, CancellationToken.None).ContinueWith(t => + return Observable.Create(observer => { - if (t.IsFaulted) + var capture = new InvocationLogCapture(invocation); + capture.Subscribe(e => observer.OnNext(e), ex => observer.OnError(ex)); + runner.Dispatch(invocation, capture, CancellationToken.None).ContinueWith(t => { - buffer.OnError(t.Exception); - } - else - { - buffer.OnCompleted(); - } - return t; + if (t.IsFaulted) + { + observer.OnError(t.Exception); + } + else + { + observer.OnCompleted(); + } + return t; + }); + return () => { }; // No action on unsubscribe }); - return buffer; } protected override void ConfigureAttributeRouting(DefaultInlineConstraintResolver resolver) diff --git a/src/NuGet.Services.Work/packages.config b/src/NuGet.Services.Work/packages.config index 9bfa3a2..c83f187 100644 --- a/src/NuGet.Services.Work/packages.config +++ b/src/NuGet.Services.Work/packages.config @@ -27,7 +27,7 @@ - +