2023-08-23 16:17:33 +08:00
|
|
|
|
package task
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"gitlink.org.cn/cloudream/common/api/storage"
|
|
|
|
|
|
"gitlink.org.cn/cloudream/common/models"
|
|
|
|
|
|
"gitlink.org.cn/cloudream/common/pkgs/logger"
|
2023-09-04 09:51:20 +08:00
|
|
|
|
"gitlink.org.cn/cloudream/common/pkgs/task"
|
2023-08-29 16:33:05 +08:00
|
|
|
|
"gitlink.org.cn/cloudream/scheduler/common/globals"
|
2023-09-04 16:48:46 +08:00
|
|
|
|
exectsk "gitlink.org.cn/cloudream/scheduler/common/pkgs/mq/executor/task"
|
2023-08-23 16:17:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type StorageCreatePackage struct {
|
|
|
|
|
|
userID int64
|
|
|
|
|
|
storageID int64
|
|
|
|
|
|
path string
|
|
|
|
|
|
bucketID int64
|
|
|
|
|
|
name string
|
|
|
|
|
|
redundancy models.TypedRedundancyInfo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewStorageCreatePackage(userID int64, storageID int64, path string, bucketID int64, name string, redundancy models.TypedRedundancyInfo) *StorageCreatePackage {
|
|
|
|
|
|
return &StorageCreatePackage{
|
|
|
|
|
|
userID: userID,
|
|
|
|
|
|
storageID: storageID,
|
|
|
|
|
|
path: path,
|
|
|
|
|
|
bucketID: bucketID,
|
|
|
|
|
|
name: name,
|
|
|
|
|
|
redundancy: redundancy,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-04 09:51:20 +08:00
|
|
|
|
func (t *StorageCreatePackage) Execute(task *task.Task[TaskContext], ctx TaskContext, complete CompleteFn) {
|
2023-08-23 16:17:33 +08:00
|
|
|
|
log := logger.WithType[StorageCreatePackage]("Task")
|
|
|
|
|
|
log.Debugf("begin")
|
|
|
|
|
|
defer log.Debugf("end")
|
|
|
|
|
|
|
2023-09-04 16:48:46 +08:00
|
|
|
|
err := t.do(task.ID(), ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
//TODO 若任务失败,上报的状态failed字段根据情况修改
|
2023-09-04 16:56:13 +08:00
|
|
|
|
ctx.reporter.Report(task.ID(), exectsk.NewCreatePackageTaskStatus("failed", err.Error(), 0))
|
2023-09-04 16:48:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
ctx.reporter.ReportNow()
|
2023-09-04 09:51:20 +08:00
|
|
|
|
|
2023-08-23 16:17:33 +08:00
|
|
|
|
complete(err, CompleteOption{
|
|
|
|
|
|
RemovingDelay: time.Minute,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-04 16:48:46 +08:00
|
|
|
|
func (t *StorageCreatePackage) do(taskID string, ctx TaskContext) error {
|
2023-08-23 16:17:33 +08:00
|
|
|
|
stgCli, err := globals.CloudreamStoragePool.Acquire()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("new cloudream storage client: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
defer stgCli.Close()
|
|
|
|
|
|
|
|
|
|
|
|
resp, err := stgCli.StorageCreatePackage(storage.StorageCreatePackageReq{
|
|
|
|
|
|
UserID: t.userID,
|
|
|
|
|
|
StorageID: t.storageID,
|
|
|
|
|
|
Path: t.path,
|
|
|
|
|
|
BucketID: t.bucketID,
|
|
|
|
|
|
Name: t.name,
|
|
|
|
|
|
Redundancy: t.redundancy,
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-04 16:48:46 +08:00
|
|
|
|
// TODO 根据接口result返回情况修改
|
2023-09-04 16:56:13 +08:00
|
|
|
|
ctx.reporter.Report(taskID, exectsk.NewCreatePackageTaskStatus("completed", "", resp.PackageID))
|
2023-08-23 16:17:33 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|