This commit is contained in:
@@ -2,21 +2,46 @@ package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"schedule_converter/service"
|
||||
|
||||
pb "schedule_converter/proto/runner"
|
||||
)
|
||||
|
||||
type TaskHandler struct {
|
||||
cancelledTasks sync.Map
|
||||
service *service.Service
|
||||
}
|
||||
|
||||
func NewTaskHandler() *TaskHandler {
|
||||
return &TaskHandler{}
|
||||
return &TaskHandler{service: service.NewService()}
|
||||
}
|
||||
|
||||
func supportedTaskTypes() []pb.TaskType {
|
||||
return []pb.TaskType{
|
||||
pb.TaskType_TASK_TYPE_LOGIN,
|
||||
pb.TaskType_TASK_TYPE_GET_SCHEDULE,
|
||||
pb.TaskType_TASK_TYPE_GET_GRADES,
|
||||
pb.TaskType_TASK_TYPE_GET_EXAMS,
|
||||
pb.TaskType_TASK_TYPE_GET_EMPTY_CLASSROOM,
|
||||
pb.TaskType_TASK_TYPE_GET_CET,
|
||||
}
|
||||
}
|
||||
|
||||
func unmarshalPayload(payload []byte, v any) error {
|
||||
if len(payload) == 0 {
|
||||
return fmt.Errorf("任务 payload 为空")
|
||||
}
|
||||
if err := json.Unmarshal(payload, v); err != nil {
|
||||
return fmt.Errorf("解析任务 payload 失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *TaskHandler) Execute(task *pb.Task) *pb.TaskResult {
|
||||
@@ -71,6 +96,8 @@ func (h *TaskHandler) dispatch(ctx context.Context, task *pb.Task) ([]byte, erro
|
||||
return h.login(ctx, task.Payload)
|
||||
case pb.TaskType_TASK_TYPE_GET_EMPTY_CLASSROOM:
|
||||
return h.getEmptyClassrooms(ctx, task.Payload)
|
||||
case pb.TaskType_TASK_TYPE_GET_CET:
|
||||
return h.getCET(ctx, task.Payload)
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: %v", errUnknownTaskType, task.Type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user