-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(example): add some examples (#25)
- Loading branch information
Showing
4 changed files
with
41 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,4 @@ | |
vendor/ | ||
.idea | ||
_backup | ||
_example | ||
.todo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
9 changes: 6 additions & 3 deletions
9
middleware/recovery/example_test.go → _example/middleware/recovery/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters