-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent.go
More file actions
175 lines (157 loc) · 3.45 KB
/
agent.go
File metadata and controls
175 lines (157 loc) · 3.45 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
/*
* Copyright (c) 2023 Alexey Khokhlov
*/
package pipelaner
import (
"context"
"fmt"
"time"
config "github.com/pipelane/pipelaner/gen/pipelaner"
"github.com/pipelane/pipelaner/internal/health"
"github.com/pipelane/pipelaner/internal/metrics"
"github.com/pipelane/pipelaner/internal/migrator"
"github.com/pipelane/pipelaner/internal/pprof"
"golang.org/x/sync/errgroup"
)
type Agent struct {
pipelaner *Pipelaner
hc *health.Server
metrics *metrics.Server
pprof *pprof.Server
cancel context.CancelFunc
cfg *config.Pipelaner
}
func NewAgent(file string) (*Agent, error) {
ctx := context.Background()
cfg, err := config.LoadFromPath(ctx, file)
if err != nil {
return nil, fmt.Errorf("read config: %w", err)
}
a := &Agent{
cfg: &cfg,
}
inits := []func(cfg *config.Pipelaner) error{
a.initAndMigrate,
a.initHealthCheck,
a.initMetricsServer,
a.initPprofServer,
a.initPipelaner,
}
for _, init := range inits {
if err = init(&cfg); err != nil {
return nil, err
}
}
return a, nil
}
func (a *Agent) initAndMigrate(cfg *config.Pipelaner) error {
mCfg := cfg.Settings.Migrations
if mCfg != nil {
m, err := migrator.NewMigrator(cfg)
if err != nil {
return fmt.Errorf("migration: %w", err)
}
err = m.Migrate()
if err != nil {
return fmt.Errorf("migrate: %w", err)
}
}
return nil
}
func (a *Agent) initHealthCheck(cfg *config.Pipelaner) error {
hcCfg := cfg.Settings.HealthCheck
if hcCfg != nil {
hc, err := health.NewHealthCheck(cfg)
if err != nil {
return fmt.Errorf("init health check: %w", err)
}
a.hc = hc
}
return nil
}
func (a *Agent) initMetricsServer(cfg *config.Pipelaner) error {
metricsCfg := cfg.Settings.Metrics
if metricsCfg != nil {
m, err := metrics.NewMetricsServer(metricsCfg)
if err != nil {
return fmt.Errorf("init metrics server: %w", err)
}
a.metrics = m
}
return nil
}
func (a *Agent) initPprofServer(cfg *config.Pipelaner) error {
pprofCfg := cfg.Settings.Pprof
if pprofCfg != nil {
p := pprof.NewServer(pprofCfg)
a.pprof = p
}
return nil
}
func (a *Agent) initPipelaner(cfg *config.Pipelaner) error {
pipelanerCfg := cfg.Pipelines
logCfg := cfg.Settings.Logger
metricsCfg := cfg.Settings.Metrics
mEnable := metricsCfg != nil
p, err := NewPipelaner(
pipelanerCfg,
logCfg,
mEnable,
cfg.Settings.StartGCAfterMessageProcess,
)
if err != nil {
return fmt.Errorf("init pipeliner: %w", err)
}
a.pipelaner = p
return nil
}
func (a *Agent) Serve(ctx context.Context) error {
g := errgroup.Group{}
inputsCtx, cancel := context.WithCancel(ctx)
a.cancel = cancel
if a.hc != nil {
g.Go(func() error {
return a.hc.Serve(ctx)
})
}
if a.pprof != nil {
g.Go(func() error {
return a.pprof.Serve(ctx)
})
}
if a.metrics != nil {
g.Go(func() error {
return a.metrics.Serve(ctx)
})
}
g.Go(func() error {
return a.pipelaner.Run(inputsCtx)
})
if err := g.Wait(); err != nil {
return fmt.Errorf("run agent: %w", err)
}
return nil
}
func (a *Agent) Shutdown(ctx context.Context) error {
a.cancel()
time.Sleep(a.cfg.Settings.GracefulShutdownDelay.GoDuration())
if a.pprof != nil {
err := a.pprof.Shutdown(ctx)
if err != nil {
return fmt.Errorf("shutdown pprof: %w", err)
}
}
if a.metrics != nil {
err := a.metrics.Shutdown(ctx)
if err != nil {
return fmt.Errorf("shutdown metrics: %w", err)
}
}
if a.hc != nil {
err := a.hc.Shutdown()
if err != nil {
return fmt.Errorf("shutdown healthcheck: %w", err)
}
}
return nil
}