美文网首页
gocron定时任务

gocron定时任务

作者: 帶頭二哥 | 来源:发表于2019-12-20 01:26 被阅读0次

安装:

go get -u github.com/jasonlvhit/gocron

每隔1秒执行一个任务,每隔4秒执行另一个任务:

package main

import (
    "fmt"
    "time"

    "github.com/jasonlvhit/gocron"
)

func task() {
    fmt.Println("I am runnning task.", time.Now())
}
func superWang() {
    fmt.Println("I am runnning superWang.", time.Now())
}

func main() {
    s := gocron.NewScheduler()
    s.Every(1).Seconds().Do(task)
    s.Every(4).Seconds().Do(superWang)

    sc := s.Start() // keep the channel
    go test(s, sc)  // wait
    <-sc            // it will happens if the channel is closed
}

func test(s *gocron.Scheduler, sc chan bool) {
    time.Sleep(8 * time.Second)
    s.Remove(task) //remove task
    time.Sleep(8 * time.Second)
    s.Clear()
    fmt.Println("All task removed")
    close(sc) // close the channel
}

相关文章

  • gocron定时任务

    安装: 每隔1秒执行一个任务,每隔4秒执行另一个任务:

  • gocron定时任务

    引用包: "github.com/robfig/cron/v3" 1. 基本使用 1.1 初始化实例 new() ...

  • gocron - 定时任务web管理系统

    gocron - 定时任务管理系统 项目简介 使用Go语言开发的定时任务集中调度和管理系统, 用于替代Linux-...

  • 使用gocron编写定时任务

    linux自带的crontab默认情况下只能精确到分钟,没法执行秒级任务。当然,也不是不行,比如: 看起来low了...

  • 基于gocron实现的定时任务服务降级方案

    一、背景 目前APP业务中启用的定时任务已达到400+,目前管理比较混乱,很多任务运行时占用服务器资源巨大,其...

  • [国产]Golang实现的定时任务管理系统:gocron

    使用Go语言开发的轻量级定时任务集中调度和管理系统, 用于替代Linux-crontab 查看文档[https:/...

  • 2019-07-31定时任务

    定时任务 定时任务实现方法 系统默认定时任务 用户自定义设置定时任务 定时任务配置文件 定时任务启动 定时任务样例...

  • 分布式定时调度-xxl-job

    一、定时任务概述 1.1 定时任务认识 1.1.1 什么是定时任务 定时任务是按照指定时间周期运行任务。使用场景为...

  • day 22 操作系统定时任务

    系统定时任务概念==生活中闹钟 系统定时任务实现方法: 实现定时任务配置: 定时任务如何进行设置 定时任务编写常见...

  • 7月30日 定时任务

    定时任务 代替人自动完成一些任务 定时任务实现的方法 定时任务软件:cronie定时任务软件:atd --- 设...

网友评论

      本文标题:gocron定时任务

      本文链接:https://www.haomeiwen.com/subject/jecbnctx.html