crontab.go 5.4 KB
package crontab

import (
	"context"
	"fmt"
	"github.com/beego/beego/v2/task"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"github.com/linmadan/egglib-go/utils/xtime"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
	"io/fs"
	"os"
	"path/filepath"
	"time"
)

type CrontabService struct {
}

func NewCrontabService(options map[string]interface{}) *CrontabService {
	newCrontabService := &CrontabService{}
	return newCrontabService
}

func (crontabService *CrontabService) initTask() {
	autoRemoveExpiredTemporaryFile := task.NewTask("定时清理过期临时文件记录", "0 10 */1 * * *", AutoRemoveExpiredTemporaryFile)
	task.AddTask("autoRemoveExpiredTemporaryFile", autoRemoveExpiredTemporaryFile)

	autoRemovePublicDownloadFile := task.NewTask("定时清理缓存文件", "0 20 */1 * * *", AutoRemovePublicDownloadFile)
	task.AddTask("autoRemovePublicDownloadFile", autoRemovePublicDownloadFile)

	autoRemoveTemporaryTable := task.NewTask("定时清理临时表", "0 57 */1 * * *", AutoRemoveTemporaryTable)
	task.AddTask("autoRemoveTemporaryTable", autoRemoveTemporaryTable)
}

func (crontabService *CrontabService) StartCrontabTask() {
	crontabService.initTask()
	task.StartTask()
	log.Logger.Info("crontab start!")
}

func (crontabService *CrontabService) StopCrontabTask() {
	task.StopTask()
	log.Logger.Info("crontab stop!")
}

func AutoRemoveExpiredTemporaryFile(ctx context.Context) error {

	defer func() {
		if r := recover(); r != nil {
			log.Logger.Error(fmt.Sprintf("%v", r), map[string]interface{}{"task": "定时清理过期临时文件记录"})
		}
	}()
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		if err != nil {
			log.Logger.Error("【定时清理过期临时文件记录】 失败:" + err.Error())
		}
		transactionContext.RollbackTransaction()
	}()

	log.Logger.Debug("【定时清理过期临时文件记录】 启动")
	end := xtime.New(time.Now()).BeginningOfDay().Add(-time.Hour * 6)
	begin := end.AddDate(0, 0, -7)
	fileRepository, _, _ := factory.FastPgFile(transactionContext, 0)
	_, files, err := fileRepository.Find(map[string]interface{}{"limit": 100, "fileType": domain.TemporaryFile.ToString(), "updatedAtBegin": begin, "updatedAtEnd": end})
	if err != nil {
		return err
	}
	deleteFileService, _ := factory.CreateDeleteFileService(transactionContext)
	if err = deleteFileService.DeleteFiles(&domain.Context{
		OperatorName: "系统",
	}, files...); err != nil {
		return err
	}

	if err = transactionContext.CommitTransaction(); err != nil {
		return err
	}
	return nil
}

func AutoRemovePublicDownloadFile(ctx context.Context) error {
	var err error
	defer func() {
		if r := recover(); r != nil {
			log.Logger.Error(fmt.Sprintf("%v", r), map[string]interface{}{"task": "定时清理缓存文件"})
		}
	}()
	defer func() {
		if err != nil {
			log.Logger.Error("【定时清理缓存文件】 失败:" + err.Error())
		}
	}()
	log.Logger.Debug("【定时清理缓存文件】 启动")
	root := "public"
	end := xtime.New(time.Now()).BeginningOfDay().AddDate(0, 0, -1)
	err = filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
		t := info.ModTime()
		if !info.IsDir() && xtime.Before(t, end) {
			err = os.Remove(path)
			if err != nil {
				return err
			}
			log.Logger.Info(fmt.Sprintf("[%v] 删除文件:%v 大小:%v 路径:%v 文件创建时间:%v", "系统", info.Name(), info.Size(), path, info.ModTime()))
			return nil
		}
		return nil
	})
	if err != nil {
		return err
	}
	return nil
}

func AutoRemoveTemporaryTable(ctx context.Context) error {
	defer func() {
		if r := recover(); r != nil {
			log.Logger.Error(fmt.Sprintf("%v", r), map[string]interface{}{"task": "定时清理过期临时文件记录"})
		}
	}()
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		if err != nil {
			log.Logger.Error("【定时清理临时表】 失败:" + err.Error())
		}
		transactionContext.RollbackTransaction()
	}()

	log.Logger.Debug("【定时清理临时表】 启动")
	end := xtime.New(time.Now()).BeginningOfDay().Add(-time.Hour * 12)
	begin := end.AddDate(0, 0, -7)

	tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
	_, tables, err := tableRepository.Find(map[string]interface{}{"beginTime": begin, "endTime": end, "tableTypes": []string{domain.TemporaryTable.ToString()}})
	if err != nil {
		return nil
	}
	for i, t := range tables {
		if err = dao.TableDelete(transactionContext.(*pgTransaction.TransactionContext), t.TableId, domain.TemporaryTable); err != nil {
			log.Logger.Error(err.Error())
			return nil
		}
		log.Logger.Info(fmt.Sprintf("序号:%d 清理临时表 %v", i, t.SQLName))
		if err = starrocks.DropView(starrocks.DB, t.SQLName); err != nil {
			log.Logger.Error(err.Error())
			return nil
		}
	}
	if err = transactionContext.CommitTransaction(); err != nil {
		return err
	}
	return nil
}