-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiodic-task.go
More file actions
112 lines (101 loc) · 2.78 KB
/
periodic-task.go
File metadata and controls
112 lines (101 loc) · 2.78 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package grace
import (
"context"
"time"
)
// PeriodicTaskConfig holds the configuration for periodic task execution
type PeriodicTaskConfig struct {
taskName string
interval time.Duration
logger Logger
runOnStart bool // whether to run the task immediately on start
stopOnError bool // whether to stop on task error
}
// PeriodicTaskOption defines the option for periodic task
type PeriodicTaskOption func(*PeriodicTaskConfig)
// WithTaskLogger sets a custom logger for the periodic task
func WithTaskLogger(logger Logger) PeriodicTaskOption {
return func(c *PeriodicTaskConfig) {
c.logger = logger
}
}
// WithRunOnStart sets whether to run the task immediately on start
func WithRunOnStart(runOnStart bool) PeriodicTaskOption {
return func(c *PeriodicTaskConfig) {
c.runOnStart = runOnStart
}
}
// WithStopOnTaskError sets whether to stop the task on error
func WithStopOnTaskError(stopOnError bool) PeriodicTaskOption {
return func(c *PeriodicTaskConfig) {
c.stopOnError = stopOnError
}
}
// RunPeriodicTask runs a task periodically with the given interval
// taskName: task name for logging
// interval: execution interval
// taskFunc: the task function to execute
// opts: optional configurations
//
// The task will stop when:
// - Context is cancelled
// - Task returns an error (if stopOnError is true)
func RunPeriodicTask(
ctx context.Context,
taskName string,
interval time.Duration,
taskFunc func(context.Context, time.Time) error,
opts ...PeriodicTaskOption,
) error {
config := &PeriodicTaskConfig{
taskName: taskName,
interval: interval,
logger: &defaultLogger{},
runOnStart: false,
stopOnError: true,
}
for _, opt := range opts {
opt(config)
}
// Run task immediately if configured
if config.runOnStart {
select {
case <-ctx.Done():
config.logger.Info("[%s] task stopped before first run", taskName)
return context.Cause(ctx)
default:
if err := taskFunc(ctx, time.Now()); err != nil {
config.logger.Error("[%s] task error on first run: %v", taskName, err)
if config.stopOnError {
return err
}
}
}
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
config.logger.Info("[%s] task stopped", taskName)
return context.Cause(ctx)
case t := <-ticker.C:
// Double-check context before executing task
// This avoids processing stale ticker events when context is cancelled
select {
case <-ctx.Done():
config.logger.Info("[%s] task stopped (skipped pending task)", taskName)
return context.Cause(ctx)
default:
// Context is not cancelled, execute the task
err := taskFunc(ctx, t)
if err != nil {
config.logger.Error("[%s] task error: %v", taskName, err)
if config.stopOnError {
return err
}
}
}
}
}
}