From f0b1ff56d1b85920bfb4eb3a4fea816777ecf200 Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Thu, 20 Feb 2025 16:39:27 +0800 Subject: [PATCH] first push --- func.go | 28 ++++++++++++++++ go.mod | 8 +++++ notify.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ notify_test.go | 44 +++++++++++++++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 func.go create mode 100644 go.mod create mode 100644 notify.go create mode 100644 notify_test.go diff --git a/func.go b/func.go new file mode 100644 index 0000000..c5f0ca2 --- /dev/null +++ b/func.go @@ -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 + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cf27480 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/notify.go b/notify.go new file mode 100644 index 0000000..009d6f0 --- /dev/null +++ b/notify.go @@ -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() + } + }() + +} diff --git a/notify_test.go b/notify_test.go new file mode 100644 index 0000000..c27291a --- /dev/null +++ b/notify_test.go @@ -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 {} +}