fix:delete crontask
This commit is contained in:
@@ -1,145 +0,0 @@
|
|||||||
package backupEtcFromNE
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"ems.agt/lib/dborm"
|
|
||||||
"ems.agt/lib/global"
|
|
||||||
"ems.agt/lib/log"
|
|
||||||
"ems.agt/restagent/config"
|
|
||||||
"ems.agt/src/framework/cron"
|
|
||||||
)
|
|
||||||
|
|
||||||
var NewProcessor = &BarProcessor{
|
|
||||||
progress: 0,
|
|
||||||
count: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
// bar 队列任务处理
|
|
||||||
type BarProcessor struct {
|
|
||||||
// 任务进度
|
|
||||||
progress int
|
|
||||||
// 执行次数
|
|
||||||
count int
|
|
||||||
}
|
|
||||||
|
|
||||||
type BarParams struct {
|
|
||||||
Duration int `json:"duration"`
|
|
||||||
TableName string `json:"tableName"`
|
|
||||||
ColName string `json:"colName"` // column name of time string
|
|
||||||
Extras string `json:"extras"` // extras condition for where
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
|
||||||
log.Infof("execute %d,last progress: %d ", s.count, s.progress)
|
|
||||||
s.count++
|
|
||||||
|
|
||||||
options := data.(cron.JobData)
|
|
||||||
sysJob := options.SysJob
|
|
||||||
var params BarParams
|
|
||||||
|
|
||||||
err := json.Unmarshal([]byte(sysJob.TargetParams), ¶ms)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("Repeat %v Job ID %s", options.Repeat, sysJob.JobID)
|
|
||||||
|
|
||||||
var nes []dborm.NeInfo
|
|
||||||
_, err = dborm.XormGetAllNeInfo(&nes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var successfulNEs, failureNEs []string
|
|
||||||
for _, neInfo := range nes {
|
|
||||||
neTypeUpper := strings.ToUpper(neInfo.NeType)
|
|
||||||
neTypeLower := strings.ToLower(neInfo.NeType)
|
|
||||||
nePath := fmt.Sprintf("%s/etc/%s", config.GetYamlConfig().OMC.Backup, neTypeLower)
|
|
||||||
isExist, err := global.PathExists(nePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to PathExists:", err)
|
|
||||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if isExist {
|
|
||||||
err = os.RemoveAll(nePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to RemoveAll:", err)
|
|
||||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = os.MkdirAll(nePath, os.ModePerm)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to MkdirAll:", err)
|
|
||||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var scpCmd string
|
|
||||||
ipType := global.ParseIPAddr(neInfo.Ip)
|
|
||||||
if neTypeLower != "omc" {
|
|
||||||
if ipType == global.IsIPv4 {
|
|
||||||
scpCmd = fmt.Sprintf("scp -r %s@%s:%s/%s/*.yaml %s/etc/%s", config.GetYamlConfig().NE.User,
|
|
||||||
neInfo.Ip, config.GetYamlConfig().NE.EtcDir,
|
|
||||||
neTypeLower, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
|
||||||
} else {
|
|
||||||
scpCmd = fmt.Sprintf("scp -r %s@[%s]:%s/%s/*.yaml %s/etc/%s", config.GetYamlConfig().NE.User,
|
|
||||||
neInfo.Ip, config.GetYamlConfig().NE.EtcDir,
|
|
||||||
neTypeLower, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ipType == global.IsIPv4 {
|
|
||||||
scpCmd = fmt.Sprintf("scp -r %s@%s:%s/etc/*.yaml %s/etc/%s", config.GetYamlConfig().NE.User,
|
|
||||||
neInfo.Ip, config.GetYamlConfig().NE.OmcDir, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
|
||||||
} else {
|
|
||||||
scpCmd = fmt.Sprintf("scp -r %s@[%s]:%s/etc/*.yaml %s/etc/%s", config.GetYamlConfig().NE.User,
|
|
||||||
neInfo.Ip, config.GetYamlConfig().NE.OmcDir, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
zipFile := fmt.Sprintf("%s-%s-etc-%s.zip", neTypeLower, strings.ToLower(neInfo.NeId), time.Now().Format(global.DateData))
|
|
||||||
zipFilePath := config.GetYamlConfig().OMC.Backup + "/" + zipFile
|
|
||||||
zipCmd := fmt.Sprintf("cd %s/etc && zip -r %s %s/*", config.GetYamlConfig().OMC.Backup, zipFilePath, neTypeLower)
|
|
||||||
|
|
||||||
command := fmt.Sprintf("%s&&%s", scpCmd, zipCmd)
|
|
||||||
|
|
||||||
log.Trace("command:", command)
|
|
||||||
out, err := global.ExecCmd(command)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Faile to exec command:", err)
|
|
||||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Tracef("command output:%s", out)
|
|
||||||
|
|
||||||
md5Sum, err := global.GetFileMD5Sum(zipFilePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Faile to md5sum:", err)
|
|
||||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
//log.Debug("md5Str:", md5Sum)
|
|
||||||
path := config.GetYamlConfig().OMC.Backup
|
|
||||||
neBackup := dborm.NeBackup{NeType: neTypeUpper, NeId: neInfo.NeId, FileName: zipFile, Path: path, Md5Sum: md5Sum}
|
|
||||||
_, err = dborm.XormInsertTableOne("ne_backup", neBackup)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Faile to XormInsertTableOne:", err)
|
|
||||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
successfulNEs = append(successfulNEs, neInfo.NeType+"/"+neInfo.NeId)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("successfulNEs: %s failureNEs: %s", successfulNEs, failureNEs)
|
|
||||||
// result
|
|
||||||
return map[string]any{
|
|
||||||
"successfulNEs": successfulNEs,
|
|
||||||
"failureNEs": failureNEs,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
package delExpiredNeBackup
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"ems.agt/lib/dborm"
|
|
||||||
"ems.agt/lib/global"
|
|
||||||
"ems.agt/lib/log"
|
|
||||||
"ems.agt/restagent/config"
|
|
||||||
"ems.agt/src/framework/cron"
|
|
||||||
)
|
|
||||||
|
|
||||||
var NewProcessor = &BarProcessor{
|
|
||||||
progress: 0,
|
|
||||||
count: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
// bar 队列任务处理
|
|
||||||
type BarProcessor struct {
|
|
||||||
// 任务进度
|
|
||||||
progress int
|
|
||||||
// 执行次数
|
|
||||||
count int
|
|
||||||
}
|
|
||||||
|
|
||||||
type BarParams struct {
|
|
||||||
Duration int `json:"duration"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
|
||||||
log.Infof("count: %d ,progress: %d ", s.count, s.progress)
|
|
||||||
s.count++
|
|
||||||
|
|
||||||
options := data.(cron.JobData)
|
|
||||||
sysJob := options.SysJob
|
|
||||||
var params BarParams
|
|
||||||
duration := 60
|
|
||||||
|
|
||||||
err := json.Unmarshal([]byte(sysJob.TargetParams), ¶ms)
|
|
||||||
if err == nil {
|
|
||||||
duration = params.Duration
|
|
||||||
}
|
|
||||||
log.Infof("Repeat: %v JobID: %s", options.Repeat, sysJob.JobID)
|
|
||||||
|
|
||||||
// // 实现任务处理逻辑
|
|
||||||
// i := 0
|
|
||||||
// s.progress = i
|
|
||||||
// for i < 5 {
|
|
||||||
// // 获取任务进度
|
|
||||||
// progress := s.progress
|
|
||||||
// log.Infof("jonId: %s => 任务进度:%d", sysJob.JobID, progress)
|
|
||||||
// // 延迟响应
|
|
||||||
// time.Sleep(time.Second * 2)
|
|
||||||
// // 程序中途执行错误
|
|
||||||
// if i == 3 {
|
|
||||||
// // arr := [1]int{1}
|
|
||||||
// // arr[i] = 3
|
|
||||||
// // fmt.Println(arr)
|
|
||||||
// // return "i = 3"
|
|
||||||
// panic("程序中途执行错误")
|
|
||||||
// }
|
|
||||||
// i++
|
|
||||||
// // 改变任务进度
|
|
||||||
// s.progress = i
|
|
||||||
// }
|
|
||||||
where := fmt.Sprintf("NOW()>ADDDATE(`create_time`,interval %d day)", duration)
|
|
||||||
affected, err := dborm.XormDeleteDataByWhere(where, "ne_backup")
|
|
||||||
if err != nil {
|
|
||||||
// panic(fmt.Sprintf("Failed to XormDeleteDataByWhere:%v", err))
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete expired files in backup directory
|
|
||||||
// todo ...
|
|
||||||
// command := fmt.Sprintf("find . -name '*.zip' -mtime +%d -type f -print | xargs rm -rf", duration)
|
|
||||||
|
|
||||||
command := fmt.Sprintf("%s/rmexpfiles.sh %s %d", config.GetYamlConfig().OMC.BinDir, config.GetYamlConfig().OMC.Backup, duration)
|
|
||||||
|
|
||||||
log.Trace("command:", command)
|
|
||||||
out, err := global.ExecCmd(command)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Faile to exec command:", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
log.Tracef("command output:%s", out)
|
|
||||||
|
|
||||||
// 返回结果,用于记录执行结果
|
|
||||||
return map[string]any{
|
|
||||||
"msg": "success",
|
|
||||||
"cmdoutput": string(out),
|
|
||||||
"affected": affected,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user