Program.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Runtime.InteropServices;
  2. using System.Text.Json;
  3. using Admin.NET.DataPlatform.Worker;
  4. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  5. using MySqlConnector;
  6. if (args is ["--check-contract", var datasetKey, var sourceVersion])
  7. {
  8. var contract = new PurchaseOrderSchemaRegistry().Find(datasetKey, sourceVersion);
  9. if (contract is null)
  10. {
  11. Console.Error.WriteLine(IngestContractError.UnknownSchema);
  12. return 1;
  13. }
  14. Console.WriteLine(JsonSerializer.Serialize(contract.Snapshot(), JsonSerializerOptions.Web));
  15. return 0;
  16. }
  17. var dispatch = args.Contains("--dispatch");
  18. if (args is not ([] or ["--once"] or ["--dispatch"] or ["--dispatch", "--once"]))
  19. {
  20. Console.Error.WriteLine("Usage: Admin.NET.DataPlatform.Worker [--dispatch] [--once] | --check-contract <dataset> <schema>");
  21. return 2;
  22. }
  23. var mysql = Environment.GetEnvironmentVariable(dispatch ? "MDP_DISPATCH_MYSQL" : "MDP_WORKER_MYSQL");
  24. if (string.IsNullOrWhiteSpace(mysql))
  25. {
  26. Console.Error.WriteLine("A dedicated database connection for the selected role is required.");
  27. return 2;
  28. }
  29. try
  30. {
  31. _ = new MySqlConnectionStringBuilder(mysql);
  32. }
  33. catch (ArgumentException)
  34. {
  35. Console.Error.WriteLine("The selected role has an invalid MySQL connection string.");
  36. return 2;
  37. }
  38. using var stopping = new CancellationTokenSource();
  39. Console.CancelKeyPress += (_, error) =>
  40. {
  41. error.Cancel = true;
  42. stopping.Cancel();
  43. };
  44. using var sigterm = OperatingSystem.IsWindows() ? null : PosixSignalRegistration.Create(PosixSignal.SIGTERM, signal =>
  45. {
  46. signal.Cancel = true;
  47. stopping.Cancel();
  48. });
  49. using var handler = new HttpClientHandler { AllowAutoRedirect = false };
  50. Uri? endpoint = null;
  51. byte[]? dispatchKey = null;
  52. if (dispatch)
  53. {
  54. dispatchKey = PurchaseDispatchProof.ReadKey(Environment.GetEnvironmentVariable(PurchaseDispatchProof.CredentialSetting));
  55. if (dispatchKey is null || !Uri.TryCreate(Environment.GetEnvironmentVariable("MDP_PURCHASE_HANDOFF_URL"), UriKind.Absolute, out endpoint)
  56. || (endpoint.Scheme != Uri.UriSchemeHttps && !(endpoint.IsLoopback && endpoint.Scheme == Uri.UriSchemeHttp))
  57. || endpoint.UserInfo.Length != 0 || endpoint.Fragment.Length != 0 || endpoint.Query.Length != 0)
  58. {
  59. Console.Error.WriteLine("The dispatcher requires its own 32-byte key and a valid handoff URL.");
  60. return 2;
  61. }
  62. handler.UseProxy = !endpoint.IsLoopback;
  63. }
  64. using var http = new HttpClient(handler);
  65. Func<CancellationToken, Task<bool>> runOne = dispatch
  66. ? new PurchaseDispatcher(mysql, new PurchaseHandoffClient(http, endpoint!, dispatchKey!)).RunOneAsync
  67. : new PurchaseWorker(new PurchaseWorkerStore(mysql)).RunOneAsync;
  68. var once = args.Contains("--once");
  69. while (!stopping.IsCancellationRequested)
  70. {
  71. try
  72. {
  73. var claimed = await runOne(stopping.Token);
  74. if (once)
  75. return 0;
  76. if (claimed)
  77. continue;
  78. }
  79. catch (OperationCanceledException) when (stopping.IsCancellationRequested)
  80. {
  81. return 0;
  82. }
  83. catch (Exception error)
  84. {
  85. Console.Error.WriteLine($"Purchase Worker iteration failed: {error.GetType().Name}.");
  86. if (once)
  87. return 1;
  88. }
  89. try
  90. {
  91. await Task.Delay(TimeSpan.FromSeconds(1), stopping.Token);
  92. }
  93. catch (OperationCanceledException) when (stopping.IsCancellationRequested)
  94. {
  95. return 0;
  96. }
  97. }
  98. return 0;