Skip to content

Commit

Permalink
feat: 实现计划任务
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Dec 15, 2023
1 parent 18b4ec9 commit 2565710
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 69 deletions.
11 changes: 7 additions & 4 deletions api/crontab/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func list(c *gin.Context) {
rq.UserId = c.GetUint("UserId")

if lst, err := cronjob.FetchAll(rq); err == nil {
c.Set("Payload", gin.H{"Items": lst})
c.Set("Payload", gin.H{
"Items": lst,
"Entries": crontab.GetEntries(lst),
})
} else {
c.Set("Error", err)
}
Expand Down Expand Up @@ -68,7 +71,7 @@ func create(c *gin.Context) {
rq.UserId = c.GetUint("UserId")

if id, err := cronjob.Create(rq); err == nil {
crontab.NewById(id)
crontab.NewById(rq.UserId, id)
c.Set("Payload", gin.H{"Id": id})
c.Set("Message", "添加成功")
} else {
Expand Down Expand Up @@ -96,7 +99,7 @@ func update(c *gin.Context) {
rq.UserId = c.GetUint("UserId")

if err := cronjob.Update(rq); err == nil {
crontab.RedoById(rq.Id)
crontab.RedoById(rq.UserId, rq.Id)
c.Set("Message", "修改成功")
} else {
c.Set("Error", err)
Expand All @@ -122,7 +125,7 @@ func delete(c *gin.Context) {

rq.UserId = c.GetUint("UserId")

crontab.UndoById(rq.Id) //TODO:高危
crontab.UndoById(rq.UserId, rq.Id)

if err := cronjob.Delete(rq); err == nil {
c.Set("Message", "删除成功")
Expand Down
19 changes: 9 additions & 10 deletions model/cronjob/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type CreateParam struct {
UserId uint
Name string `binding:"required"`
Type string `binding:"required"`
Target string `binding:"required"`
Content string `binding:"required"`
Second string `binding:"required"`
Minute string `binding:"required"`
Expand All @@ -20,8 +21,7 @@ type CreateParam struct {
Month string `binding:"required"`
DayofWeek string `binding:"required"`
Location string `binding:"required"`
PrevTime int64 `binding:"required"`
NextTime int64 `binding:"required"`
EntryId int64
}

func Create(data *CreateParam) (uint, error) {
Expand All @@ -30,6 +30,7 @@ func Create(data *CreateParam) (uint, error) {
UserId: data.UserId,
Name: data.Name,
Type: data.Type,
Target: data.Target,
Content: data.Content,
Second: data.Second,
Minute: data.Minute,
Expand All @@ -38,8 +39,7 @@ func Create(data *CreateParam) (uint, error) {
Month: data.Month,
DayofWeek: data.DayofWeek,
Location: data.Location,
PrevTime: data.PrevTime,
NextTime: data.NextTime,
EntryId: data.EntryId,
}

result := dborm.Db.Create(item)
Expand All @@ -55,6 +55,7 @@ type UpdateParam struct {
UserId uint
Name string
Type string
Target string
Content string
Second string
Minute string
Expand All @@ -63,8 +64,7 @@ type UpdateParam struct {
Month string
DayofWeek string
Location string
PrevTime int64
NextTime int64
EntryId int64
}

func Update(data *UpdateParam) error {
Expand All @@ -77,6 +77,7 @@ func Update(data *UpdateParam) error {
Updates(model.Cronjob{
Name: data.Name,
Type: data.Type,
Target: data.Target,
Content: data.Content,
Second: data.Second,
Minute: data.Minute,
Expand All @@ -85,8 +86,7 @@ func Update(data *UpdateParam) error {
Month: data.Month,
DayofWeek: data.DayofWeek,
Location: data.Location,
PrevTime: data.PrevTime,
NextTime: data.NextTime,
EntryId: data.EntryId,
})

return result.Error
Expand Down Expand Up @@ -138,8 +138,7 @@ func Fetch(data *FetchParam) (*model.Cronjob, error) {
// 获取计划列表

type FetchAllParam struct {
UserId uint
VendorId uint
UserId uint
}

func FetchAll(data *FetchAllParam) ([]*model.Cronjob, error) {
Expand Down
4 changes: 2 additions & 2 deletions model/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Cronjob struct {
UserId uint `gorm:"index"`
Name string `gorm:"size:128"`
Type string `gorm:"size:32"`
Target string `gorm:"size:32"`
Content string `gorm:"type:text"`
Second string `gorm:"size:32"`
Minute string `gorm:"size:32"`
Expand All @@ -44,8 +45,7 @@ type Cronjob struct {
Month string `gorm:"size:32"`
DayofWeek string `gorm:"size:32"`
Location string `gorm:"size:1024"`
PrevTime int64
NextTime int64
EntryId int64 `gorm:"size:32;index"`
CreatedAt int64
UpdatedAt int64
}
Expand Down
File renamed without changes.
98 changes: 98 additions & 0 deletions module/crontab/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package crontab

import (
"github.com/opentdp/go-helper/command"
"github.com/opentdp/go-helper/logman"
"github.com/opentdp/go-helper/strutil"
cron "github.com/robfig/cron/v3"

"tdp-cloud/model"
"tdp-cloud/model/cronjob"
"tdp-cloud/model/machine"
"tdp-cloud/model/script"
"tdp-cloud/module/workhub"
)

var crontab *cron.Cron

func Daemon() {

crontab = cron.New(cron.WithSeconds())

RunJobs()

}

func RunJobs() {

jobs, err := cronjob.FetchAll(&cronjob.FetchAllParam{})

if err != nil || len(jobs) == 0 {
return
}

for _, job := range jobs {
NewByJob(job)
}

crontab.Start()

}

func NewByJob(job *model.Cronjob) {

var err error

switch job.Type {
case "script":
err = NewByScriptJob(job)
}

if err != nil {
logman.Error("run jobs", "error", err)
}

}

func NewByScriptJob(job *model.Cronjob) error {

she, err := script.Fetch(&script.FetchParam{
Id: strutil.ToUint(job.Content),
UserId: job.UserId,
})
if err != nil {
return err
}

mac, err := machine.Fetch(&machine.FetchParam{
Id: strutil.ToUint(job.Location),
UserId: job.UserId,
})
if err != nil {
return err
}

spec := job.Second + " " + job.Minute + " " + job.Hour + " " + job.DayofMonth + " " + job.Month + " " + job.DayofWeek

entryId, err := crontab.AddFunc(spec, func() {
workhub.GetSendPod(mac.WorkerId).Exec(&command.ExecPayload{
Name: she.Name,
CommandType: she.CommandType,
Username: she.Username,
WorkDirectory: she.WorkDirectory,
Content: she.Content,
Timeout: she.Timeout,
})
})
if err != nil {
return err
}

err = cronjob.Update(&cronjob.UpdateParam{
Id: job.Id,
EntryId: int64(entryId),
})

return err

}
53 changes: 0 additions & 53 deletions module/crontab/main.go

This file was deleted.

56 changes: 56 additions & 0 deletions module/crontab/manage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package crontab

import (
"tdp-cloud/model"
"tdp-cloud/model/cronjob"

"github.com/robfig/cron/v3"
)

func NewById(userId, id uint) {

job, err := cronjob.Fetch(&cronjob.FetchParam{Id: id})

if err == nil && job.Id > 0 {
NewByScriptJob(job)
}

}

func UndoById(userId, id uint) {

job, err := cronjob.Fetch(&cronjob.FetchParam{Id: id})

if err == nil && job.Id > 0 {
crontab.Remove(cron.EntryID(job.Id))
}

}

func RedoById(userId, id uint) {

job, err := cronjob.Fetch(&cronjob.FetchParam{Id: id})

if err == nil && job.Id > 0 {
crontab.Remove(cron.EntryID(job.Id))
NewByScriptJob(job)
}

}

func GetEntries(jobs []*model.Cronjob) map[uint]any {

list := map[uint]any{}

for _, job := range jobs {
entry := crontab.Entry(cron.EntryID(job.EntryId))
list[job.Id] = map[string]any{
"EntryId": entry.ID,
"NextTime": entry.Next,
"PrevTime": entry.Prev,
}
}

return list

}
2 changes: 2 additions & 0 deletions service/server/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"tdp-cloud/api"
"tdp-cloud/cmd/args"
"tdp-cloud/module/certbot"
"tdp-cloud/module/crontab"
"tdp-cloud/module/migrator"
)

Expand All @@ -15,6 +16,7 @@ func origin() {
dbConnect()

go certbot.Daemon()
go crontab.Daemon()

go httpServer()

Expand Down

0 comments on commit 2565710

Please sign in to comment.