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

相关推荐
程序员岳焱22 分钟前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
麦兜*1 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
大只鹅1 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
ai小鬼头1 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
IT_10242 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
盖世英雄酱581362 小时前
容易被程序员忽略的硬件设备
程序员
bobz9652 小时前
动态规划
后端
stark张宇2 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
亚力山大抵3 小时前
实验六-使用PyMySQL数据存储的Flask登录系统-实验七-集成Flask-SocketIO的实时通信系统
后端·python·flask
超级小忍3 小时前
Spring Boot 中常用的工具类库及其使用示例(完整版)
spring boot·后端