This repository has been archived by the owner on Sep 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from NuGet/anurse/1940-searchjobs
Fix NuGet/NuGetGallery#1940 by adding search maintenance tasks
- Loading branch information
Showing
12 changed files
with
402 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/NuGet.Services.Work/Jobs/Bases/SearchIndexJobHandlerBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.SqlClient; | ||
using System.Diagnostics.Tracing; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.WindowsAzure.Storage; | ||
using NuGet.Indexing; | ||
using NuGet.Services.Configuration; | ||
|
||
namespace NuGet.Services.Work.Jobs.Bases | ||
{ | ||
public abstract class SearchIndexJobHandlerBase<T> : JobHandler<T> | ||
where T : EventSource | ||
{ | ||
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; } | ||
|
||
public SearchIndexJobHandlerBase(ConfigurationHub config) | ||
{ | ||
Config = config; | ||
} | ||
|
||
protected override InvocationResult BindContext(InvocationContext context) | ||
{ | ||
var result = base.BindContext(context); | ||
|
||
// Load default values | ||
PackageDatabase = PackageDatabase ?? Config.Sql.Legacy; | ||
StorageAccount = StorageAccount ?? Config.Storage.Primary; | ||
StorageContainerName = StorageContainerName ?? "ng-search"; | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/NuGet.Services.Work/Jobs/Models/SearchRankingReport.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.SqlClient; | ||
using System.Diagnostics.Tracing; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.WindowsAzure.Storage; | ||
using Microsoft.WindowsAzure.Storage.Blob; | ||
using NuGet.Indexing; | ||
using NuGet.Services.Configuration; | ||
using NuGet.Services.Work.Jobs.Bases; | ||
using NuGet.Services.Work.Monitoring; | ||
|
||
namespace NuGet.Services.Work.Jobs | ||
{ | ||
public class RebuildSearchIndexJob : SearchIndexJobHandlerBase<RebuildSearchIndexEventSource> | ||
{ | ||
public RebuildSearchIndexJob(ConfigurationHub config) : base(config) { } | ||
|
||
protected internal override async Task Execute() | ||
{ | ||
// This job can take a long time and is run manually. Make the job timeout long | ||
await Extend(TimeSpan.FromHours(12)); | ||
|
||
// Run the task | ||
Log.BeginningIndex( | ||
PackageDatabase.DataSource + "/" + PackageDatabase.InitialCatalog, | ||
String.IsNullOrEmpty(LocalIndexFolder) ? | ||
(StorageAccount.Credentials.AccountName + "/" + StorageContainerName) : | ||
LocalIndexFolder); | ||
FullBuildTask task = new FullBuildTask() | ||
{ | ||
SqlConnectionString = PackageDatabase.ConnectionString, | ||
StorageAccount = StorageAccount, | ||
Container = String.IsNullOrEmpty(LocalIndexFolder) ? | ||
StorageContainerName : | ||
null, | ||
Folder = LocalIndexFolder, | ||
Log = new EventSourceWriter(Log.IndexingTrace) | ||
}; | ||
task.Execute(); | ||
Log.FinishedIndex(); | ||
} | ||
} | ||
|
||
[EventSource(Name="Outercurve-NuGet-Jobs-RebuildSearchIndex")] | ||
public class RebuildSearchIndexEventSource : EventSource | ||
{ | ||
public static readonly RebuildSearchIndexEventSource Log = new RebuildSearchIndexEventSource(); | ||
private RebuildSearchIndexEventSource() { } | ||
|
||
[Event( | ||
eventId: 1, | ||
Level = EventLevel.Informational, | ||
Message = "Indexing Trace: {0}")] | ||
public void IndexingTrace(string message) { WriteEvent(1, message); } | ||
|
||
[Event( | ||
eventId: 2, | ||
Level = EventLevel.Informational, | ||
Message = "Beginning Index Rebuild from {0} to {1}", | ||
Task = Tasks.Indexing, | ||
Opcode = EventOpcode.Start)] | ||
public void BeginningIndex(string source, string destination) { WriteEvent(2, source, destination); } | ||
|
||
[Event( | ||
eventId: 3, | ||
Level = EventLevel.Informational, | ||
Message = "Finished Index Rebuild", | ||
Task = Tasks.Indexing, | ||
Opcode = EventOpcode.Stop)] | ||
public void FinishedIndex() { WriteEvent(3); } | ||
|
||
public static class Tasks | ||
{ | ||
public const EventTask Indexing = (EventTask)0x1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Tracing; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NuGet.Indexing; | ||
using NuGet.Services.Configuration; | ||
using NuGet.Services.Work.Jobs.Bases; | ||
using NuGet.Services.Work.Monitoring; | ||
|
||
namespace NuGet.Services.Work.Jobs | ||
{ | ||
public class UpdateSearchIndexJob : SearchIndexJobHandlerBase<UpdateSearchIndexEventSource> | ||
{ | ||
public UpdateSearchIndexJob(ConfigurationHub config) : base(config) { } | ||
|
||
protected internal override Task Execute() | ||
{ | ||
// Run the task | ||
UpdateIndexTask task = new UpdateIndexTask() | ||
{ | ||
SqlConnectionString = PackageDatabase.ConnectionString, | ||
StorageAccount = StorageAccount, | ||
Container = String.IsNullOrEmpty(LocalIndexFolder) ? | ||
(StorageContainerName ?? "ng-search") : | ||
null, | ||
Folder = LocalIndexFolder, | ||
Log = new EventSourceWriter(Log.IndexingTrace), | ||
}; | ||
task.Execute(); | ||
|
||
return Task.FromResult(0); | ||
} | ||
} | ||
|
||
[EventSource(Name="Outercurve-NuGet-Jobs-UpdateSearchIndex")] | ||
public class UpdateSearchIndexEventSource : EventSource | ||
{ | ||
public static readonly UpdateSearchIndexEventSource Log = new UpdateSearchIndexEventSource(); | ||
private UpdateSearchIndexEventSource() { } | ||
|
||
[Event( | ||
eventId: 1, | ||
Level = EventLevel.Informational, | ||
Message = "Indexing Trace: {0}")] | ||
public void IndexingTrace(string message) { WriteEvent(1, message); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NuGet.Services.Work.Monitoring | ||
{ | ||
/// <summary> | ||
/// Writes lines to a specified EventSource method | ||
/// </summary> | ||
public class EventSourceWriter : TextWriter | ||
{ | ||
private Action<string> _receiver; | ||
private StringBuilder _buffer = new StringBuilder(); | ||
|
||
public override Encoding Encoding | ||
{ | ||
get { return Encoding.Default; } | ||
} | ||
|
||
public EventSourceWriter(Action<string> receiver) | ||
{ | ||
_receiver = receiver; | ||
} | ||
|
||
public override void Write(char value) | ||
{ | ||
_buffer.Append(value); | ||
CheckFlushLine(); | ||
} | ||
|
||
public override void Flush() | ||
{ | ||
_receiver(_buffer.ToString().Trim()); | ||
_buffer.Clear(); | ||
} | ||
|
||
private void CheckFlushLine() | ||
{ | ||
if (_buffer.ToString().EndsWith(NewLine)) | ||
{ | ||
// Flush the buffer | ||
Flush(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.