작업 시간 업데이트 기능 추가 및 이슈 데이터 저장 확인.

This commit is contained in:
2025-12-11 17:54:18 +09:00
parent 632f834ac5
commit cf0b6907f7
5 changed files with 52 additions and 19 deletions

View File

@@ -11,13 +11,16 @@ public class JiraWorker : BackgroundService
{
private readonly Serilog.ILogger _log;
private readonly AppConfigs _configs;
private readonly IServiceScopeFactory _scopeFactory; // Singleton services cannot directly inject Scoped services.
private readonly JiraService _jira;
private readonly Channel<Issue> _issueChannel;
public JiraWorker(AppConfigs configs, JiraService jaraService, Channel<Issue> issueChannel)
public JiraWorker(AppConfigs configs, IServiceScopeFactory serviceScopeFactory,
JiraService jaraService, Channel<Issue> issueChannel)
{
_log = Log.ForContext<JiraWorker>(); ;
_configs = configs;
_scopeFactory = serviceScopeFactory;
_jira = jaraService;
_issueChannel = issueChannel;
}
@@ -28,15 +31,27 @@ public class JiraWorker : BackgroundService
{
_log.Debug("Worker running at: {time}", DateTimeOffset.Now);
var lastFetchTime = DateTimeOffset.Parse(_configs.RuntimeConfigs[Consts.ConfigLastFetchTimeKey]);
var configLastFetchTime = _configs.RuntimeConfigs.GetValueOrDefault(Consts.ConfigLastFetchTimeKey);
if (string.IsNullOrEmpty(configLastFetchTime))
{
using (var scope = _scopeFactory.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbService>();
var lastFetchTimeText = await db.GetConfigAsync(Consts.ConfigLastFetchTimeKey);
configLastFetchTime = (lastFetchTimeText != null)
? lastFetchTimeText : DateTimeOffset.Now.AddDays(-1).ToString("yyyy-MM-ddTHH:mm:sszzz");
}
}
var lastFetchTime = DateTimeOffset.Parse(configLastFetchTime);
var issueList = new List<Issue>();
var currentFetchTime = DateTimeOffset.Now;
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);
var response = await _jira.FetchJiraIssuesAsync(lastFetchTime, targetProject, startAt, stoppingToken);
if (response != null)
{
total = response.total;
@@ -49,6 +64,14 @@ public class JiraWorker : BackgroundService
{
await _issueChannel.Writer.WriteAsync(item, stoppingToken);
}
using (var scope = _scopeFactory.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbService>();
// Update last fetch time
await db.SetConfigAsync(Consts.ConfigLastFetchTimeKey, currentFetchTime.ToString("yyyy-MM-ddTHH:mm:sszzz"));
_configs.RuntimeConfigs[Consts.ConfigLastFetchTimeKey] = currentFetchTime.ToString("yyyy-MM-ddTHH:mm:sszzz");
}
await Task.Delay(_configs.AppSettings.Jira.FetchInterval * 1000, stoppingToken);
}
}