-
Notifications
You must be signed in to change notification settings - Fork 519
/
worker_scheduler.go
171 lines (152 loc) · 4.65 KB
/
worker_scheduler.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package helper
import (
"context"
"github.com/apache/incubator-devlake/errors"
"sync"
"sync/atomic"
"time"
"github.com/apache/incubator-devlake/plugins/core"
"github.com/panjf2000/ants/v2"
)
// WorkerScheduler runs asynchronous tasks in parallel with throttling support
type WorkerScheduler struct {
waitGroup sync.WaitGroup
pool *ants.Pool
ticker *time.Ticker
workerErrors []error
ctx context.Context
mu sync.Mutex
counter int32
logger core.Logger
}
//var callframeEnabled = os.Getenv("ASYNC_CF") == "true"
// NewWorkerScheduler creates a WorkerScheduler
func NewWorkerScheduler(
ctx context.Context,
workerNum int,
maxWork int,
maxWorkDuration time.Duration,
logger core.Logger,
) (*WorkerScheduler, errors.Error) {
if maxWork <= 0 {
return nil, errors.Default.New("maxWork less than 1")
}
if maxWorkDuration <= 0 {
return nil, errors.Default.New("maxWorkDuration less than 1")
}
s := &WorkerScheduler{
ctx: ctx,
ticker: time.NewTicker(maxWorkDuration / time.Duration(maxWork)),
logger: logger,
}
pool, err := ants.NewPool(workerNum, ants.WithPanicHandler(func(i interface{}) {
s.checkError(i)
}))
if err != nil {
return nil, errors.Convert(err)
}
s.pool = pool
return s, nil
}
// SubmitBlocking enqueues a async task to ants, the task will be executed in future when timing is right.
// It doesn't return error because it wouldn't be any when with a Blocking semantic, returned error does nothing but
// causing confusion, more often, people thought it is returned by the task.
// Since it is async task, the callframes would not be available for production mode, you can export Environment
// Varaible ASYNC_CF=true to enable callframes capturing when debugging.
// IMPORTANT: do NOT call SubmitBlocking inside the async task, it is likely to cause a deadlock, call
// SubmitNonBlocking instead when number of tasks is relatively small.
func (s *WorkerScheduler) SubmitBlocking(task func() errors.Error) {
if s.HasError() {
return
}
s.waitGroup.Add(1)
s.checkError(s.pool.Submit(func() {
defer s.waitGroup.Done()
id := atomic.AddInt32(&s.counter, 1)
s.logger.Debug("schedulerJob >>> %d started", id)
defer s.logger.Debug("schedulerJob <<< %d ended", id)
if s.HasError() {
return
}
// normal error
select {
case <-s.ctx.Done():
panic(s.ctx.Err())
case <-s.ticker.C:
err := task()
if err != nil {
panic(err)
}
}
}))
}
/*
func (s *WorkerScheduler) gatherCallFrames() string {
cf := "set Environment Varaible ASYNC_CF=true to enable callframes capturing"
if callframeEnabled {
cf = utils.GatherCallFrames(1)
}
return cf
}
*/
func (s *WorkerScheduler) appendError(err error) {
s.mu.Lock()
defer s.mu.Unlock()
s.workerErrors = append(s.workerErrors, err)
}
func (s *WorkerScheduler) checkError(err interface{}) {
if err == nil {
return
}
s.appendError(err.(error))
}
// HasError return if any error occurred
func (s *WorkerScheduler) HasError() bool {
return len(s.workerErrors) > 0
}
// NextTick enqueues task in a NonBlocking manner, you should only call this method within task submitted by
// SubmitBlocking method
// IMPORTANT: do NOT call this method with a huge number of tasks, it is likely to eat up all available memory
func (s *WorkerScheduler) NextTick(task func() errors.Error) {
// to make sure task will be enqueued
s.waitGroup.Add(1)
go func() {
defer s.waitGroup.Done()
s.checkError(task())
}()
}
// Wait blocks current go-routine until all workers returned
func (s *WorkerScheduler) Wait() errors.Error {
s.waitGroup.Wait()
if len(s.workerErrors) > 0 {
for _, err := range s.workerErrors {
if errors.Is(err, context.Canceled) {
return errors.Default.Wrap(err, "task canceled")
}
}
return errors.Default.Combine(s.workerErrors)
}
return nil
}
// Release resources
func (s *WorkerScheduler) Release() {
s.waitGroup.Wait()
s.pool.Release()
if s.ticker != nil {
s.ticker.Stop()
}
}