-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.go
89 lines (76 loc) · 1.52 KB
/
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
package scheduler
import (
"context"
"errors"
"sync"
"time"
"github.com/govice/golinksd/pkg/log"
"github.com/rs/xid"
)
type Task interface {
ID() string
Work() func() error
}
type Scheduler struct {
id string
queue []Task
sem chan struct{}
mu sync.Mutex
}
func New(concurrencyCeiling int) (*Scheduler, error) {
return &Scheduler{
id: xid.NewWithTime(time.Now()).String(),
sem: make(chan struct{}, concurrencyCeiling),
}, nil
}
var ErrTaskScheduled = errors.New("ErrTaskScheduled: task already scheduled")
func (s *Scheduler) Schedule(task Task) error {
s.mu.Lock()
for _, t := range s.queue {
if t.ID() == task.ID() {
return ErrTaskScheduled
}
}
s.queue = append(s.queue, task)
s.mu.Unlock()
return nil
}
func (s *Scheduler) Run(c context.Context) {
var wg sync.WaitGroup
defer func() {
log.Logln("waiting for running work to finish...")
wg.Wait()
}()
for {
if len(s.queue) == 0 {
// log.Logln("scheduler queue empty...")
time.Sleep(1 * time.Second)
continue
}
select {
case s.sem <- struct{}{}:
var t Task
if len(s.queue) > 1 {
s.mu.Lock()
t, s.queue = s.queue[0], s.queue[1:]
s.mu.Unlock()
} else if len(s.queue) == 1 {
s.mu.Lock()
t, s.queue = s.queue[0], nil
s.mu.Unlock()
}
wg.Add(1)
go func() {
log.Logln(t.ID(), "executing...")
if err := t.Work()(); err != nil {
log.Errln(t.ID(), "failed")
}
<-s.sem
wg.Done()
}()
case <-c.Done():
log.Logln(s.id, "stopping scheduler limiter")
return
}
}
}