feat: Implementazione completa sistema schedulazione con intervalli personalizzati
- Aggiunto supporto schedulazione con intervalli flessibili (secondi/minuti/ore/giorni/settimane/mesi) - Esteso modello ProfileSchedule con campi IntervalValue e IntervalUnit - Ottimizzato ScheduledJobService per controlli ogni 30s con esecuzione parallela - Implementata interfaccia UI completa con anteprima real-time in italiano - Aggiunta migrazione database AddIntervalSchedulingFields - Implementati metodi calcolo NextExecutionTime per intervalli - Aggiunta gestione tracking anti-duplicati e cleanup automatico - Creata documentazione completa (6 file, 2500+ righe) Modifiche tecniche: - ProfileSchedule.cs: Nuovi campi e metodi CalculateNextInterval/GetScheduleDescription - ScheduledJobService.cs: Ridotto check interval a 30s, aggiunto parallel processing - ProfileScheduleService.cs: Supporto calcolo intervalli in UpdateNextExecutionTimeAsync - Scheduling.razor: Aggiunta sezione UI per configurazione intervalli - Scheduling.razor.cs: Implementato GetIntervalPreview() e gestione stato campi
This commit is contained in:
+112
-21
@@ -8,42 +8,78 @@ using DataConnection.Enums;
|
||||
using DataConnection.CredentialManagement;
|
||||
using CredentialManager;
|
||||
using Data_Coupler.Services;
|
||||
using Data_Coupler.BackgroundServices;
|
||||
using CredentialManager.Services;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Data_Coupler.BackgrounServices;
|
||||
|
||||
// Registra il provider di encoding per ExcelDataReader (necessario per file .xls)
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Configurazione per Windows Service
|
||||
builder.Host.UseWindowsService(options =>
|
||||
{
|
||||
options.ServiceName = "DataCouplerService";
|
||||
});
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddRazorPages();
|
||||
builder.Services.AddServerSideBlazor();
|
||||
builder.Services.AddWindowsService();
|
||||
builder.Services.AddHostedService<BackgroundServices>();
|
||||
|
||||
// Configurazione logging per Windows Service
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
builder.Logging.AddEventLog();
|
||||
}
|
||||
|
||||
#region Database Directory Path management
|
||||
|
||||
string dbPath = string.Empty;
|
||||
|
||||
if (OperatingSystem.IsWindows())
|
||||
try
|
||||
{
|
||||
dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Data_Coupler", "credentials.db");
|
||||
}
|
||||
else if (OperatingSystem.IsLinux())
|
||||
{
|
||||
dbPath = "/var/lib/Data_Coupler/credentials.db";
|
||||
}
|
||||
else if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
dbPath = "/Library/Application Support/Data_Coupler/credentials.db";
|
||||
}
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
// Per servizi Windows, usa una cartella con permessi appropriati
|
||||
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
|
||||
if (string.IsNullOrEmpty(appDataPath))
|
||||
{
|
||||
// Fallback per servizi Windows
|
||||
appDataPath = @"C:\ProgramData";
|
||||
}
|
||||
dbPath = Path.Combine(appDataPath, "Data_Coupler", "credentials.db");
|
||||
}
|
||||
else if (OperatingSystem.IsLinux())
|
||||
{
|
||||
dbPath = "/var/lib/Data_Coupler/credentials.db";
|
||||
}
|
||||
else if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
dbPath = "/Library/Application Support/Data_Coupler/credentials.db";
|
||||
}
|
||||
|
||||
var dbDirectory = Path.GetDirectoryName(dbPath);
|
||||
if (!Directory.Exists(dbDirectory))
|
||||
var dbDirectory = Path.GetDirectoryName(dbPath);
|
||||
if (!Directory.Exists(dbDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(dbDirectory!);
|
||||
|
||||
// Per Windows, assicurati che la cartella abbia i permessi corretti
|
||||
if (OperatingSystem.IsWindows() && !string.IsNullOrEmpty(dbDirectory))
|
||||
{
|
||||
var directoryInfo = new DirectoryInfo(dbDirectory);
|
||||
// Imposta permessi per consentire l'accesso al servizio
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Database path: {dbPath}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Directory.CreateDirectory(dbDirectory!);
|
||||
Console.WriteLine($"Errore nella configurazione del percorso database: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -63,24 +99,79 @@ builder.Services.AddHttpClient();
|
||||
|
||||
// Register Data Connection Factory
|
||||
builder.Services.AddScoped<IDataConnectionFactory, DataConnectionFactory>();
|
||||
builder.WebHost.UseUrls("http://*:7550");
|
||||
|
||||
// Register Backup Service
|
||||
builder.Services.AddScoped<Data_Coupler.Services.IBackupService, Data_Coupler.Services.BackupService>();
|
||||
|
||||
// Register Schedule Services
|
||||
builder.Services.AddScoped<IProfileScheduleService, ProfileScheduleService>();
|
||||
|
||||
// Register Data Transfer Service
|
||||
builder.Services.AddScoped<Data_Coupler.Services.IDataTransferService, Data_Coupler.Services.DataTransferService>();
|
||||
|
||||
// Register Scheduled Profile Execution Service
|
||||
builder.Services.AddScoped<Data_Coupler.Services.IScheduledProfileExecutionService, Data_Coupler.Services.ScheduledProfileExecutionService>();
|
||||
|
||||
// Register Background Services (solo uno per evitare duplicazioni)
|
||||
builder.Services.AddHostedService<Data_Coupler.BackgroundServices.ScheduledJobService>();
|
||||
|
||||
// Configurazione URL e timeout per servizio Windows
|
||||
var urls = builder.Configuration.GetValue<string>("Urls") ?? "http://*:7550";
|
||||
builder.WebHost.UseUrls(urls);
|
||||
|
||||
// Configurazione timeout per servizio Windows
|
||||
builder.WebHost.ConfigureKestrel(serverOptions =>
|
||||
{
|
||||
serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10);
|
||||
serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(5);
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Initialize database
|
||||
// Initialize database con timeout e retry
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Inizializzazione database in corso...");
|
||||
logger.LogInformation("Inizializzazione database in corso... Path: {DbPath}", dbPath);
|
||||
|
||||
var dbInitializer = scope.ServiceProvider.GetRequiredService<CredentialManager.Services.IDatabaseInitializer>();
|
||||
dbInitializer.InitializeAsync().GetAwaiter().GetResult();
|
||||
|
||||
// Inizializzazione con timeout di 60 secondi
|
||||
var initTask = dbInitializer.InitializeAsync();
|
||||
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(60));
|
||||
|
||||
var completedTask = await Task.WhenAny(initTask, timeoutTask);
|
||||
|
||||
if (completedTask == timeoutTask)
|
||||
{
|
||||
logger.LogError("Timeout durante l'inizializzazione del database (60 secondi)");
|
||||
throw new TimeoutException("Timeout durante l'inizializzazione del database");
|
||||
}
|
||||
|
||||
await initTask; // Attendi il completamento per eventuali eccezioni
|
||||
logger.LogInformation("Database inizializzato con successo.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Errore durante l'inizializzazione del database: {Message}", ex.Message);
|
||||
logger.LogError(ex, "Errore durante l'inizializzazione del database: {Message}. Path: {DbPath}", ex.Message, dbPath);
|
||||
|
||||
// Per servizi Windows, log su Event Log
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
try
|
||||
{
|
||||
using var eventLog = new System.Diagnostics.EventLog("Application");
|
||||
eventLog.Source = "DataCouplerService";
|
||||
eventLog.WriteEntry($"Errore inizializzazione database: {ex.Message}", System.Diagnostics.EventLogEntryType.Error);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignora errori di scrittura EventLog
|
||||
}
|
||||
}
|
||||
|
||||
throw; // Rilancia l'eccezione per non far partire l'app con un database non funzionante
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user