Golang 使用定时任务(robfig/cron/v3)

前言

go执行定时任务,可以使用robfig/cron/v3包,robfig/cron/v3 是一个 Golang 的定时任务库,支持 cron 表达式

定时任务使用

参考文档

定时任务参考文档可以参考

arduino 复制代码
 https://pkg.go.dev/github.com/robfig/cron/v3
 

依赖安装

执行以下命令安装依赖

kotlin 复制代码
 go get github.com/robfig/cron/v3@v3.0.0
 

cron使用

每分钟执行一次

go 复制代码
package main

import (
   "github.com/robfig/cron/v3"
   "log"
   "time"
)

func TestCron() {
   c := cron.New()
   i := 1
   _, err := c.AddFunc("* * * * *", func() {
      log.Println("数据为: ", i)
      i++
   })
   if err != nil {
      return
   }
   c.Start()
   time.Sleep(time.Minute * 5)
}
func main() {
   TestCron()
}

输出结果

corn支持秒

精确到秒的 Cron 表达式

Cron v3 版本的表达式从六个参数调整为五个,取消了对秒的默认支持,需要精确到秒的控制可以使用 cron.WithSeconds() 解析器

每秒执行一次

go 复制代码
package main

import (
    "github.com/robfig/cron/v3"
    "log"
    "time"
)

func TestCron() {
    c := cron.New(cron.WithSeconds())
    i := 1
    _, err := c.AddFunc("*/1 * * * * *", func() {
       log.Println("数据为: ", i)
       i++
    })
    if err != nil {
       return
    }
    c.Start()
    time.Sleep(time.Minute * 5)
}
func main() {
    TestCron()
}

输出结果为

使用@every

每10s执行一次

go 复制代码
package main

import (
    "fmt"
    "github.com/robfig/cron/v3"
    "time"
)

func TestCron() {
    c := cron.New()
    c.AddFunc("@every 10s", func() {
       fmt.Println("Every 10 Seconds")
    })
    c.Start()

    time.Sleep(time.Minute * 5)
}
func main() {
    TestCron()
}

输出结果为

设置时区

scss 复制代码
package main

import (
    "fmt"
    "github.com/robfig/cron/v3"
    "time"
)

func TestCron() {
    //c := cron.New()
    // 上海时区早六点
    nyc, _ := time.LoadLocation("Asia/Shanghai")
    c := cron.New(cron.WithLocation(nyc))
    c.AddFunc("@every 10s", func() {
       fmt.Println(12)
    })
    c.Start()

    time.Sleep(time.Minute * 5)
}
func main() {
    TestCron()
}

AddJob使用

go 复制代码
package main

import (
    "github.com/robfig/cron/v3"
    "log"
    "time"
)

func TestCron() {
    c := cron.New(cron.WithSeconds())

    // 上海时区早六点
    _, err := c.AddJob("*/5 * * * * *", Hello{})
    if err != nil {
       return
    }
    c.Start()

    time.Sleep(time.Minute * 5)
}

type Hello struct {
}

func (t Hello) Run() {
    log.Println("hello world")
}

func main() {
    TestCron()
}

输出结果为

终止任务

go 复制代码
package main

import (
    "github.com/robfig/cron/v3"
    "log"
    "time"
)

func TestCron() {
    c := cron.New(cron.WithSeconds())

    // 上海时区早六点
    _, err := c.AddJob("*/2 * * * * *", Hello{})
    if err != nil {
       return
    }
    c.Start()

    go func() {
       time.Sleep(5 * time.Second)
       log.Println("==============终止任务")
       c.Stop()
    }()

    time.Sleep(time.Minute * 5)
}

type Hello struct {
}

func (t Hello) Run() {
    log.Println("hello world")
}

func main() {
    TestCron()
}

输出结果为

总结

cron则提供了更强大的定时任务调度功能,适用于复杂场景。根据实际需求选择合适的方法,可以方便地实现定时任务的开发

相关推荐
csdn_wuwt1 小时前
前后端中Dto是什么意思?
开发语言·网络·后端·安全·前端框架·开发
JosieBook1 小时前
【Rust】 基于Rust 从零构建一个本地 RSS 阅读器
开发语言·后端·rust
努力的小郑5 小时前
今晚Cloudflare一哆嗦,我的加班计划全泡汤
前端·后端·程序员
百***61875 小时前
springboot整合mybatis-plus(保姆教学) 及搭建项目
spring boot·后端·mybatis
q***58195 小时前
Spring全家桶简介
java·后端·spring
武昌库里写JAVA5 小时前
微擎服务器配置要求,微擎云主机多少钱一年?
java·vue.js·spring boot·后端·sql
IUGEI5 小时前
深入解析HTTP长连接原理
java·网络·后端·网络协议·tcp/ip·http·https
q***55586 小时前
SpringBoot项目中替换指定版本的tomcat
spring boot·后端·tomcat
汤姆yu6 小时前
基于springboot的电脑商城系统
java·spring boot·后端
码事漫谈6 小时前
Visual Studio 2026真的值得升级吗中国开发者实测报告
后端