分享

使用Go语言实现定时任务:轻松掌握Cron表达式

 F2967527 2024-05-27 发布于天津

一、cron基本使用

1、使用举例

package main
import ( 'fmt' 'github.com/robfig/cron')
//主函数func main() { cron2 := cron.New() //创建一个cron实例
//执行定时任务(每5秒执行一次) err:= cron2.AddFunc('*/5 * * * * *', print5) if err!=nil{ fmt.Println(err) }
//启动/关闭 cron2.Start() defer cron2.Stop() select { //查询语句,保持程序运行,在这里等同于for{} }}
//执行函数func print5() { fmt.Println('每5s执行一次cron')}

2、配置

┌─────────────second 范围 (0 - 60)│ ┌───────────── min (0 - 59)│ │ ┌────────────── hour (0 - 23)│ │ │ ┌─────────────── day of month (1 - 31)│ │ │ │ ┌──────────────── month (1 - 12)│ │ │ │ │ ┌───────────────── day of week (0 - 6)│ │ │ │ │ ││ │ │ │ │ │*  *  *  *  *  *

3、多个crontab任务

package main
import ( 'fmt'
'github.com/robfig/cron')
type TestJob struct {}
func (this TestJob) Run() { fmt.Println('testJob1...')}
type Test2Job struct {}
func (this Test2Job) Run() { fmt.Println('testJob2...')}
//启动多个任务func main() { c := cron.New() spec := '*/5 * * * * ?' //AddJob方法 c.AddJob(spec, TestJob{}) c.AddJob(spec, Test2Job{}) //启动计划任务 c.Start() //关闭着计划任务, 但是不能关闭已经在执行中的任务. defer c.Stop()
select {}}
/*testJob1...testJob2...testJob1...testJob2...*/

二、gin框架cron应用

  • 目录结构

.├── main.go└── pkg    └── jobs        ├── job_cron.go    // 分布式任务配置        └── test_task.go   // 具体任务实例

1、main.go

package main
import ( 'go_cron_demo/pkg/jobs' 'net/http'
'github.com/gin-gonic/gin')
func main() { jobs.InitJobs() r := gin.Default() r.GET('/', func(c *gin.Context) { c.String(http.StatusOK, 'hello World!') }) r.Run(':8000')}

2、pkg/jobs/job_cron.go

package jobs
import ( 'github.com/robfig/cron')
var mainCron *cron.Cron
func init() { mainCron = cron.New() mainCron.Start()}
func InitJobs() { // 每5s钟调度一次,并传参 mainCron.AddJob( '*/5 * * * * ?', TestJob{Id: 1, Name: 'zhangsan'}, )}
/* 运行结果1 zhangsantestJob1...1 zhangsantestJob1...*/

3、pkg/jobs/test_task.go

package jobs
import 'fmt'
type TestJob struct { Id int Name string}
func (this TestJob) Run() { fmt.Println(this.Id, this.Name) fmt.Println('testJob1...')}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多