name5566 / leaf

A game server framework in Go (golang)
Apache License 2.0
5.3k stars 1.32k forks source link

请问skeleton.CronFunc(cronExpr, 是怎么用的? #110

Closed jjjjilyf closed 6 years ago

jjjjilyf commented 6 years ago

cronExpr, err := timer.NewCronExpr("1 *")创建这个不是1秒钟 查询一次的意思吗?

name5566 commented 6 years ago

可以看下:https://github.com/name5566/leaf/blob/master/timer/cronexpr.go#L12

jjjjilyf commented 6 years ago

谢谢回答,我看了一下 md文件,那个test里的代码 func ExampleTimer() { d := timer.NewDispatcher(10)

// timer 1
d.AfterFunc(1, func() {
    fmt.Println("My name is Leaf")
})

// timer 2
t := d.AfterFunc(1, func() {
    fmt.Println("will not print")
})
t.Stop()

// dispatch
(<-d.ChanTimer).Cb()

// Output:
// My name is Leaf

}

如果不调用 (<-d.ChanTimer).Cb() 是不是就不执行了,如果没有stop呢?也不执行吧?没看到有从d.ChanTimer向外读取的地方啊?

jjjjilyf commented 6 years ago

func (g *Go) Go(f func(), cb func()) { g.pendingGo++

go func() {
    defer func() {
        g.ChanCb <- cb
        if r := recover(); r != nil {
            if conf.LenStackBuf > 0 {
                buf := make([]byte, conf.LenStackBuf)
                l := runtime.Stack(buf, false)
                log.Error("%v: %s", r, buf[:l])
            } else {
                log.Error("%v", r)
            }
        }
    }()

    f()
}()

}

btw黑体部分是什么意思呢,这个和上面的代码有些类似,都是把函数传到一个当前goroutine的channal中,但是同样没有看到读取出来执行的代码,拜求大神解惑。。 if conf.LenStackBuf > 0 { buf := make([]byte, conf.LenStackBuf) l := runtime.Stack(buf, false) log.Error("%v: %s", r, buf[:l]) } else { log.Error("%v", r) }

name5566 commented 6 years ago

使用 skeleton 来使用 cron。

jjjjilyf commented 6 years ago

Entry Description Equivalent to @yearly (or @annually) Run once a year at midnight of 1 January 0 0 1 1 @monthly Run once a month at midnight of the first day of the month 0 0 1 @weekly Run once a week at midnight on Sunday morning 0 0 0 @daily Run once a day at midnight 0 0 @hourly Run once an hour at the beginning of the hour 0 @reboot Run at startup N/A

NoTHX :|)