diff --git a/Services/AppDbService.cs b/Services/AppDbService.cs index 48f73c1..2bf008e 100644 --- a/Services/AppDbService.cs +++ b/Services/AppDbService.cs @@ -203,5 +203,23 @@ public class AppDbService throw; } } + + public async Task> GetUnpublishedJiraIssuesAsync(CancellationToken ct = default) + { + try + { + var unpublishedIssues = await _db.JiraIssues + .Where(ji => ji.Published < ji.Updated) + .ToListAsync(ct); + + _log.Information("Found {count} unpublished Jira issues", unpublishedIssues.Count); + return unpublishedIssues; + } + catch (Exception ex) + { + _log.Error(ex, "Failed to get unpublished Jira issues"); + throw; + } + } #endregion Table: JiraIssues } diff --git a/Workers/TriliumWorker.cs b/Workers/TriliumWorker.cs index b681778..e61c6a1 100644 --- a/Workers/TriliumWorker.cs +++ b/Workers/TriliumWorker.cs @@ -11,13 +11,16 @@ public class TriliumWorker : BackgroundService { private readonly Serilog.ILogger _log; private readonly AppConfigs _config; + private readonly IServiceScopeFactory _scopeFactory; // Singleton services cannot directly inject Scoped services. private readonly TriliumService _triliumService; private readonly Channel _issueChannel; - public TriliumWorker(AppConfigs configs, TriliumService triliumService, Channel issueChannel) + public TriliumWorker(AppConfigs configs, IServiceScopeFactory serviceScopeFactory, + TriliumService triliumService, Channel issueChannel) { _log = Log.ForContext(); _config = configs; + _scopeFactory = serviceScopeFactory; _triliumService = triliumService; _issueChannel = issueChannel; } @@ -27,6 +30,15 @@ public class TriliumWorker : BackgroundService while (!stoppingToken.IsCancellationRequested) { _log.Debug("Worker running at: {time}", DateTimeOffset.Now); + + using var scope = _scopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var unpublishedIssues = await db.GetUnpublishedJiraIssuesAsync(stoppingToken); + if (unpublishedIssues != null) + { + // Publish or update Trilium notes + } + await Task.Delay(10 *1000, stoppingToken); } }