golang 定时器
上网查了下相关资料,基本上都介绍的是github.com\robfig\cron这个包来执行定时任务,试了下确实可以执行。但是此包下没有删 除任务的方法,只有暂停的方法(Stop),若要停止之前的任务只执行新定义的任务,除非服务器重启再定义新任务。后来又参考了一下其他人的建议,采用了 github.com\jakecoffman\cron这个包。后者是在前者的基础上做了一定的修改,给每个任务添加的一个新属性Name,然后新添加 一个remove(name)方法,用来删除指定名字的任务,经测试使用后可正常删除任务,只不过之前的计划任务名得新建一个全局变量来存储。
以下先简单介绍下定义时间集合的信息:
字段名 | 是否必须 | 允许的值 | 允许的特定字符 |
秒(Seconds) | 是 | 0-59 |
* / , -
|
分(Minutes) | 是 | 0-59 |
* / , -
|
时(Hours) | 是 | 0-23 |
* / , -
|
日(Day of month) | 是 | 1-31 |
* / , – ?
|
月(Month) | 是 | 1-12 or JAN-DEC |
* / , -
|
星期(Day of week) | 否 | 0-6 or SUM-SAT |
* / , – ?
|
注:
1)月(Month)和星期(Day of week)字段的值不区分大小写,如:SUN、Sun 和 sun 是一样的。
2)星期
(Day of week)字段如果没提供,相当于是 *
2、特殊字符说明
1)星号(*)
表示 cron 表达式能匹配该字段的所有值。如在第5个字段使用星号(month),表示每个月
回到顶部
2)斜线(/)
表示增长间隔,如第1个字段(minutes) 值是 3-59/15,表示每小时的第3分钟开始执行一次,之后每隔 15 分钟执行一次(即 3、18、33、48 这些时间点执行),这里也可以表示为:3/15
回到顶部
3)逗号(,)
用于枚举值,如第6个字段值是 MON,WED,FRI,表示 星期一、三、五 执行
回到顶部
4)连字号(-)
表示一个范围,如第3个字段的值为 9-17 表示 9am 到 5pm 直接每个小时(包括9和17)
回到顶部
5)问号(?)
只用于 日(Day of month) 和 星期(Day of week),表示不指定值,可以用于代替 *
在beego下使用,我简单的封装一下(代码不是我写的,而且写的很随意,也只保证了可用的程度),自定义package jobs如下:
job.go源码:
- package jobs
- import (
- "github.com/astaxie/beego"
- "github.com/jakecoffman/cron"
- "reflect"
- "runtime/debug"
- "sync"
- "sync/atomic"
- )
- type Job struct {
- Name string
- inner cron.Job
- status uint32
- running sync.Mutex
- }
- const UNNAMED = "(unnamed)"
- func New(job cron.Job) *Job {
- name := reflect.TypeOf(job).Name()
- if name == "Func" {
- name = UNNAMED
- }
- return &Job{
- Name: name,
- inner: job,
- }
- }
- func (j *Job) Status() string {
- if atomic.LoadUint32(&j.status) > 0 {
- return "RUNNING"
- }
- return "IDLE"
- }
- func (j *Job) Run() {
- // If the job panics, just print a stack trace.
- // Don't let the whole process die.
- defer func() {
- if err := recover(); err != nil {
- beego.Error("%v", debug.Stack())
- }
- }()
- if !selfConcurrent {
- j.running.Lock()
- defer j.running.Unlock()
- }
- if workPermits != nil {
- workPermits <- struct{}{}
- defer func() { <-workPermits }()
- }
- atomic.StoreUint32(&j.status, 1)
- defer atomic.StoreUint32(&j.status, 0)
- j.inner.Run()
- }
jobrunner.go源码:
- // A job runner for executing scheduled or ad-hoc tasks asynchronously from HTTP requests.
- //
- // It adds a couple of features on top of the cron package to make it play nicely with Revel:
- // 1. Protection against job panics. (They print to ERROR instead of take down the process)
- // 2. (Optional) Limit on the number of jobs that may run simulatenously, to
- // limit resource consumption.
- // 3. (Optional) Protection against multiple instances of a single job running
- // concurrently. If one execution runs into the next, the next will be queued.
- // 4. Cron expressions may be defined in app.conf and are reusable across jobs.
- // 5. Job status reporting.
- package jobs
- import (
- //"fmt"
- "github.com/jakecoffman/cron"
- "time"
- )
- // Callers can use jobs.Func to wrap a raw func.
- // (Copying the type to this package makes it more visible)
- //
- // For example:
- // jobs.Schedule("cron.frequent", jobs.Func(myFunc))
- type Func func()
- func (r Func) Run() { r() }
- //定时执行任务
- func Schedule(spec string, job cron.Job, name string) error {
- sched := cron.Parse(spec)
- /*if err != nil {
- return err
- }*/
- MainCron.Schedule(sched, New(job), name)
- return nil
- }
- // Run the given job at a fixed interval.
- // The interval provided is the time between the job ending and the job being run again.
- // The time that the job takes to run is not included in the interval.
- func Every(duration time.Duration, job cron.Job, name string) {
- MainCron.Schedule(cron.Every(duration), New(job), name)
- }
- // Run the given job right now.
- func Now(job cron.Job) {
- go New(job).Run()
- }
- // Run the given job once, after the given delay.
- func In(duration time.Duration, job cron.Job) {
- go func() {
- time.Sleep(duration)
- New(job).Run()
- }()
- }
- // 暂停所有任务
- func Stop() {
- MainCron.Stop()
- }
- //清空任务
- func Remove(name string) {
- MainCron.RemoveJob(name)
- }
plugin.go源码:
- package jobs
- import (
- "github.com/jakecoffman/cron"
- )
- const DEFAULT_JOB_POOL_SIZE = 10
- var (
- // Singleton instance of the underlying job scheduler.
- MainCron *cron.Cron
- // This limits the number of jobs allowed to run concurrently.
- workPermits chan struct{}
- // Is a single job allowed to run concurrently with itself?
- selfConcurrent bool
- )
- func init() {
- MainCron = cron.New()
- workPermits = make(chan struct{}, DEFAULT_JOB_POOL_SIZE)
- selfConcurrent = false
- MainCron.Start()
- }
因为是网站执行定时任务,这边我是将任务是否开启的控制权放在前端页面,通过点击按钮访问controller方法,从而启动任务,所以我把任务执行方法放在了controller中。通过如下方式调用:
- import (
- //其他引用包我都省略了
- "hnnaserver/src/jobs"
- )
- type job struct {
- user string
- province int64
- method int64
- count int64
- }
- //任务执行的方法
- func (j *job) Run() {
- //do something....
- }
- func (this *SystemController) AutoDisCus() {
- spec := "0 0 15 * * 1-5"//定义执行时间点 参照上面的说明可知 执行时间为 周一至周五每天15:00:00执行
- //spec := "*/5 * * * * ?" //这是网上一般的例子 即每5s执行一次
- j := &job{user, province, method, count}
- jobs.Schedule(spec, j, taskName) //3个参数分别为 执行时间(规则之间介绍过) 要执行的任务 任务名
- }
若要删除某项任务,则调用remove方法即可,如下:
- jobs.Remove(tasksName)
golang 定时器的更多相关文章
- [Go] golang定时器与redis结合
golang定时器与redis结合,每隔1秒ping一下,每隔20秒llen一下队列的长度 package main import ( "fmt" "time" ...
- Golang定时器断续器
定时器 1.定时器结构 结构定义 type Timer struct { C <-chan Time // 接受定时器事件的通道 r runtimeTimer } type runtimeTim ...
- [Go] golang定时器的使用
golang中的定时器是使用的chanel阻塞来实现的,主要使用到了time包中的内容,如果有多个定时器的channel,为了防止阻塞,可以使用select来获取遍历channel 定时器获取的cha ...
- 一周学会go语言并应用 by王奇疏
<一周学会go语言并应用> by王奇疏 ( 欢迎加入go语言群: 218160862 , 群内有实践) 点击加入 零.安装go语言,配置环境及IDE 这部分内容不多,请参考我的这篇安装环境 ...
- Golang之定时器,recover
滴答滴答……定时器的使用 package main import ( "fmt" "time" ) //定时器的使用 func main() { t := ti ...
- 生产者消费者模式做一个golang的定时器
在主程序启动的时候开一个goroutine作为消费者,用管道连接生产者和消费者,消费者处于无限循环,从管道中获取channel传过来定时event 注意:channel在消费者创建的时候就连通生产者和 ...
- golang 中的定时器(timer),更巧妙的处理timeout
今天看到kite项目中的一段代码,发现挺有意思的. // generateToken returns a JWT token string. Please see the URL for detail ...
- golang的定时器NewTimer、NewTicker使用
package main import ( "fmt" "sync" "time" ) /** *ticker只要定义完成,从此刻开始计时, ...
- [Golang]-7 定时器和打点器
目录 定时器 打点器 After()方法 我们常常需要在未来某个时刻运行 Go 代码,或者在某段时间间隔内重复运行. Go 的内置 定时器 和 打点器 特性让这些很容易实现. 定时器 type Tim ...
随机推荐
- 转载-Python学习笔记之文件读写
Python 文件读写 Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件 ...
- 2016年10月16日 星期日 --出埃及记 Exodus 18:27
2016年10月16日 星期日 --出埃及记 Exodus 18:27 Then Moses sent his father-in-law on his way, and Jethro returne ...
- dubbo源码之四——服务发布二
dubbo版本:2.5.4 2. 服务提供者暴露一个服务的详细过程 上图是服务提供者暴露服务的主过程: 首先ServiceConfig类拿到对外提供服务的实际类ref(如:HelloWorldImpl ...
- zookeeper系列之六—zookeeper之应用
http://www.cnblogs.com/sharpxiajun/archive/2013/06/02/3113923.html Zookeeper是hadoop的一个子项目,虽然源自hadoop ...
- zookeeper系列之二—zookeeper历史
Zookeeper是什么? Zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby的一个开源版本.它是为分布式应用提供一致性服务的软件,提供的功能包括:配置服 ...
- acdream1197 Points In Cuboid
题目链接:http://acdream.info/problem?pid=1197 题意:给出一些点.每次给出一个长方体,问在长方体中的点的个数. 思路:kd-tree. const int N=11 ...
- JAVA题 矩形
package com.lo; public class juxing { private double chang; private double kuan; public double getCh ...
- require或include相对路径多层嵌套引发的问题
require或include相对路径多层嵌套引发的问题 php中require/include 包含相对路径的解决办法 在PHP中require,include一个文件时,大都是用相对路径,是个 ...
- 【转】VB中应用DDE
动态数据交换(dde)是windows应用程序间通讯的基本方法之一,在动态数据交换的过程中,提供数据和服务的应用程序称为服务器,请求数据或服务的应用程序则称为客户. dde交谈是由客户程序启动的.如果 ...
- NPO与X7R、X5R、Y5V、Z5U神马的有啥区别
主要是介质材料不同.不同介质种类由于它的主要极化类型不一样,其对电场变化的响应速度和极化率亦不一样. 在相同的体积下的容量就不同,随之带来的电容器的介质损耗.容量稳定性等也就不同.介质材料划按容量的温 ...