-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
56 lines (48 loc) · 1.19 KB
/
service.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
/*
* Copyright (c) 2021-present unTill Pro, Ltd.
*/
package router2
import (
"context"
"log"
"time"
ibusnats "github.com/untillpro/airs-ibusnats"
)
// Service s.e.
// if Service use port 443, it will be started safely with TLS and use Lets Encrypt certificate
type Service struct {
RouterParams
QueuePartitions ibusnats.QueuesPartitionsMap
srvs []interface{}
busTimeout time.Duration
}
type routerKeyType string
const routerKey = routerKeyType("router")
// Start s.e.
func (s *Service) Start(ctx context.Context) (context.Context, error) {
s.srvs = ProvideBP2(ctx, s.RouterParams, s.busTimeout)
for _, srvIntf := range s.srvs {
// simulate pipeline.ServiceOperator
srv := srvIntf.(interface {
Prepare(work interface{}) error
Run(ctx context.Context)
})
if err := srv.Prepare(nil); err != nil {
return ctx, err
}
go srv.Run(ctx)
}
if len(s.NATSServers) == 0 {
log.Println("Router started in router-only mode")
} else {
log.Println("Router started")
}
return context.WithValue(ctx, routerKey, s), nil
}
// Stop s.e.
func (s *Service) Stop(ctx context.Context) {
for _, srvIntf := range s.srvs {
srv := srvIntf.(interface{ Stop() })
srv.Stop()
}
}