https://github.com/goxtools/watcher
watcher any for Golang
https://github.com/goxtools/watcher
go golang panic retry watcher
Last synced: 4 months ago
JSON representation
watcher any for Golang
- Host: GitHub
- URL: https://github.com/goxtools/watcher
- Owner: goxtools
- License: apache-2.0
- Created: 2022-01-10T16:06:32.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-09-12T14:32:58.000Z (over 2 years ago)
- Last Synced: 2024-12-04T09:09:00.914Z (about 1 year ago)
- Topics: go, golang, panic, retry, watcher
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 1
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# watcher any for Golang
[](https://github.com/goxtools/watcher/actions/workflows/go.test.yml)
# Installation
```go
go get github.com/goxtools/watcher@v0.0.1
```
# Quickstart
### for consumer [old version]
```diff
- ch := make(chan int, c.GetConsumerNumber())
- for {
- ch <- 1
- go func(channel chan int, c mq.Consumer) {
- defer gorecover.GoRecover(context.Background(), c.GetConsumerName()+"job err")
- ctx := new(Context)
- if err := c.Consumer(ctx); err != nil {
- <-channel
- return
- }
- <-channel
- }(ch, c)
-}
```
### for consumer [watcher]
```go
for i := 0; i < c.GetConsumerNumber(); i++{
go func() {
w := NewWatcher(10, time.Second*1, time.Second*10)
w.On(func(args ...interface{}) {
c.Consumer(new(Context))
}
}
}
```
# Test Case
```go
func TestWatcher(t *testing.T) {
rand.Seed(time.Now().UnixNano())
// Retry 10 times, the retry interval is 1 second,
// after 10 seconds of stable operation,
// reset the number of retries
w := NewWatcher(10, time.Second*1, time.Second*10)
w.On(func(args ...interface{}) {
for {
if rand.Int()%9 == 0 {
panic("crash")
}
time.Sleep(500 * time.Millisecond)
fmt.Printf("%#v\n", w)
}
})
}
```