-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
48 lines (39 loc) · 785 Bytes
/
queue.go
File metadata and controls
48 lines (39 loc) · 785 Bytes
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
package fsq
import (
"context"
"os"
"os/signal"
"syscall"
)
type Queue struct {
shuttingDown IAtomic
signal chan os.Signal
consumer IQueue
}
func New(shuttingDown IAtomic, consumer IQueue) *Queue {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGHUP)
return &Queue{
shuttingDown: shuttingDown,
signal: c,
consumer: consumer,
}
}
func (q *Queue) Run(ct context.Context) error {
ctx, cancel := context.WithCancel(ct)
defer cancel()
go func() {
select {
case <-q.signal:
q.shuttingDown.Set(true)
cancel()
case <-ctx.Done():
q.shuttingDown.Set(true)
}
}()
if err := q.consumer.Consume(ctx); err != nil {
return err
}
<-ctx.Done()
return nil
}