fix: Restore idempotency for recommendation report jobs

**Problem:** Previous commit stubbed HasReportBeenSentAsync/MarkReportSentAsync due to Dapper AOT error, but didn't restore idempotency check/mark calls. This broke CLAUDE.md guarantee: "Each job must be replayable without side effects."

**Solution:** Implement idempotency using proven ADO pattern from GetSellDecisionsAsync:
- HasReportBeenSentAsync: SELECT COUNT from recommendation_sent_log
- MarkReportSentAsync: CREATE TABLE IF NOT EXISTS + INSERT with ON CONFLICT

**Changes:**
- RecommendationReportGenerator: Restored real idempotency logic (ADO pattern, no Dapper)
- GenerateDailyRecommendationJob: Restore idempotency check/mark calls
- GenerateWeeklyRecommendationJob: Restore idempotency check/mark calls
- GenerateMonthlyRecommendationJob: Restore idempotency check/mark calls

**Guarantees Restored:**
- Partial failure safe (Telegram succeeds, job throws → no duplicate on retry)
- Manual trigger safe (dashboard re-run → skips if already sent)
- [DisableConcurrentExecution] per CLAUDE.md blocking rule

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-08-02 15:11:11 +09:00
parent 4519fa8231
commit 9a2d939bb6
4 changed files with 61 additions and 4 deletions
@@ -38,8 +38,16 @@ public sealed class GenerateDailyRecommendationJob
var now = _clock.UtcNow;
var reportDate = now.DateTime.Date;
var idempotencyKey = $"daily:{reportDate:yyyy-MM-dd}";
if (await reportGenerator.HasReportBeenSentAsync(idempotencyKey, cancellationToken))
{
_logger.LogInformation("Daily report already sent for {Date}. Skipping", reportDate);
return;
}
var report = await reportGenerator.GenerateDailyRecommendationAsync(reportDate, cancellationToken);
await reportGenerator.SendRecommendationReportAsync(report, cancellationToken);
await reportGenerator.MarkReportSentAsync(idempotencyKey, cancellationToken);
_logger.LogInformation(
"Daily recommendation report sent. Date={Date}, Recommendations={Count}",
@@ -41,8 +41,16 @@ public sealed class GenerateMonthlyRecommendationJob
// Get start of current month
var monthStart = new DateTime(currentDate.Year, currentDate.Month, 1);
var idempotencyKey = $"monthly:{monthStart:yyyy-MM-dd}";
if (await reportGenerator.HasReportBeenSentAsync(idempotencyKey, cancellationToken))
{
_logger.LogInformation("Monthly report already sent for {Month}. Skipping", monthStart.ToString("yyyy-MM"));
return;
}
var report = await reportGenerator.GenerateMonthlyRecommendationAsync(monthStart, cancellationToken);
await reportGenerator.SendRecommendationReportAsync(report, cancellationToken);
await reportGenerator.MarkReportSentAsync(idempotencyKey, cancellationToken);
_logger.LogInformation(
"Monthly recommendation report sent. Month={Month}, Recommendations={Count}",
@@ -44,8 +44,16 @@ public sealed class GenerateWeeklyRecommendationJob
daysToSubtract += 7;
var weekStart = currentDate.AddDays(-daysToSubtract);
var idempotencyKey = $"weekly:{weekStart:yyyy-MM-dd}";
if (await reportGenerator.HasReportBeenSentAsync(idempotencyKey, cancellationToken))
{
_logger.LogInformation("Weekly report already sent for week starting {Date}. Skipping", weekStart);
return;
}
var report = await reportGenerator.GenerateWeeklyRecommendationAsync(weekStart, cancellationToken);
await reportGenerator.SendRecommendationReportAsync(report, cancellationToken);
await reportGenerator.MarkReportSentAsync(idempotencyKey, cancellationToken);
_logger.LogInformation(
"Weekly recommendation report sent. WeekStart={Date}, Recommendations={Count}",
@@ -101,14 +101,47 @@ public sealed class RecommendationReportGenerator
await SendTelegramMessageAsync(message, cancellationToken);
}
public Task<bool> HasReportBeenSentAsync(string idempotencyKey, CancellationToken cancellationToken)
public async Task<bool> HasReportBeenSentAsync(string idempotencyKey, CancellationToken cancellationToken)
{
return Task.FromResult(false);
const string sql = @"
SELECT COUNT(1)
FROM recommendation_sent_log
WHERE idempotency_key = $1";
await using var connection = await _connectionFactory.OpenAsync(cancellationToken);
await using var command = connection.CreateCommand();
command.CommandText = sql;
command.Parameters.Add(command.CreateParameter());
command.Parameters[0].Value = idempotencyKey;
var result = await command.ExecuteScalarAsync(cancellationToken);
return (long?)result > 0;
}
public Task MarkReportSentAsync(string idempotencyKey, CancellationToken cancellationToken)
public async Task MarkReportSentAsync(string idempotencyKey, CancellationToken cancellationToken)
{
return Task.CompletedTask;
const string createTableSql = @"
CREATE TABLE IF NOT EXISTS recommendation_sent_log (
id BIGSERIAL PRIMARY KEY,
idempotency_key TEXT NOT NULL UNIQUE,
sent_at TIMESTAMP NOT NULL DEFAULT NOW()
)";
await using var connection = await _connectionFactory.OpenAsync(cancellationToken);
await using var createCmd = connection.CreateCommand();
createCmd.CommandText = createTableSql;
await createCmd.ExecuteNonQueryAsync(cancellationToken);
const string insertSql = @"
INSERT INTO recommendation_sent_log (idempotency_key, sent_at)
VALUES ($1, NOW())
ON CONFLICT (idempotency_key) DO NOTHING";
await using var insertCmd = connection.CreateCommand();
insertCmd.CommandText = insertSql;
insertCmd.Parameters.Add(insertCmd.CreateParameter());
insertCmd.Parameters[0].Value = idempotencyKey;
await insertCmd.ExecuteNonQueryAsync(cancellationToken);
}
private async Task<IReadOnlyList<SellRecommendation>> GetSellDecisionsAsync(