-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.go
60 lines (51 loc) · 1.04 KB
/
dispatch.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
/**
* File: dispatch.go
* Author: Ming Cheng<[email protected]>
*
* Created Date: Friday, December 25th 2020, 10:46:17 pm
* Last Modified: Sunday, December 27th 2020, 8:39:40 pm
*
* http://www.opensource.org/licenses/MIT
*/
package simplyddns
import (
"context"
"sync"
"time"
)
type Dispatch struct {
wg sync.WaitGroup
jobs []*Job
Timeout time.Duration
Configs []JobConfig
}
// Start the dispatch
func (d *Dispatch) Start(ctx context.Context) {
for _, v := range d.jobs {
d.wg.Add(1)
go v.Start(ctx)
}
d.wg.Wait()
}
// Stop the dispatch
func (d *Dispatch) Stop() {
for _, v := range d.jobs {
d.wg.Done()
go v.Stop()
}
}
func NewDispatch(configs []JobConfig) (*Dispatch, error) {
log.Debugf("new displatch instance with configure: %v", configs)
dispatch := &Dispatch{
Configs: configs,
}
for _, v := range configs {
if job, err := NewJob(v); err != nil {
return nil, err
} else {
log.Debugf("add job %v to dispatch queue", job)
dispatch.jobs = append(dispatch.jobs, job)
}
}
return dispatch, nil
}