-
Notifications
You must be signed in to change notification settings - Fork 0
/
standalone.go
70 lines (57 loc) · 1.36 KB
/
standalone.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
package standalone
import (
"context"
"os/signal"
"sync"
"syscall"
"github.com/galgotech/fermions-workflow/pkg/log"
"github.com/galgotech/fermions-workflow/pkg/server"
"github.com/galgotech/fermions-workflow/pkg/setting"
"github.com/galgotech/fermions-workflow/pkg/worker"
)
func New(s setting.Setting, server *server.Server, worker *worker.Worker) *Standalone {
return &Standalone{
log: log.New("standalone"),
setting: s,
worker: worker,
server: server,
}
}
type Standalone struct {
log log.Logger
setting setting.Setting
worker *worker.Worker
server *server.Server
}
func (s *Standalone) Execute() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
// run server
go func() {
if err := s.worker.Execute(); err != nil {
s.log.Error("server", "error", err)
return
}
}()
// run worker
go func() {
if err := s.server.Execute(); err != nil {
s.log.Error("server", "error", err)
return
}
}()
// Create context that listens for the interrupt signal from the OS.
ctx := context.Background()
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
// Listen for the interrupt signal.
<-ctx.Done()
// TODO: implement force shutting down "press Ctrl+C again to force"
s.log.Info("shutting down gracefully")
stop()
s.worker.Shutdown()
s.log.Info("Exiting")
wg.Done()
}()
wg.Wait()
}