后端:
1. 阶段 5 schedule 首刀服务化落地,新增 `cmd/schedule`、`services/schedule/{dao,rpc,sv,core}`、`gateway/client/schedule`、`shared/contracts/schedule` 和 schedule port
2. gateway `/api/v1/schedule/*` 切到 schedule zrpc client,HTTP 门面只保留鉴权、参数绑定、超时和轻量转发
3. active-scheduler 的 schedule facts、feedback 和 confirm apply 改为调用 schedule RPC adapter,减少对 `schedule_events`、`schedules`、`task_classes`、`task_items` 的跨域 DB 依赖
4. 单体聊天主动调度 rerun 的 schedule 读写链路切到 schedule RPC,迁移期仅保留 task facts 直读 Gorm
5. 为 schedule zrpc 补充 `Ping` 启动健康检查,并在 gateway client 与 active-scheduler adapter 初始化时校验服务可用
6. `cmd/schedule` 独立初始化 DB / Redis,只 AutoMigrate schedule 自有表,并显式检查迁移期 task / task-class 依赖表
7. 更新 active-scheduler 依赖表检查和 preview confirm apply 抽象,保留旧 Gorm 实现作为迁移期回退路径
8. 补充 `schedule.rpc` 示例配置和 schedule HTTP RPC 超时配置
文档:
1. 更新微服务迁移计划,将阶段 5 schedule 首刀进展、当前切流点、旧实现保留范围和 active-scheduler DB 依赖收缩情况写入基线
114 lines
3.3 KiB
Go
114 lines
3.3 KiB
Go
package rpc
|
||
|
||
import (
|
||
"errors"
|
||
"log"
|
||
"strings"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/respond"
|
||
"github.com/LoveLosita/smartflow/backend/services/schedule/core/applyadapter"
|
||
"google.golang.org/genproto/googleapis/rpc/errdetails"
|
||
"google.golang.org/grpc/codes"
|
||
"google.golang.org/grpc/status"
|
||
)
|
||
|
||
const (
|
||
scheduleErrorDomain = "smartflow.schedule"
|
||
scheduleApplyErrorDomain = "smartflow.schedule.apply"
|
||
)
|
||
|
||
// grpcErrorFromServiceError 负责把 schedule 内部错误转换为 gRPC status。
|
||
//
|
||
// 职责边界:
|
||
// 1. apply 业务错误保留错误码,供 active-scheduler 反解后继续按原 confirm 语义处理;
|
||
// 2. respond.Response 继续传输项目内部 status/info;
|
||
// 3. 未分类错误只暴露通用内部错误,详细信息留在服务日志。
|
||
func grpcErrorFromServiceError(err error) error {
|
||
if err == nil {
|
||
return nil
|
||
}
|
||
var applyErr *applyadapter.ApplyError
|
||
if errors.As(err, &applyErr) {
|
||
return grpcErrorFromApplyError(applyErr)
|
||
}
|
||
var resp respond.Response
|
||
if errors.As(err, &resp) {
|
||
return grpcErrorFromResponse(resp)
|
||
}
|
||
log.Printf("schedule rpc internal error: %v", err)
|
||
return status.Error(codes.Internal, "schedule service internal error")
|
||
}
|
||
|
||
func grpcErrorFromApplyError(applyErr *applyadapter.ApplyError) error {
|
||
if applyErr == nil {
|
||
return status.Error(codes.Internal, "schedule apply error")
|
||
}
|
||
message := strings.TrimSpace(applyErr.Message)
|
||
if message == "" {
|
||
message = strings.TrimSpace(applyErr.Code)
|
||
}
|
||
st := status.New(grpcCodeFromApplyErrorCode(applyErr.Code), message)
|
||
detail := &errdetails.ErrorInfo{
|
||
Domain: scheduleApplyErrorDomain,
|
||
Reason: applyErr.Code,
|
||
Metadata: map[string]string{
|
||
"info": message,
|
||
},
|
||
}
|
||
withDetails, err := st.WithDetails(detail)
|
||
if err != nil {
|
||
return st.Err()
|
||
}
|
||
return withDetails.Err()
|
||
}
|
||
|
||
func grpcErrorFromResponse(resp respond.Response) error {
|
||
code := grpcCodeFromRespondStatus(resp.Status)
|
||
message := strings.TrimSpace(resp.Info)
|
||
if message == "" {
|
||
message = strings.TrimSpace(resp.Status)
|
||
}
|
||
st := status.New(code, message)
|
||
detail := &errdetails.ErrorInfo{
|
||
Domain: scheduleErrorDomain,
|
||
Reason: resp.Status,
|
||
Metadata: map[string]string{
|
||
"info": resp.Info,
|
||
},
|
||
}
|
||
withDetails, err := st.WithDetails(detail)
|
||
if err != nil {
|
||
return st.Err()
|
||
}
|
||
return withDetails.Err()
|
||
}
|
||
|
||
func grpcCodeFromApplyErrorCode(code string) codes.Code {
|
||
switch strings.TrimSpace(code) {
|
||
case applyadapter.ErrorCodeTargetNotFound:
|
||
return codes.NotFound
|
||
case applyadapter.ErrorCodeDBError:
|
||
return codes.Internal
|
||
case applyadapter.ErrorCodeTargetCompleted,
|
||
applyadapter.ErrorCodeTargetAlreadyScheduled,
|
||
applyadapter.ErrorCodeSlotConflict:
|
||
return codes.FailedPrecondition
|
||
default:
|
||
return codes.InvalidArgument
|
||
}
|
||
}
|
||
|
||
func grpcCodeFromRespondStatus(statusValue string) codes.Code {
|
||
switch strings.TrimSpace(statusValue) {
|
||
case respond.MissingToken.Status, respond.InvalidToken.Status, respond.InvalidClaims.Status,
|
||
respond.ErrUnauthorized.Status, respond.WrongTokenType.Status, respond.UserLoggedOut.Status:
|
||
return codes.Unauthenticated
|
||
case respond.MissingParam.Status, respond.WrongParamType.Status, respond.ParamTooLong.Status:
|
||
return codes.InvalidArgument
|
||
}
|
||
if strings.HasPrefix(strings.TrimSpace(statusValue), "5") {
|
||
return codes.Internal
|
||
}
|
||
return codes.InvalidArgument
|
||
}
|