Guard notifications job when table missing

This commit is contained in:
Eduardo Lopes 2026-01-22 15:46:19 -03:00
parent eae766dc38
commit 0cbd4fdd67
1 changed files with 30 additions and 0 deletions

View File

@ -42,6 +42,12 @@ public class VigenciaNotificationBackgroundService : BackgroundService
using var scope = _scopeFactory.CreateScope(); using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
if (!await TableExistsAsync(db, "Notifications", stoppingToken))
{
_logger.LogWarning("Tabela Notifications ainda não existe. Aguardando migrations.");
return;
}
var today = DateTime.UtcNow.Date; var today = DateTime.UtcNow.Date;
var reminderDays = _options.ReminderDays var reminderDays = _options.ReminderDays
.Distinct() .Distinct()
@ -159,6 +165,30 @@ public class VigenciaNotificationBackgroundService : BackgroundService
} }
} }
private static async Task<bool> TableExistsAsync(AppDbContext db, string tableName, CancellationToken stoppingToken)
{
if (!db.Database.IsRelational())
{
return true;
}
var connection = db.Database.GetDbConnection();
if (connection.State != System.Data.ConnectionState.Open)
{
await connection.OpenAsync(stoppingToken);
}
await using var command = connection.CreateCommand();
command.CommandText = "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = @tableName)";
var parameter = command.CreateParameter();
parameter.ParameterName = "tableName";
parameter.Value = tableName;
command.Parameters.Add(parameter);
var result = await command.ExecuteScalarAsync(stoppingToken);
return result is bool exists && exists;
}
private static Notification BuildNotification( private static Notification BuildNotification(
string tipo, string tipo,
string titulo, string titulo,