Skip to content

Commit

Permalink
Fix healthcheck with lazy compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Mar 23, 2022
1 parent a37ac35 commit 169b752
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Expand Up @@ -60,6 +60,7 @@ public DefaultControlBuilderFactory(DotvvmConfiguration configuration, IMarkupFi
/// </summary>
private (ControlBuilderDescriptor, Lazy<IControlBuilder>) CreateControlBuilder(MarkupFile file)
{
var compilationService = configuration.ServiceProvider.GetService<IDotvvmViewCompilationService>();
void editCompilationException(DotvvmCompilationException ex)
{
if (ex.FileName == null)
Expand Down Expand Up @@ -90,11 +91,18 @@ void editCompilationException(DotvvmCompilationException ex)
configuration.Resources.RegisterViewModuleResources(import, init);
}
compilationService.RegisterCompiledView(file.FileName, descriptor, null);
return result;
}
catch (DotvvmCompilationException ex)
{
editCompilationException(ex);
compilationService.RegisterCompiledView(file.FileName, descriptor, ex);
throw;
}
catch (Exception ex)
{
compilationService.RegisterCompiledView(file.FileName, descriptor, ex);
throw;
}
});
Expand All @@ -111,6 +119,12 @@ void editCompilationException(DotvvmCompilationException ex)
catch (DotvvmCompilationException ex)
{
editCompilationException(ex);
compilationService.RegisterCompiledView(file.FileName, null, ex);
throw;
}
catch (Exception ex)
{
compilationService.RegisterCompiledView(file.FileName, null, ex);
throw;
}
}
Expand Down
Expand Up @@ -9,6 +9,7 @@
using DotVVM.Framework.Configuration;
using DotVVM.Framework.Controls.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using DotVVM.Framework.Utils;

namespace DotVVM.Framework.Compilation
{
Expand Down Expand Up @@ -155,10 +156,9 @@ public bool BuildView(DotHtmlFileInfo file, out DotHtmlFileInfo? masterPage)
using var scopedServiceProvider = dotvvmConfiguration.ServiceProvider.CreateScope(); // dependencies that are configured as scoped cannot be resolved from root service provider
var compiledControl = pageBuilder.builder.Value.BuildControl(controlBuilderFactory, scopedServiceProvider.ServiceProvider);

if (compiledControl is DotvvmView view &&
view.Directives!.TryGetValue(ParserConstants.MasterPageDirective, out var masterPagePath))
if (pageBuilder.descriptor.MasterPage is { FileName: {} masterPagePath })
{
masterPage = masterPages.Value.GetOrAdd(masterPagePath, new DotHtmlFileInfo(masterPagePath));
masterPage = masterPages.Value.GetOrAdd(masterPagePath, path => new DotHtmlFileInfo(path));
}

file.Status = CompilationState.CompletedSuccessfully;
Expand All @@ -173,5 +173,30 @@ public bool BuildView(DotHtmlFileInfo file, out DotHtmlFileInfo? masterPage)
}
return true;
}

/// <summary> Callback from the compiler which adds the view compilation result to the status page. </summary>
public void RegisterCompiledView(string file, ViewCompiler.ControlBuilderDescriptor? descriptor, Exception? exception)
{
var fileInfo =
routes.Value.FirstOrDefault(t => t.VirtualPath == file) ??
controls.Value.FirstOrDefault(t => t.VirtualPath == file) ??
masterPages.Value.GetOrAdd(file, path => new DotHtmlFileInfo(path));

if (exception is null)
{
fileInfo.Status = CompilationState.CompletedSuccessfully;
fileInfo.Exception = null;
}
else
{
fileInfo.Status = CompilationState.CompilationFailed;
fileInfo.Exception = exception.Message;
}

if (descriptor?.MasterPage is { FileName: {} masterPagePath })
{
masterPages.Value.GetOrAdd(masterPagePath, path => new DotHtmlFileInfo(path));
}
}
}
}
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
Expand Down Expand Up @@ -43,5 +44,7 @@ public interface IDotvvmViewCompilationService
/// <param name="forceRecompile">If set, than everything will be recompiled.</param>
/// <returns>Returns whether compilation was successful.</returns>
Task<bool> CompileAll(bool buildInParallel=true, bool forceRecompile = false);

void RegisterCompiledView(string filePath, ViewCompiler.ControlBuilderDescriptor? descriptor, Exception? exception);
}
}

0 comments on commit 169b752

Please sign in to comment.