Skip to content

Commit

Permalink
chore(example): add some examples (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Oct 27, 2024
1 parent 769eafa commit 89fec4b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
vendor/
.idea
_backup
_example
.todo
29 changes: 29 additions & 0 deletions _example/entry/context/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/flc1125/go-cron/v4"
)

func main() {
c := cron.New(
cron.WithSeconds(),
)

_, _ = c.AddFunc("* * * * * *", func(ctx context.Context) error {
entry, ok := cron.EntryFromContext(ctx)
if ok {
log.Println(fmt.Sprintf("entry id: %d", entry.ID))
}
return nil
})

c.Start()
defer c.Stop()

time.Sleep(5 * time.Second)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package recovery_test
package main

import (
"context"
"time"

"github.com/flc1125/go-cron/v4"
"github.com/flc1125/go-cron/v4/middleware/recovery"
)

func Example() {
func main() {
c := cron.New()
c.Use(recovery.New())

_, _ = c.AddFunc("* * * * * ?", func(context.Context) error {
_, _ = c.AddFunc("* * * * * *", func(context.Context) error {
panic("YOLO")
})

c.Start()
defer c.Stop()

time.Sleep(2 * time.Second)
}
9 changes: 6 additions & 3 deletions middleware/recovery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ The `recovery` middleware is a middleware for [go-cron](https://github.com/flc11
## Usage

```go
package recovery_test
package main

import (
"context"
"time"

"github.com/flc1125/go-cron/v4"
"github.com/flc1125/go-cron/v4/middleware/recovery"
)

func Example() {
func main() {
c := cron.New()
c.Use(recovery.New())

c.AddFunc("* * * * * ?", func(ctx context.Context) error {
_, _ = c.AddFunc("* * * * * *", func(context.Context) error {
panic("YOLO")
})

c.Start()
defer c.Stop()

time.Sleep(2 * time.Second)
}
```

0 comments on commit 89fec4b

Please sign in to comment.