YaeBlog/YaeBlog/Services/BlogHotReloadService.cs
jackfiled 3aae468e65
All checks were successful
Build blog docker image / Build-Blog-Image (push) Successful in 1m15s
feat: 从Bootstrap迁移到Tailwind css (#9)
Reviewed-on: #9
2025-01-24 16:46:56 +08:00

42 lines
1.3 KiB
C#

using YaeBlog.Abstraction;
namespace YaeBlog.Services;
public sealed class BlogHotReloadService(
RendererService rendererService,
IEssayContentService essayContentService,
BlogChangeWatcher watcher,
ILogger<BlogHotReloadService> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("Hot reload is starting...");
logger.LogInformation("Change essays will lead to hot reload!");
logger.LogInformation("HINT: draft essays will be included.");
await rendererService.RenderAsync(true);
Task[] reloadTasks = [FileWatchTask(stoppingToken)];
await Task.WhenAll(reloadTasks);
}
private async Task FileWatchTask(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
logger.LogInformation("Watching file changes...");
string? changeFile = await watcher.WaitForChange(token);
if (changeFile is null)
{
logger.LogInformation("File watcher is stopping.");
break;
}
logger.LogInformation("{} changed, re-rendering.", changeFile);
essayContentService.Clear();
await rendererService.RenderAsync(true);
}
}
}