using System.Runtime.InteropServices; using System.Text.Json; using Admin.NET.DataPlatform.Worker; using Admin.NET.Plugin.AiDOP.DataPlatform.Core; using MySqlConnector; if (args is ["--check-contract", var datasetKey, var sourceVersion]) { var contract = new PurchaseOrderSchemaRegistry().Find(datasetKey, sourceVersion); if (contract is null) { Console.Error.WriteLine(IngestContractError.UnknownSchema); return 1; } Console.WriteLine(JsonSerializer.Serialize(contract.Snapshot(), JsonSerializerOptions.Web)); return 0; } var dispatch = args.Contains("--dispatch"); if (args is not ([] or ["--once"] or ["--dispatch"] or ["--dispatch", "--once"])) { Console.Error.WriteLine("Usage: Admin.NET.DataPlatform.Worker [--dispatch] [--once] | --check-contract "); return 2; } var mysql = Environment.GetEnvironmentVariable(dispatch ? "MDP_DISPATCH_MYSQL" : "MDP_WORKER_MYSQL"); if (string.IsNullOrWhiteSpace(mysql)) { Console.Error.WriteLine("A dedicated database connection for the selected role is required."); return 2; } try { _ = new MySqlConnectionStringBuilder(mysql); } catch (ArgumentException) { Console.Error.WriteLine("The selected role has an invalid MySQL connection string."); return 2; } using var stopping = new CancellationTokenSource(); Console.CancelKeyPress += (_, error) => { error.Cancel = true; stopping.Cancel(); }; using var sigterm = OperatingSystem.IsWindows() ? null : PosixSignalRegistration.Create(PosixSignal.SIGTERM, signal => { signal.Cancel = true; stopping.Cancel(); }); using var handler = new HttpClientHandler { AllowAutoRedirect = false }; Uri? endpoint = null; byte[]? dispatchKey = null; if (dispatch) { dispatchKey = PurchaseDispatchProof.ReadKey(Environment.GetEnvironmentVariable(PurchaseDispatchProof.CredentialSetting)); if (dispatchKey is null || !Uri.TryCreate(Environment.GetEnvironmentVariable("MDP_PURCHASE_HANDOFF_URL"), UriKind.Absolute, out endpoint) || (endpoint.Scheme != Uri.UriSchemeHttps && !(endpoint.IsLoopback && endpoint.Scheme == Uri.UriSchemeHttp)) || endpoint.UserInfo.Length != 0 || endpoint.Fragment.Length != 0 || endpoint.Query.Length != 0) { Console.Error.WriteLine("The dispatcher requires its own 32-byte key and a valid handoff URL."); return 2; } handler.UseProxy = !endpoint.IsLoopback; } using var http = new HttpClient(handler); Func> runOne = dispatch ? new PurchaseDispatcher(mysql, new PurchaseHandoffClient(http, endpoint!, dispatchKey!)).RunOneAsync : new PurchaseWorker(new PurchaseWorkerStore(mysql)).RunOneAsync; var once = args.Contains("--once"); while (!stopping.IsCancellationRequested) { try { var claimed = await runOne(stopping.Token); if (once) return 0; if (claimed) continue; } catch (OperationCanceledException) when (stopping.IsCancellationRequested) { return 0; } catch (Exception error) { Console.Error.WriteLine($"Purchase Worker iteration failed: {error.GetType().Name}."); if (once) return 1; } try { await Task.Delay(TimeSpan.FromSeconds(1), stopping.Token); } catch (OperationCanceledException) when (stopping.IsCancellationRequested) { return 0; } } return 0;