优化订阅自动添加定时任务逻辑

添加删除依赖的功能
node依赖采用pnpm管理
This commit is contained in:
huoxue1 2023-02-05 12:25:36 +08:00
parent 054097421d
commit 59073c434c
7 changed files with 88 additions and 39 deletions

View File

@ -41,12 +41,12 @@ RUN set -x \
&& git config --global user.email "qinglong@@users.noreply.github.com" \
&& git config --global user.name "qinglong" \
&& git config --global http.postBuffer 524288000 \
&& npm install -g yarn \
&& npm install -g pnpm \
&& rm -rf /root/.cache \
&& rm -rf /root/.npm \
&& mkdir -p ${QL_DIR}/data \
&& cd ${QL_DIR} \
&& wget https://github.com/huoxue1/qinglong/releases/download/v1.0.0/static.tar.gz \
&& wget https://github.com/huoxue1/qinglong/releases/download/v1.0.1/static.tar.gz \
&& tar -xzvf static.tar.gz
COPY ./dist/docker_linux_$TARGETARCH*/qinglong-go ${QL_DIR}/ql

View File

@ -12,6 +12,8 @@ func Api(group *gin.RouterGroup) {
group.POST("", post())
group.GET("", get())
group.GET("/:id", getDep())
group.DELETE("", del())
}
var (
@ -34,6 +36,20 @@ func get() gin.HandlerFunc {
}
}
func del() gin.HandlerFunc {
return func(ctx *gin.Context) {
var ids []int
err := ctx.ShouldBindJSON(&ids)
if err != nil {
ctx.JSON(503, res.Err(502, err))
return
}
dependencies.DelDep(ids)
ctx.JSON(200, res.Ok(true))
}
}
func post() gin.HandlerFunc {
return func(ctx *gin.Context) {
var deps []*models.Dependences

View File

@ -28,12 +28,13 @@ type Crontabs struct {
func QueryCron(page int, size int, searchValue string, orderField string, orderType string) ([]*Crontabs, error) {
crontabs := make([]*Crontabs, 0)
session := engine.Table(new(Crontabs)).Limit(size, (page-1)*size).Where(builder.Like{"name", "%" + searchValue + "%"}.Or(builder.Like{"command", "%" + searchValue + "%"}))
if orderType == "DESC" {
session.Desc(orderField)
} else if orderType == "ASC" {
session.Asc(orderField)
}
err := session.Find(&crontabs)
//if orderType == "DESC" {
// session.Desc(orderField)
//} else if orderType == "ASC" {
// session.Asc(orderField)
//}
err := session.Asc("status").Find(&crontabs)
return crontabs, err
}

View File

@ -21,9 +21,9 @@ func runYarn() {
return
}
ch := make(chan int, 1)
utils.RunTask(context.WithValue(context.Background(), "cancel", ch), "yarn install", map[string]string{}, func(ctx context.Context) {
log.Infoln("开始执行yarn初始化!")
utils.RunTask(context.WithValue(context.Background(), "cancel", ch), "pnpm install", map[string]string{}, func(ctx context.Context) {
log.Infoln("开始执行pnpm初始化!")
}, func(ctx context.Context) {
log.Infoln("yarn初始化执行完成!")
log.Infoln("pnpm初始化执行完成!")
}, os.Stdout)
}

View File

@ -8,6 +8,7 @@ import (
"github.com/huoxue1/qinglong-go/service/config"
"github.com/huoxue1/qinglong-go/utils"
"io"
"os"
"strings"
"time"
)
@ -22,12 +23,45 @@ func AddDep(dep *models.Dependences) {
}
}
func DelDep(ids []int) {
for _, id := range ids {
dep, err := models.GetDependences(id)
if err != nil {
continue
}
if dep.Type == models.NODE {
unInstallDep("pnpm remove "+dep.Name, dep)
} else if dep.Type == models.PYTHON {
pip := config.GetKey("PipCmd", "pip")
unInstallDep(fmt.Sprintf("%s uninstall %s", pip, dep.Name), dep)
} else {
unInstallDep("apk uninstall "+dep.Name, dep)
}
}
}
func unInstallDep(command string, dep *models.Dependences) {
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
go utils.RunTask(ctx, command, map[string]string{}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
_, _ = writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
_, _ = writer.Write([]byte(fmt.Sprintf("\n##执行结束.. %s耗时%.1f秒\n\n", time.Now().Format("2006-01-02 15:04:05"), time.Now().Sub(now).Seconds())))
_ = models.DeleteDependences(dep.Id)
}, os.Stdout)
}
func addNodeDep(dep *models.Dependences) {
log := ""
buffer := bytes.NewBufferString(log)
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
utils.RunTask(ctx, fmt.Sprintf("yarn add %s", dep.Name), map[string]string{}, func(ctx context.Context) {
go utils.RunTask(ctx, fmt.Sprintf("pnpm add %s", dep.Name), map[string]string{}, func(ctx context.Context) {
dep.Status = 0
_, _ = models.AddDependences(dep)
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
@ -39,7 +73,7 @@ func addNodeDep(dep *models.Dependences) {
logs = append(logs, i2+"\n\n")
}
dep.Log = logs
models.AddDependences(dep)
_ = models.UpdateDependences(dep)
}, buffer)
}
@ -49,7 +83,9 @@ func addPythonDep(dep *models.Dependences) {
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
pip := config.GetKey("PipCmd", "pip")
utils.RunTask(ctx, fmt.Sprintf(pip+" install %s", dep.Name), map[string]string{}, func(ctx context.Context) {
go utils.RunTask(ctx, fmt.Sprintf(pip+" install %s", dep.Name), map[string]string{}, func(ctx context.Context) {
dep.Status = 0
_, _ = models.AddDependences(dep)
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
@ -61,7 +97,7 @@ func addPythonDep(dep *models.Dependences) {
logs = append(logs, i2+"\n\n")
}
dep.Log = logs
models.AddDependences(dep)
models.UpdateDependences(dep)
}, buffer)
}
@ -70,7 +106,9 @@ func addLinuxDep(dep *models.Dependences) {
buffer := bytes.NewBufferString(log)
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
utils.RunTask(ctx, fmt.Sprintf("apk add %s", dep.Name), map[string]string{}, func(ctx context.Context) {
go utils.RunTask(ctx, fmt.Sprintf("apk add %s", dep.Name), map[string]string{}, func(ctx context.Context) {
dep.Status = 0
_, _ = models.AddDependences(dep)
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
@ -82,6 +120,6 @@ func addLinuxDep(dep *models.Dependences) {
logs = append(logs, i2+"\n\n")
}
dep.Log = logs
models.AddDependences(dep)
models.UpdateDependences(dep)
}, buffer)
}

View File

@ -277,17 +277,19 @@ func getSubCron(filePath string) (name string, cron string, err error) {
}
cronReg := regexp.MustCompile(`([0-9\-*/,]{1,} ){4,5}([0-9\-*/,]){1,}`)
nameEnv := regexp.MustCompile(`new\sEnv\(['|"](.*?)['|"]\)`)
if cronReg.Match(data) {
cron = string(cronReg.FindAll(data, 1)[0])
cron = strings.TrimPrefix(cron, "//")
if nameEnv.Match(data) {
name = string(nameEnv.FindAllSubmatch(data, 1)[0][1])
if cronReg.Match(data) {
cron = strings.TrimPrefix(strings.TrimPrefix(string(cronReg.FindAll(data, 1)[0]), "//"), " ")
} else {
name = path.Base(filePath)
key := config.GetKey("DefaultCronRule", "0 9 * * *")
if key == "" {
key = "0 9 * * *"
}
cron = key
}
name = string(nameEnv.FindAllSubmatch(data, 1)[0][1])
return
} else {
return "", "", errors.New("not found cron")
}
cron = strings.TrimPrefix(cron, " ")
return
}

View File

@ -4,13 +4,11 @@ import (
"errors"
"github.com/huoxue1/qinglong-go/utils/log"
"github.com/robfig/cron/v3"
"strings"
"sync"
)
var (
manager sync.Map
defaultCron *cron.Cron
SixCron *cron.Cron
)
@ -20,10 +18,8 @@ type mapValue struct {
}
func init() {
defaultCron = cron.New(cron.WithChain(cron.Recover(&log.CronLog{})))
SixCron = cron.New(cron.WithChain(cron.Recover(&log.CronLog{})), cron.WithParser(
cron.NewParser(cron.Second|cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow|cron.Descriptor)))
defaultCron.Start()
cron.NewParser(cron.SecondOptional|cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow|cron.Descriptor)))
SixCron.Start()
}
@ -31,16 +27,12 @@ func AddCron(id string, value string, task func()) error {
if value == "7 7 7 7 7" {
value = "7 7 7 7 6"
}
crons := strings.Split(value, " ")
cronCmd := defaultCron
if len(crons) == 6 {
cronCmd = SixCron
}
en, err := cronCmd.AddFunc(value, task)
en, err := SixCron.AddFunc(value, task)
if err != nil {
return err
}
manager.Store(id, &mapValue{en, cronCmd})
manager.Store(id, &mapValue{en, SixCron})
return nil
}