| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors;
- using Microsoft.Extensions.DependencyInjection;
- using static Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress.MdpRelationalSource;
- namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
- public sealed partial class MdpIngestDatasetController
- {
- [HttpGet("{id:long}/relational-sync/{profileId}/schedule")]
- public Task<IActionResult> GetRelationalSchedule(long id, string profileId, long? factoryId, CancellationToken token) =>
- RelationalSchedule(id, profileId, factoryId, false, null, token);
- [HttpPut("{id:long}/relational-sync/{profileId}/schedule")]
- public Task<IActionResult> SaveRelationalSchedule(long id, string profileId, long? factoryId,
- [FromBody] RelationalScheduleInput input, CancellationToken token) =>
- RelationalSchedule(id, profileId, factoryId, true, input.TaskCode, token);
- public sealed record RelationalScheduleInput(string? TaskCode);
- private Task<IActionResult> RelationalSchedule(long id, string profileId, long? factoryId,
- bool save, string? taskCode, CancellationToken token) => WithScope(factoryId, async scope =>
- {
- if (!await Can(TrialPermission) || !await Can(PublishPermission)) return Forbid();
- var dataset = await Datasets(scope).FirstAsync(d => d.Id == id);
- if (dataset is null) return NotFound();
- try
- {
- var profiles = await TrialProfiles(scope, id, token);
- var profile = profiles.SingleOrDefault(p => p.Id == profileId && p.SyncEnabled);
- if (profile is null) return NotFound();
- var source = await BoundSource(dataset, profile.SourceId);
- if (source is null || !dataset.Enabled || dataset.ActiveSchemaVersion is null || await Lifecycle(id) != "ACTIVE")
- return Conflict(new { message = "请先启用该来源已审批的接收版本" });
- var (_, binding, _) = await CreateRelationalTrial(db, profile, dataset, source);
- if (save && !string.IsNullOrWhiteSpace(taskCode))
- {
- using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
- deadline.CancelAfter(TimeSpan.FromSeconds(20));
- var preflight = await InspectProfile(profile, binding, deadline.Token);
- var blocked = preflight.Checks.FirstOrDefault(c => c.Severity == "block");
- if (blocked is not null) return Conflict(new { message = blocked.Message, code = blocked.Code });
- }
- var service = HttpContext.RequestServices.GetRequiredService<MdpRelationalSyncService>();
- if (save)
- await service.SaveScheduleBindingAsync(profile, binding, taskCode?.Trim(), user.UserId, token);
- return Ok(await service.ReadScheduleBindingAsync(profile, binding, token));
- }
- catch (OperationCanceledException) when (!token.IsCancellationRequested)
- {
- return StatusCode(504, new { message = "来源预检超时" });
- }
- catch (MdpIngestHttpException error)
- {
- var message = error.Code == "RELATIONAL_TASK_INELIGIBLE"
- ? "任务需在当前租户启用,并关联当前来源且独立于其他同步实体"
- : "请核对当前来源配置或已批准的接收版本";
- return StatusCode(error.StatusCode, new { message, code = error.Code });
- }
- catch (MySqlConnector.MySqlException error) when (error.Number == 1062)
- {
- return Conflict(new { message = "该任务已绑定其他来源,请刷新后重选" });
- }
- catch (Exception error) when (error is System.Data.Common.DbException or SqlSugarException or ConnectorContractException
- or IOException or System.Text.Json.JsonException or ArgumentException or InvalidOperationException)
- {
- return StatusCode(503, new { message = "请核对调度存储与来源配置" });
- }
- });
- }
|