Skip to content

Commit

Permalink
fix download issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronchen2k committed Nov 1, 2022
1 parent 3d0d7ad commit 4d547be
Show file tree
Hide file tree
Showing 18 changed files with 233 additions and 166 deletions.
41 changes: 20 additions & 21 deletions cmd/host/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
_cronUtils "github.com/easysoft/zagent/pkg/lib/cron"
"github.com/kataras/iris/v12"
"sync"
"time"
)

type CronService struct {
Expand All @@ -27,26 +26,26 @@ func (s *CronService) Init() {
s.syncMap.Store("isRunning", false)
s.syncMap.Store("lastCompletedTime", int64(0))

_cronUtils.AddTask(
"execution",
fmt.Sprintf("@every %ds", consts.AgentCheckExecutionInterval),
func() {
isRunning, _ := s.syncMap.Load("isRunning")
lastCompletedTime, _ := s.syncMap.Load("lastCompletedTime")

if isRunning.(bool) || time.Now().Unix()-lastCompletedTime.(int64) < consts.AgentCheckExecutionInterval {
//_logUtils.Infof("skip this iteration " + _dateUtils.DateTimeStr(time.Now()))
return
}
s.syncMap.Store("isRunning", true)

//
s.HostService.Check()

s.syncMap.Store("isRunning", false)
s.syncMap.Store("lastCompletedTime", time.Now().Unix())
},
)
//_cronUtils.AddTask(
// "execution",
// fmt.Sprintf("@every %ds", consts.AgentCheckExecutionInterval),
// func() {
// isRunning, _ := s.syncMap.Load("isRunning")
// lastCompletedTime, _ := s.syncMap.Load("lastCompletedTime")
//
// if isRunning.(bool) || time.Now().Unix()-lastCompletedTime.(int64) < consts.AgentCheckExecutionInterval {
// //_logUtils.Infof("skip this iteration " + _dateUtils.DateTimeStr(time.Now()))
// return
// }
// s.syncMap.Store("isRunning", true)
//
// //
// s.HostService.Check()
//
// s.syncMap.Store("isRunning", false)
// s.syncMap.Store("lastCompletedTime", time.Now().Unix())
// },
//)

_cronUtils.AddTask(
"download",
Expand Down
2 changes: 1 addition & 1 deletion cmd/host/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func Init() {
agentConf.Init(consts.AppNameAgentHost)
_db.InitDB("agent")
_db.InitDB("host")

irisServer := NewServer(nil)
irisServer.App.Logger().SetLevel("info")
Expand Down
2 changes: 1 addition & 1 deletion cmd/host/router/handler/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewDownloadCtrl() *DownloadCtrl {
// @Success 200 {object} _domain.Response "code = success | fail"
// @Router /api/v1/download/add [post]
func (c *DownloadCtrl) Add(ctx iris.Context) {
req := v1.DownloadReq{}
req := make([]v1.DownloadReq, 0)
err := ctx.ReadJSON(&req)
if err != nil {
_, _ = ctx.JSON(_httpUtils.RespData(consts.ResultFail, err.Error(), nil))
Expand Down
6 changes: 3 additions & 3 deletions cmd/host/router/v1/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package v1
import consts "github.com/easysoft/zagent/internal/pkg/const"

type DownloadReq struct {
Urls []string `json:"urls"`

ZentaoTask int `json:"zentaoTask"`
Url string `json:"url"`
Md5 string `json:"md5"`
ZentaoTask int `json:"zentaoTask"`
}

type DownloadResp struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/host/model/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ type BaseModel struct {

ID uint `gorm:"primary_key" sql:"type:INT(10) UNSIGNED NOT NULL" json:"id"`
CreatedAt *time.Time `json:"createdAt"`
UpdatedAt *time.Time `json:"updatedAt"`
DeletedAt *time.Time `sql:"index" json:"deletedAt"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
DeletedAt *time.Time `sql:"index" json:"deletedAt,omitempty"`

Deleted bool `json:"deleted" gorm:"default:false"`
Disabled bool `json:"disabled" gorm:"default:false"`
Deleted bool `json:"deleted,omitempty" gorm:"default:false"`
Disabled bool `json:"disabled,omitempty" gorm:"default:false"`
}

var (
Expand Down
18 changes: 9 additions & 9 deletions internal/host/model/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ type Task struct {
BaseModel

Name string `json:"name"`
Desc string `json:"desc"`
Desc string `json:"desc,omitempty"`

// for download
Url string `json:"url"`
Md5 string `json:"md5"`
Path string `json:"path"`
Url string `json:"url,omitempty"`
Md5 string `json:"md5,omitempty"`
Path string `json:"path,omitempty"`
Status consts.TaskStatus `json:"status"`
Retry int `json:"retry"`
CompletionRate float64 `json:"completionRate" gorm:"-"`

// for export vm
Vm string `json:"vm"`
Backing string `json:"backing"`
Xml string `json:"xml"`
//Path string `json:"path"`
Vm string `json:"vm,omitempty"`
Backing string `json:"backing,omitempty"`
Xml string `json:"xml,omitempty"`
//Path string `json:"path,omitempty"`

StartTime *time.Time `json:"startTime"`
EndTime *time.Time `json:"endTime"`
TimeoutTime *time.Time `json:"timeoutTime"`
TimeoutTime *time.Time `json:"timeoutTime,omitempty"`

ZentaoTask int `json:"zentaoTask"`
TaskType consts.TaskType `json:"taskType"`
Expand Down
24 changes: 19 additions & 5 deletions internal/host/repo/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,29 @@ func (r *TaskRepo) Update(po *agentModel.Task) (err error) {
return
}

func (r *TaskRepo) UpdateStatus(id uint, filePath, xmlDesc string, status consts.TaskStatus) (err error) {
err = r.DB.Model(&agentModel.Task{}).Where("id = ?", id).
Updates(map[string]interface{}{"status": status, "end_time": time.Now()}).Error
func (r *TaskRepo) UpdateStatus(id uint, filePath string, completionRate int, xmlDesc string,
status consts.TaskStatus, isStart, isEnd bool) (err error) {

updates := map[string]interface{}{"status": status, "xml_desc": xmlDesc}

if filePath != "" {
err = r.DB.Model(&agentModel.Task{}).Where("id = ?", id).
Updates(map[string]interface{}{"path": filePath}).Error
updates["path"] = filePath
}

if completionRate > 0 {
updates["completion_rate"] = completionRate
}

if isStart {
updates["start_time"] = time.Now()
}
if isEnd {
updates["end_time"] = time.Now()
}

err = r.DB.Model(&agentModel.Task{}).Where("id = ?", id).
Updates(updates).Error

return
}

Expand Down
21 changes: 15 additions & 6 deletions internal/host/service/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ func NewDownloadService() *DownloadService {
return &DownloadService{}
}

func (s *DownloadService) AddTasks(req v1.DownloadReq) (err error) {
for _, item := range req.Urls {
func (s *DownloadService) AddTasks(req []v1.DownloadReq) (err error) {
for _, item := range req {
po := agentModel.Task{
Url: item,
ZentaoTask: req.ZentaoTask,
Url: item.Url,
Md5: item.Md5,
ZentaoTask: item.ZentaoTask,
TaskType: consts.DownloadImage,
Status: consts.Created,
}

s.TaskRepo.Save(&po)
Expand All @@ -43,9 +45,16 @@ func (s *DownloadService) StartTask(po agentModel.Task) {
channelMap.Store(int(po.ID), ch)

go func() {
filePath, finalStatus := downloadUtils.Start(po, ch)
filePath := downloadUtils.GetPath(po)

s.TaskRepo.UpdateStatus(po.ID, filePath, "", finalStatus)
s.TaskRepo.UpdateStatus(po.ID, filePath, 0, "", consts.InProgress, true, false)

finalStatus, existFile := downloadUtils.Start(po, filePath, ch)
if existFile != "" {
filePath = existFile
}

s.TaskRepo.UpdateStatus(po.ID, filePath, 1, "", finalStatus, false, true)

po = s.TaskRepo.Get(po.ID)
s.TaskService.SubmitResult(po)
Expand Down
13 changes: 8 additions & 5 deletions internal/host/service/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ func (s *ExportService) StartTask(po agentModel.Task) {
channelMap.Store(int(po.ID), ch)

go func() {
filePath, xmlDesc, finalStatus := s.ExportVm(po)
s.TaskRepo.UpdateStatus(po.ID, filePath, xmlDesc, finalStatus)
filePath := filepath.Join(consts.FolderBacking, po.Name)

s.TaskRepo.UpdateStatus(po.ID, filePath, 0, "", consts.InProgress, true, false)

xmlDesc, finalStatus := s.ExportVm(po, filePath)

s.TaskRepo.UpdateStatus(po.ID, filePath, 0, xmlDesc, finalStatus, false, true)

po = s.TaskRepo.Get(po.ID)
s.TaskService.SubmitResult(po)
Expand All @@ -39,7 +44,7 @@ func (s *ExportService) StartTask(po agentModel.Task) {
}()
}

func (s *ExportService) ExportVm(po agentModel.Task) (pth, xml string, status consts.TaskStatus) {
func (s *ExportService) ExportVm(po agentModel.Task, pth string) (xml string, status consts.TaskStatus) {
vmName := po.Vm

dom, err := s.LibvirtService.GetVm(vmName)
Expand All @@ -57,8 +62,6 @@ func (s *ExportService) ExportVm(po agentModel.Task) (pth, xml string, status co
return
}

pth = filepath.Join(consts.FolderBacking, vmName)

s.LibvirtService.ShutdownVmByName(vmName)

cmd := fmt.Sprintf(consts.CmdExportVm, vmDiskPath, pth)
Expand Down
8 changes: 7 additions & 1 deletion internal/host/service/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ func (s *TaskService) CheckTask() (err error) {
}

func (s *TaskService) ListTask() (ret v1.ListTaskResp, err error) {
ret = v1.ListTaskResp{}
ret = v1.ListTaskResp{
Created: make([]agentModel.Task, 0),
InProgress: make([]agentModel.Task, 0),
Canceled: make([]agentModel.Task, 0),
Completed: make([]agentModel.Task, 0),
Failed: make([]agentModel.Task, 0),
}

pos, _ := s.TaskRepo.Query()

Expand Down
1 change: 1 addition & 0 deletions internal/pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Init(app string) {
Inst.WorkDir = _fileUtils.AddPathSepIfNeeded(filepath.Join(home, consts.AppName))

if Inst.RunMode == consts.RunModeHost {
consts.DownloadDir = _fileUtils.AddPathSepIfNeeded(filepath.Join(Inst.WorkDir, consts.FolderDownload))
consts.NovncDir = _fileUtils.AddPathSepIfNeeded(filepath.Join(Inst.WorkDir, consts.FolderNovnc))
consts.WebsockifyDir = _fileUtils.AddPathSepIfNeeded(filepath.Join(Inst.WorkDir, consts.FolderWebsockify))

Expand Down
1 change: 0 additions & 1 deletion internal/pkg/const/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package consts

const (
AppName = "zagent"
AppNameServer = "server"
AppNameAgent = "agent"
AppNameAgentHost = "host"
AppNameAgentVm = "vm"
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/const/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
LogDir = fmt.Sprintf("log%s", _const.PthSep)
ControlActions = []string{"start", "stop", "restart", "install", "uninstall"}

DownloadDir = ""
NovncDir = ""
WebsockifyDir = ""

Expand Down
17 changes: 5 additions & 12 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package _db

import (
"fmt"
agentConf "github.com/easysoft/zagent/internal/pkg/conf"
consts "github.com/easysoft/zagent/internal/pkg/const"
_fileUtils "github.com/easysoft/zagent/pkg/lib/file"
_logUtils "github.com/easysoft/zagent/pkg/lib/log"
"gorm.io/driver/sqlite"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"gorm.io/plugin/dbresolver"
"path/filepath"
"strings"
"time"

_ "gorm.io/driver/sqlite"
Expand All @@ -29,18 +30,12 @@ func GetInst() *Instance {
func InitDB(mode string) {
var dialector gorm.Dialector

if mode == "agent" {
if mode == "host" {
conn := DBFile(mode)
dialector = sqlite.Open(conn)

} else {
_logUtils.Info("not supported database adapter")
}

prefix := ""
if mode == "agent" {
prefix = agentConf.Inst.DB.Prefix
}
prefix := agentConf.Inst.DB.Prefix

DB, err := gorm.Open(dialector, &gorm.Config{
SkipDefaultTransaction: false,
Expand Down Expand Up @@ -88,8 +83,6 @@ func (i *Instance) Close() error {
}

func DBFile(mode string) string {
dbName := "agent"

path := filepath.Join(_fileUtils.GetExeDir(), strings.ToLower(dbName+".db"))
path := filepath.Join(_fileUtils.GetExeDir(), fmt.Sprintf("%s-%s.db", consts.AppName, mode))
return path
}
28 changes: 24 additions & 4 deletions pkg/lib/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ var (
TaskMap sync.Map
)

func Start(task agentModel.Task, ch chan int) (pth string, status consts.TaskStatus) {
func Start(task agentModel.Task, pth string, ch chan int) (status consts.TaskStatus, existFile string) {
fmt.Printf("Start to download %s ...\n", task.Url)

targetDir := consts.FolderDownload
targetDir := consts.DownloadDir
if task.Md5 == "" {
getMd5FromRemote(&task, targetDir)
} else {
saveMd5FromRequest(&task, targetDir)
}

pth = findSameFile(task, targetDir)
if pth != "" {
existFile = findSameFile(task, targetDir)
if existFile != "" {
status = consts.Completed
return
}
Expand Down Expand Up @@ -167,6 +169,15 @@ func getMd5FromRemote(task *agentModel.Task, dir string) (err error) {
return
}

func saveMd5FromRequest(task *agentModel.Task, dir string) (err error) {
index2 := strings.LastIndex(task.Url, "/")
md5FilePath := filepath.Join(dir, task.Url[index2:]+".md5")

_fileUtils.WriteFile(md5FilePath, task.Md5)

return
}

func findSameFile(task agentModel.Task, dir string) (pth string) {
files, _ := ioutil.ReadDir(dir)

Expand All @@ -188,3 +199,12 @@ func findSameFile(task agentModel.Task, dir string) (pth string) {

return
}

func GetPath(task agentModel.Task) (pth string) {
index := strings.LastIndex(task.Url, "/")
name := task.Url[index:]

pth = filepath.Join(consts.DownloadDir, name)

return
}
Loading

0 comments on commit 4d547be

Please sign in to comment.