|
| 1 | +// Copyright (C) 2014-2018 Goodrain Co., Ltd. |
| 2 | +// RAINBOND, Application Management Platform |
| 3 | + |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. For any non-GPL usage of Rainbond, |
| 8 | +// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd. |
| 9 | +// must be obtained first. |
| 10 | + |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU General Public License for more details. |
| 15 | + |
| 16 | +// You should have received a copy of the GNU General Public License |
| 17 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +package handler |
| 20 | + |
| 21 | +import ( |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + "github.com/goodrain/rainbond/api/model" |
| 26 | + "github.com/goodrain/rainbond/api/util" |
| 27 | + "github.com/goodrain/rainbond/db" |
| 28 | + "github.com/goodrain/rainbond/mq/client" |
| 29 | + "github.com/google/uuid" |
| 30 | + "github.com/sirupsen/logrus" |
| 31 | +) |
| 32 | + |
| 33 | +// TarImageHandle tar包镜像处理 |
| 34 | +type TarImageHandle struct { |
| 35 | + MQClient client.MQClient |
| 36 | +} |
| 37 | + |
| 38 | +var tarImageHandle *TarImageHandle |
| 39 | + |
| 40 | +// GetTarImageHandle 获取tar镜像处理handler |
| 41 | +func GetTarImageHandle() *TarImageHandle { |
| 42 | + if tarImageHandle == nil { |
| 43 | + logrus.Error("TarImageHandle is not initialized") |
| 44 | + } |
| 45 | + return tarImageHandle |
| 46 | +} |
| 47 | + |
| 48 | +// CreateTarImageHandle 创建tar镜像处理handler |
| 49 | +func CreateTarImageHandle(mqClient client.MQClient) { |
| 50 | + tarImageHandle = &TarImageHandle{ |
| 51 | + MQClient: mqClient, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// LoadTarImage 开始异步解析tar包镜像 |
| 56 | +func (t *TarImageHandle) LoadTarImage(tenantID string, req model.LoadTarImageReq) (*model.LoadTarImageResp, *util.APIHandleError) { |
| 57 | + loadID := uuid.New().String() |
| 58 | + |
| 59 | + logrus.Infof("[LoadTarImage] Starting, load_id: %s, tenant_id: %s, event_id: %s, tar_file_path: %s", |
| 60 | + loadID, tenantID, req.EventID, req.TarFilePath) |
| 61 | + |
| 62 | + // 构建任务发送到MQ |
| 63 | + task := client.TaskStruct{ |
| 64 | + Topic: "builder", |
| 65 | + TaskType: "load-tar-image", |
| 66 | + TaskBody: map[string]interface{}{ |
| 67 | + "event_id": req.EventID, |
| 68 | + "tar_file_path": req.TarFilePath, |
| 69 | + "load_id": loadID, |
| 70 | + "tenant_id": tenantID, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + logrus.Infof("[LoadTarImage] Sending task to MQ, topic: %s, task_type: %s, task_body: %+v", |
| 75 | + task.Topic, task.TaskType, task.TaskBody) |
| 76 | + |
| 77 | + // 发送任务到消息队列 |
| 78 | + err := t.MQClient.SendBuilderTopic(task) |
| 79 | + if err != nil { |
| 80 | + logrus.Errorf("[LoadTarImage] Failed to send task to MQ: %v", err) |
| 81 | + return nil, util.CreateAPIHandleError(500, fmt.Errorf("启动解析任务失败")) |
| 82 | + } |
| 83 | + |
| 84 | + logrus.Infof("[LoadTarImage] Task sent successfully to MQ, load_id: %s, event_id: %s", loadID, req.EventID) |
| 85 | + |
| 86 | + return &model.LoadTarImageResp{ |
| 87 | + LoadID: loadID, |
| 88 | + Status: "loading", |
| 89 | + }, nil |
| 90 | +} |
| 91 | + |
| 92 | +// GetTarLoadResult 查询tar包解析结果 |
| 93 | +func (t *TarImageHandle) GetTarLoadResult(loadID string) (*model.TarLoadResult, *util.APIHandleError) { |
| 94 | + // 从etcd查询结果 |
| 95 | + key := fmt.Sprintf("/rainbond/tarload/%s", loadID) |
| 96 | + logrus.Infof("[GetTarLoadResult] Querying etcd for load_id: %s, key: %s", loadID, key) |
| 97 | + |
| 98 | + res, err := db.GetManager().KeyValueDao().Get(key) |
| 99 | + if err != nil || res == nil { |
| 100 | + if err != nil { |
| 101 | + logrus.Infof("[GetTarLoadResult] Key not found in etcd (task may still be processing): load_id: %s, error: %v", loadID, err) |
| 102 | + } else { |
| 103 | + logrus.Infof("[GetTarLoadResult] Result is nil for load_id: %s (task may still be processing)", loadID) |
| 104 | + } |
| 105 | + // 如果没找到结果,可能还在处理中 |
| 106 | + return &model.TarLoadResult{ |
| 107 | + LoadID: loadID, |
| 108 | + Status: "loading", |
| 109 | + Message: "正在解析中...", |
| 110 | + }, nil |
| 111 | + } |
| 112 | + |
| 113 | + logrus.Infof("[GetTarLoadResult] Found result in etcd for load_id: %s, value length: %d bytes", loadID, len(res.V)) |
| 114 | + |
| 115 | + // 解析结果 |
| 116 | + var result model.TarLoadResult |
| 117 | + if err := json.Unmarshal([]byte(res.V), &result); err != nil { |
| 118 | + logrus.Errorf("[GetTarLoadResult] Failed to decode result for load_id: %s, error: %v, raw_value: %s", loadID, err, res.V) |
| 119 | + return nil, util.CreateAPIHandleError(500, fmt.Errorf("解析结果格式错误")) |
| 120 | + } |
| 121 | + |
| 122 | + logrus.Infof("[GetTarLoadResult] Successfully decoded result for load_id: %s, status: %s", loadID, result.Status) |
| 123 | + |
| 124 | + return &result, nil |
| 125 | +} |
0 commit comments