using SmartB2B.Optima.Adapter; using SmartB2B.Optima.Service.Configuration; using SmartB2B.Sync.Contracts; using SmartB2B.Sync.Service; using SmartB2B.Sync.Service.Diagnostics; using SmartB2B.Sync.Service.Sql; namespace SmartB2B.Optima.Service; internal static class Program { public static async Task Main(string[] args) { ConsoleLogger.InstallTimestampedOutput(); IDisposable? sentry = null; IErpOrderAdapter? adapter = null; var cancellation = new CancellationTokenSource(); Console.CancelKeyPress += (_, eventArgs) => { eventArgs.Cancel = true; cancellation.Cancel(); }; AppDomain.CurrentDomain.ProcessExit += (_, _) => cancellation.Cancel(); try { var commandLine = ServiceCommandLine.Parse(args, "optima.json"); ConsoleLogger.Configure(commandLine.VerboseLogging); var settings = OptimaServiceSettings.Load(commandLine.ConfigurationPath); var runtime = settings.ResolveRuntimeSettings(commandLine.VerboseLogging); var serviceVersion = typeof(Program).Assembly.GetName().Version?.ToString(3) ?? "unknown"; sentry = SentryBootstrap.Initialize( settings.Sentry.Dsn, settings.Portal, "optima", serviceVersion, commandLine.ConfigurationPath, commandLine.VerboseLogging); adapter = new OptimaOrderAdapter(runtime.InstallationPath); var adapterInfo = adapter.Initialize(runtime.AdapterConfiguration); Console.WriteLine($"Załadowano {adapterInfo.ErpName} {adapterInfo.ErpVersion} z '{runtime.InstallationPath}'."); Console.WriteLine($"Firma/baza: {adapterInfo.DatabaseName}; portal WAMP: {settings.Portal}."); if (commandLine.CheckConfigurationOnly) { var sql = new SqlRawHandler(runtime.SqlConnectionString, runtime.SqlCommandTimeoutSeconds); await sql.CheckConnectionAsync(cancellation.Token).ConfigureAwait(false); adapter.CheckConnection(); Console.WriteLine("Konfiguracja, połączenie SQL i logowanie COM Optimy są poprawne."); return 0; } var options = new SyncServiceOptions( "SmartB2B Optima Sync", serviceVersion, "Optima path", runtime.InstallationPath, settings.Portal, settings.Wamp.ServerUrl, settings.Wamp.ReconnectDelaySeconds, runtime.SqlConnectionString, runtime.SqlCommandTimeoutSeconds, runtime.LogDirectory, runtime.LogPrefix); await SyncServiceRunner.RunAsync( options, adapter, runtime.AdapterConfiguration, adapterInfo, cancellation.Token) .ConfigureAwait(false); Console.WriteLine("SmartB2B Optima Sync zatrzymany."); return 0; } catch (ServiceConfigurationException exception) { Console.Error.WriteLine($"Błąd konfiguracji: {exception.Message}"); return 2; } catch (Exception exception) { Console.Error.WriteLine($"Błąd krytyczny: {exception}"); await SentryReporter.CaptureAsync(exception).ConfigureAwait(false); return 1; } finally { adapter?.Dispose(); sentry?.Dispose(); } } }