git-svn-id: svn://47.119.165.148/zhub@63 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-01-09 10:53:35 +00:00
parent 2dcfc5e9bf
commit f1b7a862f9
15 changed files with 377 additions and 50 deletions

62
zsub/ztimer.go Normal file
View File

@@ -0,0 +1,62 @@
package zsub
import (
"fmt"
"github.com/robfig/cron"
"strings"
"time"
)
type ZTimer struct {
conns []*ZConn
expr string
topic string
cron *cron.Cron
}
func (s ZSub) timer(rcmd []string, c *ZConn) {
timer := s.timers[rcmd[1]]
if timer == nil {
timer = &ZTimer{
conns: []*ZConn{},
topic: rcmd[1],
}
s.timers[rcmd[1]] = timer
}
_conns := make([]*ZConn, 0)
for _, conn := range timer.conns {
if conn == c {
continue
}
_conns = append(_conns, c)
}
_conns = append(_conns, c)
timer.conns = _conns
if !strings.EqualFold(timer.expr, rcmd[2]) {
timer.expr = rcmd[2]
if timer.cron != nil {
timer.cron.Stop()
}
timer.cron = func() *cron.Cron {
c := cron.New()
c.AddFunc(timer.expr, func() {
fmt.Println(time.Now().Second())
for _, conn := range timer.conns {
send(conn.conn, "timer", timer.topic)
}
})
go c.Run()
return c
}()
}
s.timers[rcmd[1]] = timer
fmt.Println("xx")
}
func (t ZTimer) close(c *ZConn) {
// todo timer zconn
}