1
1
package mysql
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"sync"
6
7
"time"
@@ -32,15 +33,16 @@ type taskHandle struct {
32
33
33
34
runner DriverHandle
34
35
36
+ ctx context.Context
37
+ cancelFunc context.CancelFunc
35
38
waitCh chan * drivers.ExitResult
36
- doneCh chan struct {}
37
39
stats * common.TaskStatistics
38
40
39
41
driverConfig * common.MySQLDriverConfig
40
42
shutdown bool
41
43
}
42
44
43
- func newDtleTaskHandle (logger g.LoggerType , cfg * drivers.TaskConfig , state drivers.TaskState , started time.Time ) * taskHandle {
45
+ func newDtleTaskHandle (ctx context. Context , logger g.LoggerType , cfg * drivers.TaskConfig , state drivers.TaskState , started time.Time ) * taskHandle {
44
46
h := & taskHandle {
45
47
logger : logger ,
46
48
stateLock : sync.RWMutex {},
@@ -50,8 +52,8 @@ func newDtleTaskHandle(logger g.LoggerType, cfg *drivers.TaskConfig, state drive
50
52
completedAt : time.Time {},
51
53
exitResult : nil ,
52
54
waitCh : make (chan * drivers.ExitResult ),
53
- doneCh : make (chan struct {}),
54
55
}
56
+ h .ctx , h .cancelFunc = context .WithCancel (ctx )
55
57
go h .watchWaitCh ()
56
58
return h
57
59
}
@@ -61,9 +63,9 @@ func (h *taskHandle) watchWaitCh() {
61
63
case r := <- h .waitCh :
62
64
h .stateLock .Lock ()
63
65
h .exitResult = r
66
+ h .cancelFunc ()
64
67
h .stateLock .Unlock ()
65
- close (h .doneCh )
66
- case <- h .doneCh :
68
+ case <- h .ctx .Done ():
67
69
}
68
70
}
69
71
@@ -149,7 +151,7 @@ func (h *taskHandle) run(d *Driver) {
149
151
t := time .NewTimer (0 )
150
152
for {
151
153
select {
152
- case <- h .doneCh :
154
+ case <- h .ctx . Done () :
153
155
if ! t .Stop () { <- t .C }
154
156
return
155
157
case <- t .C :
@@ -181,12 +183,12 @@ func (h *taskHandle) NewRunner(d *Driver) (runner DriverHandle, err error) {
181
183
case common .TaskTypeSrc :
182
184
if h .driverConfig .OracleConfig != nil {
183
185
h .logger .Debug ("found oracle src" , "OracleConfig" , h .driverConfig .OracleConfig )
184
- runner , err = extractor .NewExtractorOracle (ctx , h .driverConfig , h .logger , d .storeManager , h .waitCh , d .ctx )
186
+ runner , err = extractor .NewExtractorOracle (ctx , h .driverConfig , h .logger , d .storeManager , h .waitCh , h .ctx )
185
187
if err != nil {
186
188
return nil , errors .Wrap (err , "NewExtractor" )
187
189
}
188
190
} else {
189
- runner , err = mysql .NewExtractor (ctx , h .driverConfig , h .logger , d .storeManager , h .waitCh , d .ctx )
191
+ runner , err = mysql .NewExtractor (ctx , h .driverConfig , h .logger , d .storeManager , h .waitCh , h .ctx )
190
192
if err != nil {
191
193
return nil , errors .Wrap (err , "NewOracleExtractor" )
192
194
}
@@ -196,13 +198,13 @@ func (h *taskHandle) NewRunner(d *Driver) (runner DriverHandle, err error) {
196
198
if h .driverConfig .KafkaConfig != nil {
197
199
h .logger .Debug ("found kafka" , "KafkaConfig" , h .driverConfig .KafkaConfig )
198
200
runner , err = kafka .NewKafkaRunner (ctx , h .driverConfig .KafkaConfig , h .logger ,
199
- d .storeManager , d .config .NatsAdvertise , h .waitCh , d .ctx )
201
+ d .storeManager , d .config .NatsAdvertise , h .waitCh , h .ctx )
200
202
if err != nil {
201
203
return nil , errors .Wrap (err , "NewKafkaRunner" )
202
204
}
203
205
} else {
204
206
runner , err = mysql .NewApplier (ctx , h .driverConfig , h .logger , d .storeManager ,
205
- d .config .NatsAdvertise , h .waitCh , d .eventer , h .taskConfig , d .ctx )
207
+ d .config .NatsAdvertise , h .waitCh , d .eventer , h .taskConfig , h .ctx )
206
208
if err != nil {
207
209
return nil , errors .Wrap (err , "NewApplier" )
208
210
}
@@ -301,6 +303,21 @@ func (h *taskHandle) Destroy() {
301
303
}
302
304
}
303
305
306
+ func (h * taskHandle ) GetExitResult () * drivers.ExitResult {
307
+ h .stateLock .Lock ()
308
+ defer h .stateLock .Unlock ()
309
+ if h .exitResult == nil {
310
+ return & drivers.ExitResult {
311
+ ExitCode : 0 ,
312
+ Signal : 0 ,
313
+ OOMKilled : false ,
314
+ Err : nil ,
315
+ }
316
+ } else {
317
+ return h .exitResult .Copy ()
318
+ }
319
+ }
320
+
304
321
type DriverHandle interface {
305
322
Run ()
306
323
0 commit comments