first push

This commit is contained in:
renzhiyuan 2025-02-20 16:39:27 +08:00
commit 33ec38ebbe
4 changed files with 167 additions and 0 deletions

28
func.go Normal file
View File

@ -0,0 +1,28 @@
package l_notify
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func keepAlive() {
// 创建一个通道来监听操作系统信号
stop := make(chan bool)
// 监听系统信号
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signalChan
stop <- true
}()
select {
case <-stop:
fmt.Println("Received stop signal, exiting...")
return
}
}

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module gitea.cdlsxd.cn/self-tools/l_notify
go 1.23.6
require (
gitea.cdlsxd.cn/self-tools/l_request v1.0.4 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
)

87
notify.go Normal file
View File

@ -0,0 +1,87 @@
package l_notify
import (
"fmt"
"gitea.cdlsxd.cn/self-tools/l_request"
"github.com/robfig/cron/v3"
)
type Notify struct {
Request *l_request.Request
DoFuc func() (l_request.Response, error) //测试用
DelayList []int32 //[30,60,120...]从前往后分别延迟30秒60秒120秒...推送,[60]每隔60秒推送一次
ResultHandle func(l_request.Response, error) (stop bool) //推送结果处理函数,stop为true时表示停止推送
cron *cron.Cron
stop chan struct{}
currenEntry cron.EntryID
isLoop bool
i int8
}
func (n *Notify) Notify() error {
//必要参数检测
err := n.check()
if err != nil {
return err
}
if len(n.DelayList) == 1 {
//如果只有一次,则默认循环推送
n.isLoop = true
}
n.stop = make(chan struct{})
//启动定时器
n.cron = cron.New()
n.next()
n.cron.Start()
return nil
}
func (n *Notify) next() {
if n.i+1 > int8(len(n.DelayList)) {
//当不存在下次执行,则退出定时器
n.cron.Stop()
close(n.stop)
return
}
time := "@every " + fmt.Sprintf("%ds", n.DelayList[n.i])
n.currenEntry, _ = n.cron.AddFunc(time, func() {
e := n.ResultHandle(n.DoFuc())
if n.isLoop {
//循环推送直到成功
if e {
n.stop <- struct{}{}
}
} else {
n.stop <- struct{}{}
}
})
//开启信号监听
n.registerListener()
return
}
func (n *Notify) check() error {
if n.Request == nil && n.DoFuc == nil {
return fmt.Errorf("未初始化Request")
}
if len(n.DelayList) == 0 {
return fmt.Errorf("未设置推送延迟")
}
return nil
}
func (n *Notify) registerListener() {
//注册信号监听通道
go func() {
select {
case <-n.stop:
n.cron.Remove(n.currenEntry)
n.i += 1
n.next()
}
}()
}

44
notify_test.go Normal file
View File

@ -0,0 +1,44 @@
package l_notify
import (
"fmt"
"gitea.cdlsxd.cn/self-tools/l_request"
cron2 "github.com/robfig/cron/v3"
"testing"
"time"
)
func TestNotify(t *testing.T) {
var a int32
c := &Notify{
DoFuc: func() (l_request.Response, error) {
a++
return l_request.Response{StatusCode: int(a)}, nil
},
DelayList: []int32{3},
ResultHandle: func(r l_request.Response, e error) (stop bool) {
fmt.Printf("%d-----%s\n", r.StatusCode, time.Now().Format(time.TimeOnly))
if r.StatusCode == 5 {
a = 0
return true
}
return false
},
}
err := c.Notify()
if err != nil {
fmt.Print(a)
}
}
func TestNotify2(t *testing.T) {
cron := cron2.New()
id, _ := cron.AddFunc("@every 1s", func() { fmt.Println("Every 1") })
cron.Start()
time.Sleep(3 * time.Second)
cron.Remove(id)
cron.AddFunc("@every 1s", func() { fmt.Println("Every 2") })
//cron.Start()
select {}
}