This commit is contained in:
@@ -107,24 +107,38 @@ func (h *ExportShardHandler) HandleExportShard(ctx context.Context, task *asynq.
|
||||
}
|
||||
}
|
||||
|
||||
strategy, ok := h.sceneRegistry.Get(exportTask.Scene)
|
||||
source, ok := h.sceneRegistry.Get(exportTask.Scene)
|
||||
if !ok {
|
||||
_ = h.markShardFinalFailed(ctx, exportTask.ID, shardTask.ID, updater, "导出场景未注册")
|
||||
return asynq.SkipRetry
|
||||
}
|
||||
|
||||
rows, err := strategy.QueryRows(ctx, exportTask, shardTask.CursorStart, shardTask.CursorEnd)
|
||||
headers := exporter.ResolvedHeadersFromTask(exportTask)
|
||||
if len(headers) == 0 {
|
||||
return h.handleShardError(ctx, exportTask.ID, shardTask.ID, updater, "读取导出表头失败", fmt.Errorf("resolved_headers 为空"))
|
||||
}
|
||||
|
||||
params := exporter.ParseExportParams(exportTask)
|
||||
rows, err := source.Fetch(ctx, params, shardTask.ShardOffset, shardTask.ShardLimit)
|
||||
if err != nil {
|
||||
return h.handleShardError(ctx, exportTask.ID, shardTask.ID, updater, "查询分片数据失败", err)
|
||||
}
|
||||
if hasMisalignedRows(rows, headers) {
|
||||
h.logger.Warn("导出分片数据列数与表头不一致,已按固定表头对齐",
|
||||
zap.Uint("task_id", exportTask.ID),
|
||||
zap.Uint("shard_id", shardTask.ID),
|
||||
zap.Int("header_count", len(headers)),
|
||||
)
|
||||
}
|
||||
rows = alignRowsToHeaders(rows, headers)
|
||||
|
||||
localPath, fileSize, err := writeExportFile(exportTask.Format, strategy.Headers(), rows)
|
||||
localPath, fileSize, err := writeExportFile(constants.ExportTaskFormatCSV, headers, rows, true)
|
||||
if err != nil {
|
||||
return h.handleShardError(ctx, exportTask.ID, shardTask.ID, updater, "生成分片文件失败", err)
|
||||
}
|
||||
defer func() { _ = os.Remove(localPath) }()
|
||||
|
||||
fileName := fmt.Sprintf("%s-shard-%04d.%s", exportTask.TaskNo, shardTask.ShardNo, exportTask.Format)
|
||||
fileName := fmt.Sprintf("%s-shard-%04d.csv.shard", exportTask.TaskNo, shardTask.ShardNo)
|
||||
fileKey, err := uploadExportFile(ctx, h.storageSvc, localPath, fileName)
|
||||
if err != nil {
|
||||
return h.handleShardError(ctx, exportTask.ID, shardTask.ID, updater, "上传分片文件失败", err)
|
||||
|
||||
Reference in New Issue
Block a user