-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
84 lines (69 loc) · 1.66 KB
/
example_test.go
File metadata and controls
84 lines (69 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cronlib_test
import (
"context"
"fmt"
"time"
"github.com/raythurman2386/cronlib"
)
func ExampleCron_AddJob() {
c := cronlib.NewCron()
// Run every second
// Note: We use a channel to ensure the example output is deterministic
// for the purpose of this testable example.
done := make(chan struct{})
_, err := c.AddJob("* * * * * *", func() {
fmt.Println("Job executed")
close(done)
})
if err != nil {
fmt.Println("Error scheduling job:", err)
return
}
c.Start()
defer c.Stop()
<-done
// Output: Job executed
}
func ExampleCron_AddJobWithOptions() {
c := cronlib.NewCron()
// Define a job with specific options
opts := cronlib.JobOptions{
Overlap: cronlib.OverlapForbid, // Skip if previous run is still active
}
_, err := c.AddJobWithOptions("*/5 * * * * *", func(ctx context.Context) {
fmt.Println("Job with context running")
}, opts)
if err != nil {
fmt.Println("Error:", err)
}
c.Start()
// Allow time for execution in a real app
time.Sleep(1 * time.Second)
c.Stop()
}
func ExampleChain() {
// Define a custom wrapper that logs execution
loggingWrapper := func(next func(context.Context) error) func(context.Context) error {
return func(ctx context.Context) error {
fmt.Println("Starting job...")
err := next(ctx)
fmt.Println("Job finished")
return err
}
}
c := cronlib.NewCron()
opts := cronlib.JobOptions{
Wrappers: []cronlib.JobWrapper{loggingWrapper},
}
_, err := c.AddJobWithOptions("@every 1s", func(ctx context.Context) {
fmt.Println("Doing work")
}, opts)
if err != nil {
fmt.Println("Error:", err)
return
}
c.Start()
// Wait for one run
time.Sleep(1100 * time.Millisecond)
c.Stop()
}