-
Notifications
You must be signed in to change notification settings - Fork 7
/
executors.go
457 lines (405 loc) · 8.73 KB
/
executors.go
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package routine
import (
"context"
"io"
"log"
"net/http"
"os"
"os/exec"
"sync"
"time"
_ "net/http/pprof"
"github.com/gorhill/cronexpr"
"github.com/x-mod/errors"
)
//GuaranteeExecutor struct, make sure of none error return
type GuaranteeExecutor struct {
exec Executor
}
//Guarantee insure exec NEVER PANIC
func Guarantee(exec Executor) *GuaranteeExecutor {
return &GuaranteeExecutor{exec}
}
//Execute implement Executor interface
func (g *GuaranteeExecutor) Execute(ctx context.Context) error {
do := func(ctx context.Context) (err error) {
defer func() {
if rc := recover(); rc != nil {
switch rv := rc.(type) {
case error:
err = rv.(error)
return
default:
err = errors.Errorf("%v", rv)
return
}
}
}()
err = g.exec.Execute(ctx)
return
}
if err := do(ctx); err != nil {
log.Println("execute failed:", err)
}
return nil
}
//RetryExecutor struct
type RetryExecutor struct {
retryTimes int
exec Executor
}
type _retry struct{}
//FromRetry current retied times
func FromRetry(ctx context.Context) int {
if ctx != nil {
retried := ctx.Value(_retry{})
if retried != nil {
return retried.(int)
}
}
return 0
}
//Retry new
func Retry(retry int, exec Executor) *RetryExecutor {
return &RetryExecutor{
retryTimes: retry,
exec: exec,
}
}
//Execute implement Executor interface
func (retry *RetryExecutor) Execute(ctx context.Context) error {
var err error
if retry.retryTimes == 0 {
retry.retryTimes = 1
}
for i := 0; i < retry.retryTimes; i++ {
if err = retry.exec.Execute(context.WithValue(ctx, _retry{}, i+1)); err != nil {
continue
}
return nil
}
return err
}
//RepeatExecutor struct
type RepeatExecutor struct {
repeatTimes int
repeatInterval time.Duration
exec Executor
}
type _repeat struct{}
//FromRepeat current repeated times
func FromRepeat(ctx context.Context) int {
if ctx != nil {
repeated := ctx.Value(_repeat{})
if repeated != nil {
return repeated.(int)
}
}
return 0
}
//Repeat new
func Repeat(repeat int, interval time.Duration, exec Executor) *RepeatExecutor {
return &RepeatExecutor{
repeatTimes: repeat,
repeatInterval: interval,
exec: exec,
}
}
//Execute implement Executor
func (r *RepeatExecutor) Execute(ctx context.Context) error {
fn := func(repeat int) error {
if err := r.exec.Execute(context.WithValue(ctx, _repeat{}, repeat)); err != nil {
return err
}
if r.repeatInterval > 0 {
<-time.After(r.repeatInterval)
}
return nil
}
if r.repeatTimes > 0 {
for i := 0; i < r.repeatTimes; i++ {
if err := fn(i + 1); err != nil {
return errors.Annotatef(err, "repeat %d failed:", i)
}
}
return nil
}
for i := 0; ; i++ {
if err := fn(i + 1); err != nil {
return errors.Annotatef(err, "repeat %d failed:", i)
}
}
}
//CrontabExecutor struct
type CrontabExecutor struct {
plan string
workday bool
weekend bool
mutes []*CrontabMute
exec Executor
}
type CrontabMute struct {
begin time.Time
end time.Time
}
type _crontab struct{}
//FromCrontab current crontab time
func FromCrontab(ctx context.Context) time.Time {
if ctx != nil {
crontab := ctx.Value(_crontab{})
if crontab != nil {
return crontab.(time.Time)
}
}
return time.Time{}
}
//Crontab new
func Crontab(plan string, exec Executor) *CrontabExecutor {
return &CrontabExecutor{
plan: plan,
exec: exec,
}
}
func (c *CrontabExecutor) Weekend(flag bool) {
c.weekend = flag
}
func (c *CrontabExecutor) Workday(flag bool) {
c.workday = flag
}
func (c *CrontabExecutor) Everyday(flag bool) {
if flag {
c.weekend = false
c.workday = false
}
}
func (c *CrontabExecutor) Mute(begin time.Time, end time.Time) {
m := &CrontabMute{begin: begin, end: end}
c.mutes = append(c.mutes, m)
}
func (c *CrontabExecutor) IsTimeMuted(tm time.Time) bool {
for _, m := range c.mutes {
if m.begin.Before(tm) && m.end.After(tm) {
return true
}
}
switch tm.Weekday() {
case time.Sunday, time.Saturday:
if c.workday {
return true
}
case time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday:
if c.weekend {
return true
}
}
return false
}
//Execute implement Executor
func (c *CrontabExecutor) Execute(ctx context.Context) error {
exp, err := cronexpr.Parse(c.plan)
if err != nil {
return err
}
next := exp.Next(time.Now())
if next.IsZero() {
return ErrNonePlan
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(next.Sub(time.Now())):
if c.IsTimeMuted(next) == false {
if err := c.exec.Execute(context.WithValue(ctx, _crontab{}, next)); err != nil {
return err
}
}
next = exp.Next(time.Now())
if next.IsZero() {
return ErrNonePlan
}
}
}
}
//CommandExecutor struct
type CommandExecutor struct {
command string
args []string
envs []string
stdin io.Reader
stdout io.Writer
stderr io.Writer
}
type CommandOpt func(*CommandExecutor)
func ARG(arg string) CommandOpt {
return func(c *CommandExecutor) {
c.args = append(c.args, arg)
}
}
func ENV(env string) CommandOpt {
return func(c *CommandExecutor) {
c.envs = append(c.envs, env)
}
}
func Stdin(rd io.Reader) CommandOpt {
return func(c *CommandExecutor) {
c.stdin = rd
}
}
func Stdout(wr io.Writer) CommandOpt {
return func(c *CommandExecutor) {
c.stdout = wr
}
}
func Stderr(wr io.Writer) CommandOpt {
return func(c *CommandExecutor) {
c.stderr = wr
}
}
//Command new
func Command(cmd string, opts ...CommandOpt) *CommandExecutor {
ce := &CommandExecutor{
command: cmd,
args: []string{},
envs: []string{},
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
}
for _, opt := range opts {
opt(ce)
}
return ce
}
//Execute implement Executor
func (cmd *CommandExecutor) Execute(ctx context.Context) error {
c := exec.CommandContext(ctx, cmd.command, cmd.args...)
c.Env = append(c.Env, cmd.envs...)
c.Stdin = cmd.stdin
c.Stdout = cmd.stdout
c.Stderr = cmd.stderr
if err := c.Start(); err != nil {
return err
}
return c.Wait()
}
//TimeoutExecutor struct
type TimeoutExecutor struct {
timeout time.Duration
exec Executor
}
//Timeout new
func Timeout(d time.Duration, exec Executor) *TimeoutExecutor {
return &TimeoutExecutor{
timeout: d,
exec: exec,
}
}
func (tm *TimeoutExecutor) Execute(ctx context.Context) error {
tmCtx, cancel := context.WithTimeout(ctx, tm.timeout)
defer cancel()
return New(tm.exec).Execute(tmCtx)
}
//DeadlineExecutor struct
type DeadlineExecutor struct {
deadline time.Time
exec Executor
}
//Deadline new
func Deadline(d time.Time, exec Executor) *DeadlineExecutor {
return &DeadlineExecutor{
deadline: d,
exec: exec,
}
}
//Execute implement Executor
func (tm *DeadlineExecutor) Execute(ctx context.Context) error {
tmCtx, cancel := context.WithDeadline(ctx, tm.deadline)
defer cancel()
return New(tm.exec).Execute(tmCtx)
}
//ConcurrentExecutor struct
type ConcurrentExecutor struct {
concurrent int
exec Executor
wg sync.WaitGroup
}
//Concurrent new
func Concurrent(c int, exec Executor) *ConcurrentExecutor {
return &ConcurrentExecutor{
concurrent: c,
exec: exec,
}
}
//Execute implement Executor
func (ce *ConcurrentExecutor) Execute(ctx context.Context) error {
for i := 0; i < ce.concurrent; i++ {
ce.wg.Add(1)
go func(i int) {
defer ce.wg.Done()
if err := ce.exec.Execute(ctx); err != nil {
log.Println("concurrent ", i, " failed:", err)
}
}(i)
}
ce.wg.Wait()
return nil
}
//ParallelExecutor
type ParallelExecutor struct {
execs []Executor
wg sync.WaitGroup
}
//Parallel new
func Parallel(execs ...Executor) *ParallelExecutor {
return &ParallelExecutor{
execs: execs,
}
}
//Execute implement Executor
func (pe *ParallelExecutor) Execute(ctx context.Context) error {
for i, exec := range pe.execs {
pe.wg.Add(1)
go func(i int, exec Executor) {
defer pe.wg.Done()
if err := exec.Execute(ctx); err != nil {
log.Println("parallel ", i, " failed:", err)
}
}(i, exec)
}
pe.wg.Wait()
return nil
}
//AppendExecutor
type AppendExecutor struct {
execs []Executor
}
//Append new
func Append(execs ...Executor) *AppendExecutor {
executor := &AppendExecutor{
execs: []Executor{},
}
executor.execs = append(executor.execs, execs...)
return executor
}
func (ae *AppendExecutor) Append(execs ...Executor) {
ae.execs = append(ae.execs, execs...)
}
func (ae *AppendExecutor) Execute(ctx context.Context) error {
for i, exec := range ae.execs {
if err := exec.Execute(ctx); err != nil {
return errors.Annotatef(err, "no(%d)", i)
}
}
return nil
}
//Profiling
type ProfilingExecutor struct {
address string
}
func Profiling(addr string) *ProfilingExecutor {
return &ProfilingExecutor{address: addr}
}
func (d *ProfilingExecutor) Execute(ctx context.Context) error {
return http.ListenAndServe(d.address, nil)
}