MdpRelationalScheduleController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using static Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress.MdpRelationalSource;
  4. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  5. public sealed partial class MdpIngestDatasetController
  6. {
  7. [HttpGet("{id:long}/relational-sync/{profileId}/schedule")]
  8. public Task<IActionResult> GetRelationalSchedule(long id, string profileId, long? factoryId, CancellationToken token) =>
  9. RelationalSchedule(id, profileId, factoryId, false, null, token);
  10. [HttpPut("{id:long}/relational-sync/{profileId}/schedule")]
  11. public Task<IActionResult> SaveRelationalSchedule(long id, string profileId, long? factoryId,
  12. [FromBody] RelationalScheduleInput input, CancellationToken token) =>
  13. RelationalSchedule(id, profileId, factoryId, true, input.TaskCode, token);
  14. public sealed record RelationalScheduleInput(string? TaskCode);
  15. private Task<IActionResult> RelationalSchedule(long id, string profileId, long? factoryId,
  16. bool save, string? taskCode, CancellationToken token) => WithScope(factoryId, async scope =>
  17. {
  18. if (!await Can(TrialPermission) || !await Can(PublishPermission)) return Forbid();
  19. var dataset = await Datasets(scope).FirstAsync(d => d.Id == id);
  20. if (dataset is null) return NotFound();
  21. try
  22. {
  23. var profiles = await TrialProfiles(scope, id, token);
  24. var profile = profiles.SingleOrDefault(p => p.Id == profileId && p.SyncEnabled);
  25. if (profile is null) return NotFound();
  26. var source = await BoundSource(dataset, profile.SourceId);
  27. if (source is null || !dataset.Enabled || dataset.ActiveSchemaVersion is null || await Lifecycle(id) != "ACTIVE")
  28. return Conflict(new { message = "请先启用该来源已审批的接收版本" });
  29. var (_, binding, _) = await CreateRelationalTrial(db, profile, dataset, source);
  30. if (save && !string.IsNullOrWhiteSpace(taskCode))
  31. {
  32. using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
  33. deadline.CancelAfter(TimeSpan.FromSeconds(20));
  34. var preflight = await InspectProfile(profile, binding, deadline.Token);
  35. var blocked = preflight.Checks.FirstOrDefault(c => c.Severity == "block");
  36. if (blocked is not null) return Conflict(new { message = blocked.Message, code = blocked.Code });
  37. }
  38. var service = HttpContext.RequestServices.GetRequiredService<MdpRelationalSyncService>();
  39. if (save)
  40. await service.SaveScheduleBindingAsync(profile, binding, taskCode?.Trim(), user.UserId, token);
  41. return Ok(await service.ReadScheduleBindingAsync(profile, binding, token));
  42. }
  43. catch (OperationCanceledException) when (!token.IsCancellationRequested)
  44. {
  45. return StatusCode(504, new { message = "来源预检超时" });
  46. }
  47. catch (MdpIngestHttpException error)
  48. {
  49. var message = error.Code == "RELATIONAL_TASK_INELIGIBLE"
  50. ? "任务需在当前租户启用,并关联当前来源且独立于其他同步实体"
  51. : "请核对当前来源配置或已批准的接收版本";
  52. return StatusCode(error.StatusCode, new { message, code = error.Code });
  53. }
  54. catch (MySqlConnector.MySqlException error) when (error.Number == 1062)
  55. {
  56. return Conflict(new { message = "该任务已绑定其他来源,请刷新后重选" });
  57. }
  58. catch (Exception error) when (error is System.Data.Common.DbException or SqlSugarException or ConnectorContractException
  59. or IOException or System.Text.Json.JsonException or ArgumentException or InvalidOperationException)
  60. {
  61. return StatusCode(503, new { message = "请核对调度存储与来源配置" });
  62. }
  63. });
  64. }