-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
69 lines (59 loc) · 1.03 KB
/
app.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
/*
*
* Copyright 2022-present Zander Schwid & Co. LLC. All rights reserved.
*
*/
package main
import (
"context"
"fmt"
"github.com/pkg/errors"
"go.uber.org/atomic"
"os"
"os/signal"
"syscall"
"time"
)
type App struct {
done chan struct{}
err atomic.Error
}
func NewApp() *App {
t := &App{
done: make(chan struct{}),
}
t.init()
return t
}
func (t *App) Context() context.Context {
return t
}
func (t *App) Deadline() (deadline time.Time, ok bool) {
return time.Time{}, false
}
func (t *App) Done() <-chan struct{} {
return t.done
}
func (t *App) Err() error {
return t.err.Load()
}
func (t *App) Value(key interface{}) interface{} {
return nil
}
func (t *App) init() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan,
os.Interrupt,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
for signal := range signalChan {
fmt.Printf("Signal '%v'\n", signal)
t.err.Store(errors.Errorf("interrupted by signal '%v'", signal))
t.done <- struct{}{}
break
}
}()
}