56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
using Microsoft.Extensions.Options;
|
|
using Serilog;
|
|
using System.Numerics;
|
|
using System.Threading.Channels;
|
|
using TriliumMind.Models;
|
|
using TriliumMind.Services;
|
|
|
|
namespace TriliumMind.Workers;
|
|
|
|
public class JiraWorker : BackgroundService
|
|
{
|
|
private readonly Serilog.ILogger _log;
|
|
private readonly AppConfigs _configs;
|
|
private readonly JiraService _jira;
|
|
private readonly Channel<Issue> _issueChannel;
|
|
|
|
public JiraWorker(AppConfigs configs, JiraService jaraService, Channel<Issue> issueChannel)
|
|
{
|
|
_log = Log.ForContext<JiraWorker>(); ;
|
|
_configs = configs;
|
|
_jira = jaraService;
|
|
_issueChannel = issueChannel;
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
_log.Debug("Worker running at: {time}", DateTimeOffset.Now);
|
|
|
|
var lastFetchTime = DateTimeOffset.Parse(_configs.RuntimeConfigs[Consts.ConfigLastFetchTimeKey]);
|
|
|
|
var issueList = new List<Issue>();
|
|
foreach (var targetProject in _configs.AppSettings.Jira.TargetProjects)
|
|
{
|
|
int startAt = 0, total = 0;
|
|
do
|
|
{
|
|
var response = await _jira.FetchJiraIssuesAsync(DateTimeOffset.Now.AddDays(-1), targetProject, startAt, stoppingToken);
|
|
if (response != null)
|
|
{
|
|
total = response.total;
|
|
startAt += response.maxResults;
|
|
issueList.AddRange(response.issues);
|
|
}
|
|
} while(startAt < total);
|
|
}
|
|
foreach (var item in issueList)
|
|
{
|
|
await _issueChannel.Writer.WriteAsync(item, stoppingToken);
|
|
}
|
|
await Task.Delay(_configs.AppSettings.Jira.FetchInterval * 1000, stoppingToken);
|
|
}
|
|
}
|
|
}
|