Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions agent/app/api/v2/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
// @Tags Cronjob
// @Summary Create cronjob
// @Accept json
// @Param request body dto.CronjobCreate true "request"
// @Param request body dto.CronjobOperate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /cronjobs [post]
// @x-panel-log {"bodyKeys":["type","name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"ๅˆ›ๅปบ่ฎกๅˆ’ไปปๅŠก [type][name]","formatEN":"create cronjob [type][name]"}
func (b *BaseApi) CreateCronjob(c *gin.Context) {
var req dto.CronjobCreate
var req dto.CronjobOperate
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
Expand All @@ -31,6 +31,28 @@ func (b *BaseApi) CreateCronjob(c *gin.Context) {
helper.SuccessWithOutData(c)
}

// @Tags Cronjob
// @Summary Load cronjob info
// @Accept json
// @Param request body dto.OperateByID true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /cronjobs/load/info [post]
func (b *BaseApi) LoadCronjobInfo(c *gin.Context) {
var req dto.OperateByID
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}

data, err := cronjobService.LoadInfo(req)
if err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithData(c, data)
}

// @Tags Cronjob
// @Summary Load cronjob spec time
// @Accept json
Expand Down Expand Up @@ -174,14 +196,14 @@ func (b *BaseApi) DeleteCronjob(c *gin.Context) {
// @Tags Cronjob
// @Summary Update cronjob
// @Accept json
// @Param request body dto.CronjobUpdate true "request"
// @Param request body dto.CronjobOperate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /cronjobs/update [post]
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"ๆ›ดๆ–ฐ่ฎกๅˆ’ไปปๅŠก [name]","formatEN":"update cronjob [name]"}
func (b *BaseApi) UpdateCronjob(c *gin.Context) {
var req dto.CronjobUpdate
var req dto.CronjobOperate
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
Expand Down
37 changes: 2 additions & 35 deletions agent/app/dto/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type CronjobSpec struct {
Spec string `json:"spec" validate:"required"`
}

type CronjobCreate struct {
type CronjobOperate struct {
ID uint `json:"id"`
Name string `json:"name" validate:"required"`
Type string `json:"type" validate:"required"`
SpecCustom bool `json:"specCustom"`
Expand Down Expand Up @@ -48,40 +49,6 @@ type CronjobCreate struct {
AlertTitle string `json:"alertTitle"`
}

type CronjobUpdate struct {
ID uint `json:"id" validate:"required"`
Type string `json:"type" validate:"required"`
Name string `json:"name" validate:"required"`
SpecCustom bool `json:"specCustom"`
Spec string `json:"spec" validate:"required"`

Executor string `json:"executor"`
ScriptMode string `json:"scriptMode"`
Script string `json:"script"`
Command string `json:"command"`
ContainerName string `json:"containerName"`
User string `json:"user"`

AppID string `json:"appID"`
Website string `json:"website"`
ExclusionRules string `json:"exclusionRules"`
DBType string `json:"dbType"`
DBName string `json:"dbName"`
URL string `json:"url"`
IsDir bool `json:"isDir"`
SourceDir string `json:"sourceDir"`

SourceAccountIDs string `json:"sourceAccountIDs"`
DownloadAccountID uint `json:"downloadAccountID"`
RetainCopies int `json:"retainCopies" validate:"number,min=1"`
RetryTimes int `json:"retryTimes" validate:"number,min=0"`
Timeout uint `json:"timeout" validate:"number,min=1"`
Secret string `json:"secret"`

AlertCount uint `json:"alertCount"`
AlertTitle string `json:"alertTitle"`
}

type CronjobUpdateStatus struct {
ID uint `json:"id" validate:"required"`
Status string `json:"status" validate:"required"`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes include:

  1. Type Renaming: The struct CronjobCreate has been renamed to CronjobOperate. This is beneficial because it clearly indicates that this struct is used for operations rather than creating a new cron job.

  2. Addition of Missing Fields:

    • Added ID, which can be useful for identifying specific cron jobs when performing updates.
    • Added fields like Command, ContainerName, User, etc., based on the previous structs for clarity and completeness. These should be validated appropriately according to their types.
  3. Optimization Suggestions:

    • Consistency in Field Names: Ensure consistency between field names across all related structs (CronjobCreate, CronjobUpdate) if applicable, such as using common prefixes like App_, Website_, etc., instead of separate fields.
    • Validation Annotations: While validation annotations help maintain data integrity, ensure they do not conflict with each other or are overly restrictive (e.g., making fields required unnecessarily).
    • Comments: Add comments where necessary to explain complex logic or sections of code, especially important in areas involving script/custom execution paths.

Overall, these changes enhance readability and functionality while providing a clear structure for handling various operations related to cron jobs.

Expand Down
30 changes: 26 additions & 4 deletions agent/app/service/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ type CronjobService struct{}
type ICronjobService interface {
SearchWithPage(search dto.PageCronjob) (int64, interface{}, error)
SearchRecords(search dto.SearchRecord) (int64, interface{}, error)
Create(cronjobDto dto.CronjobCreate) error
Create(cronjobDto dto.CronjobOperate) error
LoadNextHandle(spec string) ([]string, error)
HandleOnce(id uint) error
Update(id uint, req dto.CronjobUpdate) error
Update(id uint, req dto.CronjobOperate) error
UpdateStatus(id uint, status string) error
Delete(req dto.CronjobBatchDelete) error
Download(down dto.CronjobDownload) (string, error)
StartJob(cronjob *model.Cronjob, isUpdate bool) (string, error)
CleanRecord(req dto.CronjobClean) error

LoadInfo(req dto.OperateByID) (*dto.CronjobOperate, error)
LoadRecordLog(req dto.OperateByID) string
}

Expand Down Expand Up @@ -75,6 +76,25 @@ func (u *CronjobService) SearchWithPage(search dto.PageCronjob) (int64, interfac
return total, dtoCronjobs, err
}

func (u *CronjobService) LoadInfo(req dto.OperateByID) (*dto.CronjobOperate, error) {
cronjob, err := cronjobRepo.Get(repo.WithByID(req.ID))
var item dto.CronjobOperate
if err := copier.Copy(&item, &cronjob); err != nil {
return nil, buserr.WithDetail("ErrStructTransform", err.Error(), nil)
}
alertBase := dto.AlertBase{
AlertType: cronjob.Type,
EntryID: cronjob.ID,
}
alertCount := xpack.GetAlert(alertBase)
if alertCount != 0 {
item.AlertCount = alertCount
} else {
item.AlertCount = 0
}
return &item, err
}

func (u *CronjobService) SearchRecords(search dto.SearchRecord) (int64, interface{}, error) {
total, records, err := cronjobRepo.PageRecords(
search.Page,
Expand Down Expand Up @@ -216,7 +236,7 @@ func (u *CronjobService) HandleOnce(id uint) error {
return nil
}

func (u *CronjobService) Create(req dto.CronjobCreate) error {
func (u *CronjobService) Create(req dto.CronjobOperate) error {
cronjob, _ := cronjobRepo.Get(repo.WithByName(req.Name))
if cronjob.ID != 0 {
return buserr.New("ErrRecordExist")
Expand Down Expand Up @@ -306,7 +326,7 @@ func (u *CronjobService) Delete(req dto.CronjobBatchDelete) error {
return nil
}

func (u *CronjobService) Update(id uint, req dto.CronjobUpdate) error {
func (u *CronjobService) Update(id uint, req dto.CronjobOperate) error {
var cronjob model.Cronjob
if err := copier.Copy(&cronjob, &req); err != nil {
return buserr.WithDetail("ErrStructTransform", err.Error(), nil)
Expand Down Expand Up @@ -353,6 +373,8 @@ func (u *CronjobService) Update(id uint, req dto.CronjobUpdate) error {
upMap["source_account_ids"] = req.SourceAccountIDs
upMap["download_account_id"] = req.DownloadAccountID
upMap["retain_copies"] = req.RetainCopies
upMap["retry_times"] = req.RetryTimes
upMap["timeout"] = req.Timeout
upMap["secret"] = req.Secret
err = cronjobRepo.Update(id, upMap)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions agent/router/ro_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func (s *CronjobRouter) InitRouter(Router *gin.RouterGroup) {
{
cmdRouter.POST("", baseApi.CreateCronjob)
cmdRouter.POST("/next", baseApi.LoadNextHandle)
cmdRouter.POST("/load/info", baseApi.LoadCronjobInfo)
cmdRouter.POST("/del", baseApi.DeleteCronjob)
cmdRouter.POST("/update", baseApi.UpdateCronjob)
cmdRouter.POST("/status", baseApi.UpdateCronjobStatus)
Expand Down
6 changes: 3 additions & 3 deletions agent/utils/cmd/cmdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
cmd.Dir = c.workDir
}

if err := cmd.Start(); err != nil {
return "", err
}
if c.timeout != 0 {
if err := cmd.Start(); err != nil {
return "", err
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
Expand Down
6 changes: 3 additions & 3 deletions core/utils/cmd/cmdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
cmd.Dir = c.workDir
}

if err := cmd.Start(); err != nil {
return "", err
}
if c.timeout != 0 {
if err := cmd.Start(); err != nil {
return "", err
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
Expand Down
42 changes: 22 additions & 20 deletions frontend/src/api/interface/cronjob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export namespace Cronjob {
sourceAccountItems: Array<number>;

retainCopies: number;
retryTimes: number;
timeout: number;
timeoutItem: number;
timeoutUint: string;
status: string;
secret: string;
hasAlert: boolean;
Expand All @@ -45,25 +49,41 @@ export namespace Cronjob {
export interface Item {
val: string;
}
export interface CronjobCreate {
export interface CronjobOperate {
id: number;
name: string;
type: string;
specCustom: boolean;
spec: string;
specs: Array<string>;
specObjs: Array<SpecObj>;

script: string;
appID: string;
website: string;
exclusionRules: string;
dbType: string;
dbName: string;
url: string;
isDir: boolean;
sourceDir: string;

//shell
executor: string;
scriptMode: string;
script: string;
command: string;
containerName: string;
user: string;

sourceAccountIDs: string;
downloadAccountID: number;
retainCopies: number;
retryTimes: number;
timeout: number;
secret: string;

alertCount: number;
alertTitle: string;
}
export interface SpecObj {
specType: string;
Expand All @@ -73,24 +93,6 @@ export namespace Cronjob {
minute: number;
second: number;
}
export interface CronjobUpdate {
id: number;
specCustom: boolean;
spec: string;

script: string;
website: string;
exclusionRules: string;
dbType: string;
dbName: string;
url: string;
sourceDir: string;

sourceAccountIDs: string;
downloadAccountID: number;
retainCopies: number;
secret: string;
}
export interface CronjobDelete {
ids: Array<number>;
cleanData: boolean;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several issues with this code:

  1. The addition of new fields (retryTimes, timeout, etc.) to both Item and CronjobCreate/CronjobOperate interfaces without updating corresponding types where these fields should be used can lead to compile errors when using the updated interfaces.

  2. The id field is duplicated in some interfaces (Cronjob, CronjobCreat, CronjobOperate). This inconsistency might cause confusion or bugs if not handled properly.

  3. Missing documentation or comments on the newly added fields can make it harder to understand their purpose and usage.

  4. There seems to be an indentation issue near "website" under SourceItems.

To resolve these issues:

  • Ensure all references to 'item.id' use the correct ID field from appropriate contexts.
  • Remove duplicate 'id' fields across different interfaces.
  • Add necessary comments above each new field explaining its purpose and functionality.
  • Correct the indentation around "website" under "SourceAccountItems".

Optimization suggestions could include checking whether sourceAccountIDs and downloadAccountID support more complex data structures and provide functions for easily manipulating them instead of relying solely on string representations. Additionally, considering adding error handling for null/undefined value checks before operations that rely on them would enhance robustness.

Expand Down
10 changes: 7 additions & 3 deletions frontend/src/api/modules/cronjob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ export const loadNextHandle = (spec: string) => {
return http.post<Array<String>>(`/cronjobs/next`, { spec: spec });
};

export const loadCronjobInfo = (id: number) => {
return http.post<Cronjob.CronjobOperate>(`/cronjobs/load/info`, { id: id });
};

export const getRecordLog = (id: number) => {
return http.post<string>(`/cronjobs/records/log`, { id: id });
};

export const addCronjob = (params: Cronjob.CronjobCreate) => {
return http.post<Cronjob.CronjobCreate>(`/cronjobs`, params);
export const addCronjob = (params: Cronjob.CronjobOperate) => {
return http.post<Cronjob.CronjobOperate>(`/cronjobs`, params);
};

export const editCronjob = (params: Cronjob.CronjobUpdate) => {
export const editCronjob = (params: Cronjob.CronjobOperate) => {
return http.post(`/cronjobs/update`, params);
};

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/lang/modules/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,7 @@ const message = {
ntp_helper: 'You can configure the NTP server on the Quick Setup page of the Toolbox.',
app: 'Backup App',
website: 'Backup Website',
rulesHelper:
'When there are multiple compression exclusion rules, they need to be displayed with line breaks. For example: \n*.log \n*.sql',
rulesHelper: 'When multiple compression exclusion rules exist, separate them with [,]. Example: *.log,*.sql',
lastRecordTime: 'Last Execution',
all: 'All',
failedRecord: 'Failed Records',
Expand Down Expand Up @@ -995,6 +994,9 @@ const message = {
url: 'URL Address',
targetHelper: 'Backup accounts are maintained in panel settings.',
retainCopies: 'Retain Copies',
retryTimes: 'Retry Attempts',
timeout: 'Timeout',
retryTimesHelper: '0 means no retry after failure',
retainCopiesHelper: 'Number of copies to retain for execution records and logs',
retainCopiesHelper1: 'Number of copies to retain for backup files',
retainCopiesUnit: ' copies (View)',
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/lang/modules/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ const message = {
ntp_helper: 'ใƒ„ใƒผใƒซใƒœใƒƒใ‚ฏใ‚นใฎใ‚ฏใ‚คใƒƒใ‚ฏใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใƒšใƒผใ‚ธใงNTPใ‚ตใƒผใƒใƒผใ‚’ๆง‹ๆˆใงใใพใ™ใ€‚',
app: 'ใƒใƒƒใ‚ฏใ‚ขใƒƒใƒ—ใ‚ขใƒ—ใƒช',
website: 'ใƒใƒƒใ‚ฏใ‚ขใƒƒใƒ—ใ‚ฆใ‚งใƒ–ใ‚ตใ‚คใƒˆ',
rulesHelper: 'ๅœง็ธฎ้™คๅค–ใƒซใƒผใƒซใŒใ‚ใ‚‹ๅ ดๅˆใ€ใƒฉใ‚คใƒณใƒ–ใƒฌใƒผใ‚ฏใง่กจ็คบใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€ n*.log n*.sql',
rulesHelper: '่ค‡ๆ•ฐใฎๅœง็ธฎ้™คๅค–ใƒซใƒผใƒซใŒใ‚ใ‚‹ๅ ดๅˆใ€[,]ใงๅŒบๅˆ‡ใ‚Šใพใ™ใ€‚ไพ‹: *.log,*.sql',
lastRecordTime: 'ๆœ€ๅพŒใฎๅฎŸ่กŒๆ™‚้–“',
all: 'ๅ…จใฆ',
failedRecord: 'ๅคฑๆ•—่จ˜้Œฒ',
Expand All @@ -962,6 +962,9 @@ const message = {
url: 'URLใ‚ขใƒ‰ใƒฌใ‚น',
targetHelper: 'ใƒใƒƒใ‚ฏใ‚ขใƒƒใƒ—ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใฏใ€ใƒ‘ใƒใƒซ่จญๅฎšใง็ถญๆŒใ•ใ‚Œใพใ™ใ€‚',
retainCopies: '่จ˜้Œฒใ‚’ไฟๆŒใ—ใพใ™',
retryTimes: 'ใƒชใƒˆใƒฉใ‚คๅ›žๆ•ฐ',
timeout: 'ใ‚ฟใ‚คใƒ ใ‚ขใ‚ฆใƒˆ',
retryTimesHelper: '0ใฏๅคฑๆ•—ๅพŒใƒชใƒˆใƒฉใ‚คใ—ใชใ„ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™',
retainCopiesHelper: 'ๅฎŸ่กŒ่จ˜้Œฒใจใƒญใ‚ฐใฎใŸใ‚ใซไฟๆŒใ™ใ‚‹ใ‚ณใƒ”ใƒผใฎๆ•ฐ',
retainCopiesHelper1: 'ใƒใƒƒใ‚ฏใ‚ขใƒƒใƒ—ใƒ•ใ‚กใ‚คใƒซ็”จใซไฟๆŒใ™ใ‚‹ใ‚ณใƒ”ใƒผใฎๆ•ฐ',
retainCopiesUnit: 'ใ‚ณใƒ”ใƒผ๏ผˆ่กจ็คบ๏ผ‰',
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/lang/modules/ko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ const message = {
ntp_helper: 'Toolbox ์˜ ๋น ๋ฅธ ์„ค์ • ํŽ˜์ด์ง€์—์„œ NTP ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.',
app: '๋ฐฑ์—… ์•ฑ',
website: '๋ฐฑ์—… ์›น์‚ฌ์ดํŠธ',
rulesHelper: '์—ฌ๋Ÿฌ ๊ฐœ์˜ ์••์ถ• ์ œ์™ธ ๊ทœ์น™์ด ์žˆ์„ ๊ฒฝ์šฐ ์ค„ ๋ฐ”๊ฟˆ์œผ๋กœ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ:\n*.log \n*.sql',
rulesHelper: '์—ฌ๋Ÿฌ ๊ฐœ์˜ ์••์ถ• ์ œ์™ธ ๊ทœ์น™์ด ์žˆ๋Š” ๊ฒฝ์šฐ [,]๋กœ ๊ตฌ๋ถ„ํ•˜์„ธ์š”. ์˜ˆ: *.log,*.sql',
lastRecordTime: '๋งˆ์ง€๋ง‰ ์‹คํ–‰ ์‹œ๊ฐ„',
all: '์ „์ฒด',
failedRecord: '์‹คํŒจํ•œ ๋ ˆ์ฝ”๋“œ',
Expand All @@ -950,6 +950,9 @@ const message = {
url: 'URL ์ฃผ์†Œ',
targetHelper: '๋ฐฑ์—… ๊ณ„์ •์€ ํŒจ๋„ ์„ค์ •์—์„œ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค.',
retainCopies: '๊ธฐ๋ก ๋ณด๊ด€',
retryTimes: '์žฌ์‹œ๋„ ํšŸ์ˆ˜',
timeout: 'ํƒ€์ž„์•„์›ƒ',
retryTimesHelper: '0์€ ์‹คํŒจ ํ›„ ์žฌ์‹œ๋„ ์•ˆ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค',
retainCopiesHelper: '์‹คํ–‰ ๊ธฐ๋ก๊ณผ ๋กœ๊ทธ์— ๋Œ€ํ•ด ๋ณด๊ด€ํ•  ๋ณต์‚ฌ๋ณธ ์ˆ˜',
retainCopiesHelper1: '๋ฐฑ์—… ํŒŒ์ผ์— ๋Œ€ํ•ด ๋ณด๊ด€ํ•  ๋ณต์‚ฌ๋ณธ ์ˆ˜',
retainCopiesUnit: '๊ฐœ (๋ณด๊ธฐ)',
Expand Down
Loading
Loading