go 动态定时任务 API 接口
使用
- 连接数据库表,启动服务 初始化所有定时任务,然后通过 API接口 动态修改 定时任务。
- 数据库表
CREATE TABLE `go_cron` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`time` varchar(100) DEFAULT ‘‘ COMMENT ‘定时时间‘,
`ip` varchar(100) DEFAULT ‘‘ COMMENT ‘内网IP‘,
`shell` varchar(50) DEFAULT ‘‘ COMMENT ‘命令‘,
`env` varchar(50) DEFAULT ‘‘ COMMENT ‘环境‘,
`run` int(10) DEFAULT ‘0‘ COMMENT ‘是否运行‘,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
代码
package main
import (
"fmt"
"github.com/Unknwon/com"
"github.com/gin-gonic/gin"
"github.com/gogf/gf/g/os/gcron"
"github.com/gogf/gf/g/os/glog"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"strconv"
)
var db *gorm.DB
type Cron struct {
ID int `gorm:"primary_key" json:"id"`
Time string `json:"time"`
Ip string `json:"ip"`
Shell string `json:"shell"`
Env string `json:"env"`
Run int `json:"run"`
}
func GetCron(id int) (user Cron) {
db.Where("id = ?", id).First(&user)
return
}
func GetCronAll() ([] *Cron) {
var cron []*Cron
db.Where("run = ?", 0).Find(&cron)
return cron
}
func InitDb() {
var err error
db, err = gorm.Open("mysql", fmt.Sprintf("root:123456@tcp(192.168.100.50)/go?charset=utf8&parseTime=True&loc=Local", ))
if err != nil {
glog.Println(err)
}
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return "go_" + defaultTableName
}
db.SingularTable(true)
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
}
func main() {
InitDb()
r := gin.Default()
gc := gcron.New()
data := GetCronAll()
for _, v := range data {
_, err := gc.Add(v.Time, func() { glog.Println(v.shell) }, strconv.Itoa(v.ID))
if err != nil {
fmt.Println("初始化定时任务错误 ",err)
}
}
r.GET("/crons/:id", func(c *gin.Context) {
id := com.StrTo(c.Param("id")).MustInt()
run := com.StrTo(c.Query("run")).MustInt()
if run == 0 {
gc.Start(strconv.Itoa(id))
glog.Println("启动", id)
c.JSON(200, gin.H{
"data": 0,
})
} else if run == 1 {
glog.Println("关闭", id)
gc.Stop(strconv.Itoa(id))
c.JSON(200, gin.H{
"data": 1,
})
} else {
glog.Println("参数错误", id)
c.JSON(400, gin.H{
"data": "参数不对, 参数为 run=0/1",
})
}
})
r.POST("/crons/:id", func(c *gin.Context) {
id := com.StrTo(c.Param("id")).MustInt()
data := GetCron(id)
_, err := gc.Add(data.Time, func() { glog.Println(data.shell) }, strconv.Itoa(data.ID))
if err != nil {
glog.Println("新增任务错误 ",err)
c.JSON(400, gin.H{
"data":err,
})
}else {
glog.Println("新增任务 成功 ")
c.JSON(200, gin.H{
"data":0,
})
}
})
r.PUT("/crons/:id", func(c *gin.Context) {
id := com.StrTo(c.Param("id")).MustInt()
data := GetCron(id)
gc.Stop(strconv.Itoa(id))
gc.Remove(strconv.Itoa(id))
_, err := gc.Add(data.Time, func() { glog.Println(data.shell) }, strconv.Itoa(data.ID))
if err != nil {
glog.Println("更新 任务错误 ",err)
c.JSON(400, gin.H{
"data":err,
})
}else {
glog.Println("更新 任务 成功 ")
c.JSON(200, gin.H{
"data":0,
})
}
})
r.DELETE("/crons/:id", func (c *gin.Context) {
id := com.StrTo(c.Param("id")).MustInt()
fmt.Println(gc.Search(strconv.Itoa(id)))
glog.Println("删除", id)
gc.Stop(strconv.Itoa(id))
fmt.Println(gc.Search(strconv.Itoa(id)))
gc.Remove(strconv.Itoa(id))
fmt.Println(gc.Search(strconv.Itoa(id)))
err := gc.Search(strconv.Itoa(id))
if err != nil {
c.JSON(400, gin.H{
"data": "删除成功",
})
}else {
c.JSON(200, gin.H{
"data": 0,
})
}
})
_ = r.Run("127.0.0.1:8080")
}
原文地址:https://blog.51cto.com/hequan/2372597
时间: 2024-11-05 20:29:04