Skip to content

Commit

Permalink
Merge pull request #33 from trenz-gmbh/reindex-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss authored Dec 5, 2023
2 parents 88b6e72 + 7f21e2f commit 4a94cdc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
28 changes: 26 additions & 2 deletions TRENZ.Docs.API/Controllers/SearchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ namespace TRENZ.Docs.API.Controllers;
[Route("api/[controller]/[action]")]
public class SearchController : ControllerBase
{
private static DateTime? _lastReindex;

private readonly IIndexingService _indexingService;
private readonly IConfiguration _configuration;
private readonly IndexWorker _indexWorker;

public SearchController(IIndexingService indexingService)
public SearchController(IIndexingService indexingService, IConfiguration configuration, IndexWorker indexWorker)
{
_indexingService = indexingService;
_configuration = configuration;
_indexWorker = indexWorker;
}

[HttpGet]
Expand All @@ -26,4 +32,22 @@ public async Task<IndexStats> Stats()
{
return await _indexingService.GetStats();
}
}

[HttpGet]
public async Task<IActionResult> Reindex([FromQuery] string? key, CancellationToken cancellationToken = default)
{
if (_configuration["ReindexPassword"] != key)
return Unauthorized();

#if !DEBUG
if (DateTime.Now - _lastReindex < TimeSpan.FromSeconds(_configuration.GetValue<int>("ReindexThrottling")))
return new StatusCodeResult(429);

_lastReindex = DateTime.Now;
#endif

await _indexWorker.DoReindex(cancellationToken);

return Ok();
}
}
47 changes: 28 additions & 19 deletions TRENZ.Docs.API/Worker.cs → TRENZ.Docs.API/IndexWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace TRENZ.Docs.API
{
public class Worker : BackgroundService
public class IndexWorker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly ILogger<IndexWorker> _logger;
private readonly IConfiguration _configuration;
private readonly IHostApplicationLifetime _lifetime;
private readonly IIndexingService _indexingService;
private readonly ISourcesProvider _sourcesProvider;
private readonly IFileProcessingService _fileProcessingService;
private readonly INavTreeProvider _navTreeProvider;

public Worker(ILogger<Worker> logger, IConfiguration configuration, IHostApplicationLifetime lifetime, IIndexingService indexingService, ISourcesProvider sourcesProvider, IFileProcessingService fileProcessingService, INavTreeProvider navTreeProvider)
public IndexWorker(ILogger<IndexWorker> logger, IConfiguration configuration, IHostApplicationLifetime lifetime, IIndexingService indexingService, ISourcesProvider sourcesProvider, IFileProcessingService fileProcessingService, INavTreeProvider navTreeProvider)
{
_logger = logger;
_configuration = configuration;
Expand All @@ -29,23 +29,9 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Running at {Time}", DateTimeOffset.Now);
_logger.LogInformation("Worker running at {Time}", DateTimeOffset.Now);

var sourceFiles = await GatherFiles(stoppingToken).ToListAsync(stoppingToken);
var tree = await _navTreeProvider.RebuildAsync(sourceFiles, stoppingToken);

var indexTree = tree.WithoutHiddenNodes().WithoutChildlessContentlessNodes();
var markdownFiles = sourceFiles
.Where(file => file.RelativePath.EndsWith(".md"))
.Select(file => (file, node: indexTree.FindNodeByLocation(file.Location)))
.Where(tup => tup.node is not null)
.Select(tup => tup.file);

var indexFiles = await _fileProcessingService
.ProcessAsync(markdownFiles, stoppingToken)
.ToListAsync(stoppingToken);

await _indexingService.IndexAsync(indexFiles, stoppingToken);
await DoReindex(stoppingToken);

if (_configuration["TrenzDocsApi:OneShot"] == "true")
{
Expand All @@ -64,6 +50,29 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
_lifetime.StopApplication();
}

public async Task DoReindex(CancellationToken cancellationToken)
{
_logger.LogInformation("Reindexing...");

var sourceFiles = await GatherFiles(cancellationToken).ToListAsync(cancellationToken);
var tree = await _navTreeProvider.RebuildAsync(sourceFiles, cancellationToken);

var indexTree = tree.WithoutHiddenNodes().WithoutChildlessContentlessNodes();
var markdownFiles = sourceFiles
.Where(file => file.RelativePath.EndsWith(".md"))
.Select(file => (file, node: indexTree.FindNodeByLocation(file.Location)))
.Where(tup => tup.node is not null)
.Select(tup => tup.file);

var indexFiles = await _fileProcessingService
.ProcessAsync(markdownFiles, cancellationToken)
.ToListAsync(cancellationToken);

await _indexingService.IndexAsync(indexFiles, cancellationToken);

_logger.LogInformation("Reindexing done");
}

private async IAsyncEnumerable<ISourceFile> GatherFiles([EnumeratorCancellation] CancellationToken cancellationToken)
{
_logger.LogInformation("Gathering files...");
Expand Down
3 changes: 2 additions & 1 deletion TRENZ.Docs.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
builder.Services.AddAuthAdapter();
}

builder.Services.AddHostedService<Worker>();
builder.Services.AddSingleton<IndexWorker>();
builder.Services.AddHostedService<IndexWorker>(sp => sp.GetRequiredService<IndexWorker>());

builder.Services.AddControllers();

Expand Down
3 changes: 2 additions & 1 deletion TRENZ.Docs.API/appsettings.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"Endpoint": "https://my-secure-provider.com/SignIn",
"ClientId": "my-client-id",
"ClientSecret": "my-client-secret"
}
},
"ReindexPassword": "$ome.$3cur3_Key"
}
3 changes: 2 additions & 1 deletion TRENZ.Docs.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ReindexThrottling": 600
}

0 comments on commit 4a94cdc

Please sign in to comment.