-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.go
More file actions
189 lines (159 loc) · 4.39 KB
/
job.go
File metadata and controls
189 lines (159 loc) · 4.39 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package dgcron
import (
"context"
"log"
"sync"
"time"
dgctx "github.com/darwinOrg/go-common/context"
dgerr "github.com/darwinOrg/go-common/enums/error"
dglogger "github.com/darwinOrg/go-logger"
"github.com/panjf2000/ants/v2"
"github.com/rolandhe/saber/gocc"
)
type RecoverProcessor func(ctx *dgctx.DgContext, name, errorMessage string)
var recoverProcessors []RecoverProcessor
func RegisterRecoverProcessor(processor RecoverProcessor) {
recoverProcessors = append(recoverProcessors, processor)
}
func AddFixDurationJob(name string, duration time.Duration, job DgJob) {
log.Printf("add fix duration job, name: %s, duration: %s", name, duration)
go func() {
for {
go func() {
ctx := dgctx.SimpleDgContext()
defer func() {
if err := recover(); err != nil {
dglogger.Errorf(ctx, "job panic, name: %s, err: %v", name, err)
processRecover(ctx, name, err)
}
}()
job(ctx)
}()
time.Sleep(duration)
}
}()
}
func AddFixDurationJobWithTimeout(name string, duration time.Duration, timeout time.Duration, job DgJobWithCancel) {
log.Printf("add fix duration job, name: %s, duration: %s, timeout: %d", name, duration, timeout)
go func() {
for {
go func() {
ctx, cancel := dgctx.WithTimeout(context.Background(), timeout)
defer cancel()
defer func() {
if err := recover(); err != nil {
dglogger.Errorf(ctx, "job with timeout panic, name: %s, err: %v", name, err)
processRecover(ctx, name, err)
}
}()
job(ctx, cancel)
}()
time.Sleep(duration)
}
}()
}
func AddFixDelayJob(name string, delay time.Duration, job DgJob) {
log.Printf("add fix delay job, name: %s, delay: %s", name, delay)
go func() {
for {
func() {
ctx := dgctx.SimpleDgContext()
defer func() {
if err := recover(); err != nil {
dglogger.Errorf(ctx, "job panic, name: %s, err: %v", name, err)
processRecover(ctx, name, err)
}
}()
job(ctx)
}()
time.Sleep(delay)
}
}()
}
func AddFixDelayJobWithTimeout(name string, delay time.Duration, timeout time.Duration, job DgJobWithCancel) {
log.Printf("add fix delay job, name: %s, delay: %s, timeout: %d", name, delay, timeout)
go func() {
for {
func() {
ctx, cancel := dgctx.WithTimeout(context.Background(), timeout)
defer cancel()
defer func() {
if err := recover(); err != nil {
dglogger.Errorf(ctx, "job with timeout panic, name: %s, err: %v", name, err)
processRecover(ctx, name, err)
}
}()
job(ctx, cancel)
}()
time.Sleep(delay)
}
}()
}
func RunSemaphoreJob(ctx *dgctx.DgContext, name string, semaphore gocc.Semaphore, acquireTimeout time.Duration, job DgJob) bool {
if !semaphore.AcquireTimeout(acquireTimeout) {
return false
}
go func() {
defer semaphore.Release()
dglogger.Infof(ctx, "run semaphore job, name: %s", name)
defer func() {
if err := recover(); err != nil {
dglogger.Errorf(ctx, "job panic, name: %s, err: %v", name, err)
processRecover(ctx, name, err)
}
}()
job(ctx)
}()
return true
}
func RunSemaphoreJobWithTimeout(ctx *dgctx.DgContext, name string, semaphore gocc.Semaphore, acquireTimeout time.Duration, jobTimeout time.Duration, job DgJobWithCancel) bool {
if !semaphore.AcquireTimeout(acquireTimeout) {
return false
}
go func() {
defer semaphore.Release()
dglogger.Infof(ctx, "run semaphore job, name: %s", name)
cancel := ctx.WithTimeout(context.Background(), jobTimeout)
defer cancel()
defer func() {
if err := recover(); err != nil {
dglogger.Errorf(ctx, "job with timeout panic, name: %s, err: %v", name, err)
processRecover(ctx, name, err)
}
}()
job(ctx, cancel)
}()
return true
}
func RunParallelMap[T any, R any](slice []T, poolSize int, iteratee func(item T, index int) R) []R {
p, _ := ants.NewPool(poolSize)
defer p.Release()
result := make([]R, len(slice))
var wg sync.WaitGroup
wg.Add(len(slice))
for i, item := range slice {
_ = p.Submit(func() {
defer wg.Done()
result[i] = iteratee(item, i)
})
}
wg.Wait()
return result
}
func processRecover(ctx *dgctx.DgContext, name string, err any) {
if len(recoverProcessors) == 0 {
return
}
var errorMessage string
switch err.(type) {
case string:
errorMessage = err.(string)
case error:
errorMessage = err.(error).Error()
default:
errorMessage = dgerr.SYSTEM_ERROR.Message
}
for _, processor := range recoverProcessors {
processor(ctx, name, errorMessage)
}
}