mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	refactor: 新增base.Repo与base.App,重构repo与app层代码
This commit is contained in:
		@@ -3,30 +3,33 @@ package application
 | 
			
		||||
import (
 | 
			
		||||
	"mayfly-go/internal/machine/domain/entity"
 | 
			
		||||
	"mayfly-go/internal/machine/domain/repository"
 | 
			
		||||
	"mayfly-go/pkg/base"
 | 
			
		||||
	"mayfly-go/pkg/biz"
 | 
			
		||||
	"mayfly-go/pkg/errorx"
 | 
			
		||||
	"mayfly-go/pkg/logx"
 | 
			
		||||
	"mayfly-go/pkg/model"
 | 
			
		||||
	"mayfly-go/pkg/rediscli"
 | 
			
		||||
	"mayfly-go/pkg/scheduler"
 | 
			
		||||
	"mayfly-go/pkg/utils/anyx"
 | 
			
		||||
	"mayfly-go/pkg/utils/collx"
 | 
			
		||||
	"mayfly-go/pkg/utils/stringx"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type MachineCronJob interface {
 | 
			
		||||
	base.App[*entity.MachineCronJob]
 | 
			
		||||
 | 
			
		||||
	// 分页获取机器任务列表信息
 | 
			
		||||
	GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
 | 
			
		||||
	GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
 | 
			
		||||
 | 
			
		||||
	// 获取分页执行结果列表
 | 
			
		||||
	GetExecPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
 | 
			
		||||
	GetExecPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
 | 
			
		||||
 | 
			
		||||
	// 根据id获取
 | 
			
		||||
	GetById(id uint64, cols ...string) *entity.MachineCronJob
 | 
			
		||||
 | 
			
		||||
	Save(entity *entity.MachineCronJob) uint64
 | 
			
		||||
	Save(entity *entity.MachineCronJob) (uint64, error)
 | 
			
		||||
 | 
			
		||||
	Delete(id uint64)
 | 
			
		||||
 | 
			
		||||
	// 获取管理的机器列表id
 | 
			
		||||
	// 获取计划任务关联的机器列表id
 | 
			
		||||
	GetRelateMachineIds(cronJobId uint64) []uint64
 | 
			
		||||
 | 
			
		||||
	// 获取机器关联的计划任务列表
 | 
			
		||||
@@ -43,7 +46,8 @@ type MachineCronJob interface {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type machineCropJobAppImpl struct {
 | 
			
		||||
	machineCropJobRepo       repository.MachineCronJob
 | 
			
		||||
	base.AppImpl[*entity.MachineCronJob, repository.MachineCronJob]
 | 
			
		||||
 | 
			
		||||
	machineCropJobRelateRepo repository.MachineCronJobRelate
 | 
			
		||||
	machineCropJobExecRepo   repository.MachineCronJobExec
 | 
			
		||||
	machineApp               Machine
 | 
			
		||||
@@ -55,48 +59,50 @@ func newMachineCronJobApp(
 | 
			
		||||
	machineCropJobExecRepo repository.MachineCronJobExec,
 | 
			
		||||
	machineApp Machine,
 | 
			
		||||
) MachineCronJob {
 | 
			
		||||
	return &machineCropJobAppImpl{
 | 
			
		||||
		machineCropJobRepo:       machineCropJobRepo,
 | 
			
		||||
	app := &machineCropJobAppImpl{
 | 
			
		||||
		machineCropJobRelateRepo: machineCropJobRelateRepo,
 | 
			
		||||
		machineCropJobExecRepo:   machineCropJobExecRepo,
 | 
			
		||||
		machineApp:               machineApp,
 | 
			
		||||
	}
 | 
			
		||||
	app.Repo = machineCropJobRepo
 | 
			
		||||
	return app
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 分页获取机器脚本任务列表
 | 
			
		||||
func (m *machineCropJobAppImpl) GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
 | 
			
		||||
	return m.machineCropJobRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
 | 
			
		||||
func (m *machineCropJobAppImpl) GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
 | 
			
		||||
	return m.GetRepo().GetPageList(condition, pageParam, toEntity, orderBy...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 获取分页执行结果列表
 | 
			
		||||
func (m *machineCropJobAppImpl) GetExecPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
 | 
			
		||||
func (m *machineCropJobAppImpl) GetExecPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
 | 
			
		||||
	return m.machineCropJobExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 根据id获取
 | 
			
		||||
func (m *machineCropJobAppImpl) GetById(id uint64, cols ...string) *entity.MachineCronJob {
 | 
			
		||||
	return m.machineCropJobRepo.GetById(id, cols...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 保存机器任务信息
 | 
			
		||||
func (m *machineCropJobAppImpl) Save(mcj *entity.MachineCronJob) uint64 {
 | 
			
		||||
func (m *machineCropJobAppImpl) Save(mcj *entity.MachineCronJob) (uint64, error) {
 | 
			
		||||
	// 更新操作
 | 
			
		||||
	if mcj.Id != 0 {
 | 
			
		||||
		m.machineCropJobRepo.UpdateById(mcj)
 | 
			
		||||
		m.UpdateById(mcj)
 | 
			
		||||
		cj, err := m.GetById(new(entity.MachineCronJob), mcj.Id)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return 0, errorx.NewBiz("该任务不存在")
 | 
			
		||||
		}
 | 
			
		||||
		// 处理最新的计划任务
 | 
			
		||||
		m.addCronJob(m.GetById(mcj.Id))
 | 
			
		||||
		return mcj.Id
 | 
			
		||||
		m.addCronJob(cj)
 | 
			
		||||
		return mcj.Id, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	m.addCronJob(mcj)
 | 
			
		||||
	m.machineCropJobRepo.Insert(mcj)
 | 
			
		||||
	return mcj.Id
 | 
			
		||||
	if err := m.Insert(mcj); err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
	return mcj.Id, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *machineCropJobAppImpl) Delete(id uint64) {
 | 
			
		||||
	m.machineCropJobRepo.Delete(id)
 | 
			
		||||
	m.machineCropJobExecRepo.Delete(&entity.MachineCronJobExec{CronJobId: id})
 | 
			
		||||
	m.machineCropJobRelateRepo.Delete(&entity.MachineCronJobRelate{CronJobId: id})
 | 
			
		||||
	m.DeleteById(id)
 | 
			
		||||
	m.machineCropJobExecRepo.DeleteByCond(&entity.MachineCronJobExec{CronJobId: id})
 | 
			
		||||
	m.machineCropJobRelateRepo.DeleteByCond(&entity.MachineCronJobRelate{CronJobId: id})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *machineCropJobAppImpl) GetRelateMachineIds(cronJobId uint64) []uint64 {
 | 
			
		||||
@@ -125,7 +131,7 @@ func (m *machineCropJobAppImpl) CronJobRelateMachines(cronJobId uint64, machineI
 | 
			
		||||
	m.machineCropJobRelateRepo.BatchInsert(addVals)
 | 
			
		||||
 | 
			
		||||
	for _, delId := range delIds {
 | 
			
		||||
		m.machineCropJobRelateRepo.Delete(&entity.MachineCronJobRelate{CronJobId: cronJobId, MachineId: delId})
 | 
			
		||||
		m.machineCropJobRelateRepo.DeleteByCond(&entity.MachineCronJobRelate{CronJobId: cronJobId, MachineId: delId})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -147,14 +153,14 @@ func (m *machineCropJobAppImpl) MachineRelateCronJobs(machineId uint64, cronJobs
 | 
			
		||||
	m.machineCropJobRelateRepo.BatchInsert(addVals)
 | 
			
		||||
 | 
			
		||||
	for _, delId := range delIds {
 | 
			
		||||
		m.machineCropJobRelateRepo.Delete(&entity.MachineCronJobRelate{CronJobId: delId, MachineId: machineId})
 | 
			
		||||
		m.machineCropJobRelateRepo.DeleteByCond(&entity.MachineCronJobRelate{CronJobId: delId, MachineId: machineId})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *machineCropJobAppImpl) InitCronJob() {
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if err := recover(); err != nil {
 | 
			
		||||
			logx.Errorf("机器计划任务初始化失败: %s", err.(error).Error())
 | 
			
		||||
			logx.ErrorTrace("机器计划任务初始化失败: %s", err.(error))
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
@@ -166,7 +172,7 @@ func (m *machineCropJobAppImpl) InitCronJob() {
 | 
			
		||||
	cond.Status = entity.MachineCronJobStatusEnable
 | 
			
		||||
	mcjs := new([]entity.MachineCronJob)
 | 
			
		||||
 | 
			
		||||
	pr := m.GetPageList(cond, pageParam, mcjs)
 | 
			
		||||
	pr, _ := m.GetPageList(cond, pageParam, mcjs)
 | 
			
		||||
	total := pr.Total
 | 
			
		||||
	add := 0
 | 
			
		||||
 | 
			
		||||
@@ -218,7 +224,7 @@ func (m *machineCropJobAppImpl) runCronJob(key string) {
 | 
			
		||||
 | 
			
		||||
	cronJob := new(entity.MachineCronJob)
 | 
			
		||||
	cronJob.Key = key
 | 
			
		||||
	err := m.machineCropJobRepo.GetBy(cronJob)
 | 
			
		||||
	err := m.GetBy(cronJob)
 | 
			
		||||
	// 不存在或禁用,则移除该任务
 | 
			
		||||
	if err != nil || cronJob.Status == entity.MachineCronJobStatusDisable {
 | 
			
		||||
		scheduler.RemoveByKey(key)
 | 
			
		||||
@@ -233,7 +239,7 @@ func (m *machineCropJobAppImpl) runCronJob(key string) {
 | 
			
		||||
func (m *machineCropJobAppImpl) runCronJob0(mid uint64, cronJob *entity.MachineCronJob) {
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if err := recover(); err != nil {
 | 
			
		||||
			res := err.(error).Error()
 | 
			
		||||
			res := anyx.ToString(err)
 | 
			
		||||
			m.machineCropJobExecRepo.Insert(&entity.MachineCronJobExec{
 | 
			
		||||
				MachineId: mid,
 | 
			
		||||
				CronJobId: cronJob.Id,
 | 
			
		||||
@@ -245,7 +251,9 @@ func (m *machineCropJobAppImpl) runCronJob0(mid uint64, cronJob *entity.MachineC
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	res, err := m.machineApp.GetCli(uint64(mid)).Run(cronJob.Script)
 | 
			
		||||
	machineCli, err := m.machineApp.GetCli(uint64(mid))
 | 
			
		||||
	biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
 | 
			
		||||
	res, err := machineCli.Run(cronJob.Script)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if res == "" {
 | 
			
		||||
			res = err.Error()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user