forked from mantzas/patron
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathservice.go
More file actions
181 lines (153 loc) · 3.75 KB
/
service.go
File metadata and controls
181 lines (153 loc) · 3.75 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package patron
import (
"context"
"errors"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/beatlabs/patron/observability"
"github.com/beatlabs/patron/observability/log"
)
const (
srv = "srv"
ver = "ver"
host = "host"
)
// Component represents a long-running unit started by the Service.
// Implementations should return when the context is canceled.
type Component interface {
Run(ctx context.Context) error
}
// Service manages application lifecycle and observability setup.
// It starts a default HTTP component for management endpoints.
type Service struct {
name string
version string
termSig chan os.Signal
sighupHandler func()
observabilityCfg observability.Config
observabilityProvider *observability.Provider
}
// New creates a new Service instance with sane defaults and optional configuration.
func New(name, version string, options ...OptionFunc) (*Service, error) {
if name == "" {
return nil, errors.New("name is required")
}
if version == "" {
version = "dev"
}
var err error
ctx := context.Background()
cfg := observabilityConfig(name, version)
observabilityProvider, err := observability.Setup(ctx, cfg)
if err != nil {
return nil, err
}
s := &Service{
name: name,
version: version,
termSig: make(chan os.Signal, 1),
sighupHandler: func() {
slog.Debug("sighup received: nothing setup")
},
observabilityCfg: cfg,
observabilityProvider: observabilityProvider,
}
optionErrors := make([]error, 0)
for _, option := range options {
err = option(s)
if err != nil {
optionErrors = append(optionErrors, err)
}
}
if len(optionErrors) > 0 {
return nil, errors.Join(optionErrors...)
}
s.setupOSSignal()
return s, nil
}
// Run starts the provided components and blocks until termination or a component error.
func (s *Service) Run(ctx context.Context, components ...Component) error {
if len(components) == 0 || components[0] == nil {
return errors.New("components are empty or nil")
}
defer func() {
ctx, cnl := context.WithTimeout(context.Background(), 5*time.Second)
defer cnl()
err := s.observabilityProvider.Shutdown(ctx)
if err != nil {
slog.Error("failed to close observability provider", log.ErrorAttr(err))
}
}()
ctx, cnl := context.WithCancel(ctx)
chErr := make(chan error, len(components))
wg := sync.WaitGroup{}
wg.Add(len(components))
for _, cp := range components {
go func(c Component) {
defer wg.Done()
chErr <- c.Run(ctx)
}(cp)
}
log.FromContext(ctx).Info("service started", slog.String("name", s.name))
ee := make([]error, 0, len(components))
ee = append(ee, s.waitTermination(chErr))
cnl()
wg.Wait()
close(chErr)
for err := range chErr {
ee = append(ee, err)
}
return errors.Join(ee...)
}
func (s *Service) setupOSSignal() {
signal.Notify(s.termSig, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
}
func (s *Service) waitTermination(chErr <-chan error) error {
for {
select {
case sig := <-s.termSig:
slog.Info("signal received", slog.Any("type", sig))
switch sig {
case syscall.SIGHUP:
s.sighupHandler()
return nil
default:
return nil
}
case err := <-chErr:
if err != nil {
slog.Info("component error received")
}
return err
}
}
}
func observabilityConfig(name, version string) observability.Config {
var lvl string
lvl, ok := os.LookupEnv("PATRON_LOG_LEVEL")
if !ok {
lvl = "info"
}
hostname, err := os.Hostname()
if err != nil {
hostname = host
}
attrs := []slog.Attr{
slog.String(srv, name),
slog.String(ver, version),
slog.String(host, hostname),
}
return observability.Config{
Name: name,
Version: version,
LogConfig: log.Config{
Attributes: attrs,
IsJSON: false,
Level: lvl,
},
}
}