.
git-svn-id: svn://47.119.165.148/zhub@63 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
62
zdb/ztimer.go
Normal file
62
zdb/ztimer.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package _zdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/robfig/cron"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
zTimer = make(map[string]*ZTimer)
|
||||
)
|
||||
|
||||
type ZTimer struct {
|
||||
conns []*net.Conn
|
||||
expr string
|
||||
topic string
|
||||
cron *cron.Cron
|
||||
}
|
||||
|
||||
func timer(rcmd []string, conn net.Conn) {
|
||||
ztimer := zTimer[rcmd[1]]
|
||||
if ztimer == nil {
|
||||
ztimer = &ZTimer{
|
||||
conns: []*net.Conn{},
|
||||
topic: rcmd[1],
|
||||
}
|
||||
zTimer[rcmd[1]] = ztimer
|
||||
}
|
||||
|
||||
_conns := make([]*net.Conn, 0)
|
||||
for _, c := range ztimer.conns {
|
||||
if *&conn == *c {
|
||||
continue
|
||||
}
|
||||
_conns = append(_conns, c)
|
||||
}
|
||||
_conns = append(_conns, &conn)
|
||||
ztimer.conns = _conns
|
||||
|
||||
if !strings.EqualFold(ztimer.expr, rcmd[2]) {
|
||||
ztimer.expr = rcmd[2]
|
||||
if ztimer.cron != nil {
|
||||
ztimer.cron.Stop()
|
||||
}
|
||||
ztimer.cron = func() *cron.Cron {
|
||||
c := cron.New()
|
||||
c.AddFunc(ztimer.expr, func() {
|
||||
fmt.Println(time.Now().Second())
|
||||
for _, conn := range ztimer.conns {
|
||||
Send(*conn, "timer", ztimer.topic)
|
||||
}
|
||||
})
|
||||
go c.Run()
|
||||
return c
|
||||
}()
|
||||
}
|
||||
|
||||
zTimer[ztimer.topic] = ztimer
|
||||
fmt.Println("xx")
|
||||
}
|
||||
Reference in New Issue
Block a user